ArgoCD is a GitOps tool with a straightforward but powerful objective: to declaratively deploy applications to Kubernetes by managing application resources directly from version control systems, such as Git repositories. Every commit to the repository represents a change, which ArgoCD can apply to the Kubernetes cluster either manually or automatically. This approach ensures that deployment processes are fully controlled through version-controlled files, fostering an explicit and auditable release process.
For example, releasing a new application version involves updating the image tag in the resource files and committing the changes to the repository. ArgoCD syncs with the repository and seamlessly deploys the new version to the cluster.
Since ArgoCD itself operates on Kubernetes, it is straightforward to set up and integrates seamlessly with lightweight Kubernetes distributions like K3s. In this tutorial, we will demonstrate how to configure a local Kubernetes cluster using K3D and deploy applications with ArgoCD, utilizing the argocd-example-apps repository as a practical example.
Prerequisites
Before we begin, ensure you have the following installed:
Check that the application is deployed successfully:
Terminal window
$kubectlgetpods
NAMEREADYSTATUSRESTARTSAGE
guestbook-ui-649789b49c-zwjt81/1Running05m36s
Access the deployed application by exposing it via a NodePort or LoadBalancer:
Terminal window
$kubectlport-forwardsvc/guestbook-ui8081:80
Forwardingfrom127.0.0.1:8081 ->80
Forwardingfrom [::1]:8081 -> 80
Conclusion
In this tutorial, you’ve set up a local Kubernetes cluster using K3D and deployed applications with ArgoCD. This setup provides a simple and powerful way to practice GitOps workflows locally. By leveraging tools like ArgoCD, you can ensure your deployments are consistent, auditable, and declarative. Happy GitOps-ing!
K3D is a lightweight wrapper around k3s that allows you to run Kubernetes clusters inside Docker containers. While K3D is widely used for local development and testing, effective monitoring of services running on Kubernetes clusters is essential for debugging, performance tuning, and understanding resource usage.
In this blog, I will explore two popular monitoring tools for Kubernetes: Kubernetes Dashboard, the official web-based UI for Kubernetes, and Octant, a local, real-time, standalone dashboard developed by VMware. Both tools have unique strengths, and this guide will help you understand when to use one over the other.
Setting Up Kubernetes Dashboard On K3D
First you need to create a cluster using k3d cluster create:
Then, create a service account and bind role, to access the dashboard, you need a service account with the proper permissions. Create a service account and cluster role binding using the following YAML:
Note: Don’t forget to copy and paste the token when prompted.
Setting Up Octant on K3D
First, you need to install Octant, You can install Octant using a package manager or download it directly from the official releases. For example, on macOS, you can use Homebrew:
Terminal window
$brewinstalloctant
Next, on Linux just download the appropriate binary and move it to your path:
Both Kubernetes Dashboard and Octant offer valuable features for monitoring Kubernetes clusters in K3D. If you need a quick and easy way to monitor your local cluster with minimal setup, Octant is a great choice. On the other hand, if you want an experience closer to managing a production environment, Kubernetes Dashboard is the better option.
In this post, I’ll show you how to start with K3D, an awesome tool for running lightweight Kubernetes clusters using K3S on Docker. I hope this post will help you quickly set up and understand K3D. Let’s dive in!
What is K3S?
Before starting with K3D we need to know about K3S. Developed by Rancher Labs, K3S is a lightweight Kubernetes distribution designed for IoT and edge environments. It is a fully conformant Kubernetes distribution but optimized to work in resource-constrained settings by reducing its resource footprint and dependencies.
Key highlights of K3S include:
- Optimized for Edge: Ideal for small clusters and resource-limited environments.
- Built-In Tools: Networking (Flannel), ServiceLB Load-Balancer controller and Ingress (Traefik) are included, minimizing setup complexity.
- Compact Design: K3S simplifies Kubernetes by bundling everything into a single binary and removing unnecessary components like legacy APIs.
Now let’s dive into K3D.
What is K3D?
K3D acts as a wrapper for K3S, making it possible to run K3S clusters inside Docker containers. It provides a convenient way to manage these clusters, offering speed, simplicity, and scalability for local Kubernetes environments.
Here’s why K3D is popular:
- Ease of Use: Quickly spin up and tear down clusters with simple commands.
- Resource Efficiency: Run multiple clusters on a single machine without significant overhead.
- Development Focus: Perfect for local development, CI/CD pipelines, and testing.
Let’s move on to how you can set up K3D and start using it.
Requirements
Before starting with K3D, make sure you have installed the following prerequisites based on your operating system.
- Docker
Follow the Docker installation guide for your operating system. Alternatively, you can simplify the process with these commands:
Terminal window
$curl-fsSLhttps://get.docker.com-oget-docker.sh
$sudoshget-docker.sh
$sudousermod-aGdocker$USER#add user to the docker group
$dockerversion
Client:DockerEngine-Community
Version:27.4.0
APIversion:1.47
Goversion:go1.22.10
Gitcommit:bde2b89
Built:SatDec710:38:402024
OS/Arch:linux/amd64
Context:default
...
- Kubectl
The Kubernetes command-line tool, kubectl, is required to interact with your K3D cluster. Install it by following the instructions on the official Kubernetes documentation. Or you can follow this step:
Bootstrapping a cluster with configuration files allows you to automate and customize the process of setting up a K3D cluster. By using a configuration file, you can easily specify cluster details such as node count, roles, ports, volumes, and more, making it easy to recreate or modify clusters.
Here’s an example of a basic cluster configuration file my-cluster.yaml that sets up a K3D cluster with multiple nodes:
apiVersion: k3d.io/v1alpha5
kind: Simple
metadata:
name: my-cluster
servers: 1
agents: 2
image: rancher/k3s:v1.30.4-k3s1
ports:
- port: 30000-30100:30000-30100
nodeFilters:
- server:*
options:
k3s:
extraArgs:
- arg: --disable=traefik
nodeFilters:
- server:*
A K3D config to create a cluster named my-cluster with 1 server, 2 agents, K3S version v1.30.4-k3s1, host-to-server port mapping (30000-30100), and Traefik disabled on server nodes.
Once your K3D cluster is up and running, you can deploy applications onto the cluster. A deployment in Kubernetes ensures that a specified number of pod replicas are running, and it manages updates to those pods.
Use the kubectl create deployment command to define and start a deployment. For example:
Terminal window
$kubectlcreatedeploymentnginx--image=nginx
deployment.apps/nginxcreated
Check deployment status using kubectl get deplyment command:
Try to access using browser by using LoadBalancer EXTERNAL-IP:
Terminal window
$http://172.18.0.2:30893
Conclusion
K3D simplifies the process of running Kubernetes clusters with K3S on Docker, making it ideal for local development and testing. By setting up essential tools like Docker, kubectl, and K3D, you can easily create and manage clusters. You can deploy applications with just a few commands, expose them, and access them locally. K3D offers a flexible and lightweight solution for Kubernetes, allowing developers to experiment and work with clusters in an efficient way.
Thank you for taking the time to read this guide. I hope it was helpful in getting you started with K3D and Kubernetes!😀
So we will create 2 VMs using Terraform, then deploying a flask project and the database using Ansible.
🔨 Requirements
I used Ubuntu 22.04 LTS as the OS for this project. If you’re using a different OS, please make the necessary adjustments when installing the required dependencies.
The major pre-requisite for this setup is KVM hypervisor. So you need to install KVM in your system. If you use Ubuntu you can follow this step:
we will use the libvirt provider with Terraform to deploy a KVM Virtual Machine.
Create main.tf, just specify the provider and version you want to use:
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.8.1"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
Thereafter, run terraform init command to initialize the environment:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/template from the dependency lock file
- Reusing previous version of dmacvicar/libvirt from the dependency lock file
- Reusing previous version of hashicorp/null from the dependency lock file
- Using previously-installed hashicorp/template v2.2.0
- Using previously-installed dmacvicar/libvirt v0.8.1
- Using previously-installed hashicorp/null v3.2.3
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Now create our variables.tf. This variables.tf file defines inputs for the libvirt disk pool path, the Ubuntu 20.04 image URL as OS for the VMs , and a list of VM hostnames.
the script will provisions multiple KVM VMs using the Libvirt provider. It downloads an Ubuntu 20.04 base image, clones it for each VM, configures cloud-init for user and network settings, and deploys VMs with specified hostnames, 1GB memory, and SPICE graphics. The setup dynamically adapts based on the number of hostnames provided in var.vm_hostnames.
As I’ve mentioned, I’m using cloud-init, so lets setup the network config and cloud init under the config directory:
Terminal window
$mkdirconfig/
Then create our config/cloud_init.yml, just make sure that you configure your public ssh key for ssh access in the config:
#cloud-config
runcmd:
- sed -i '/PermitRootLogin/d' /etc/ssh/sshd_config
Next, create docker-compose.yml.j2 to templates. With this file we will create a container using docker compose:
version: '3.7'
services:
flask:
build: flask
container_name: app
restart: unless-stopped
ports:
- 5000:5000
environment:
- DEBUG=0
networks:
- flask_network
networks:
flask_network:
Next, create main.yml in tasks. With this file we will clone flask project, add compose file, replace config.py and create new container using docker compose:
First, you need to install Ansible. Just follow this link to install Ansible on your operating system installation guide.
After installation, create new directory called ansible-docker.
Terminal window
$mkdiransible-docker && cdansible-docker
Create a new file called ansible.cfg as the Ansible configuration setting and then define the inventory file.
[defaults]
inventory = hosts
host_key_checking = True
deprecation_warnings = False
collections = ansible.posix, community.general
Then create a new file called hosts, where the name is defined on ansible.cfg.
Terminal window
[example-server]
0.0.0.0ansible_user=root
NB: Don’t forget to change the IP Address and host name.
After setup the Ansible configuration setting & inventory file, let’s create a YAML file called playbook.yml
---
- name: Setup Docker on Ubuntu Server 22.04
hosts: all
become: true
remote_user: root
roles:
- config
- docker
Then create roles directory:
Config, On this directory I will create a directory called tasks. After that, I should create yaml file called main.yml to run update, upgrade & install many dependencies.
---
- name: Update&Upgrade
ansible.builtin.apt:
name: aptitude
state: present
update_cache: true
- name: Install dependencies
ansible.builtin.apt:
name:
- net-tools
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
- python3-pip
- virtualenv
- python3-setuptools
- gnupg-agent
- autoconf
- dpkg-dev
- file
- g++
- gcc
- libc-dev
- make
- pkg-config
- re2c
- wget
state: present
update_cache: true
Docker, On this directory create 2 directories called tasks & templates.
On tasks directory create new file called main.yml. This file contains Docker installation, Docker Compose installation & private registry setup.
---
- name: Add Docker GPG apt Key
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add repository into sources list
ansible.builtin.apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable