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:
- 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:
NB: I use Ubuntu 22.04 to install the requirements.
Create Your First Cluster
Basic
This command will create a cluster named mycluster with one control plane node.
Once the cluster is created, check its status using kubectl:
To ensure that the nodes in the cluster are active, run:
To list all the clusters created with K3D, use the following command:
Stop, start & delete your cluster, use the following command:
If you want to start a cluster with extra server and worker nodes, then extend the creation command like this:
After creating the cluster, you can verify its status using these commands:
Bootstrapping Cluster
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:
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.
The result after creation:
Create Simple Deployment
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:
Check deployment status using kubectl get deplyment command:
Expose the deployment:
Verify the Pod and Service:
Try to access using browser by using LoadBalancer EXTERNAL-IP:
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!😀