K3d: Lightweight Kubernetes with K3s in Docker

Nayan TankNayan Tank
3 min read

K3d is a lightweight wrapper that runs K3s (a lightweight Kubernetes distribution) inside Docker containers. It simplifies the process of creating and managing Kubernetes clusters for development and testing.


🔹 Why Use K3d?

  • Lightweight – Runs K3s inside Docker, making it resource-efficient.

  • Fast Deployment – Quickly spin up and tear down Kubernetes clusters.

  • Multi-Node Support – Supports multi-node clusters with minimal overhead.

  • Easy CI/CD Integration – Works well in CI/CD pipelines due to its fast startup time.

  • Local Development – Perfect for testing Kubernetes workloads locally without needing a full-fledged cluster.


🔹 Installation

1️⃣ Install K3d

Use the following command to install K3d:

curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash

Alternatively, use Homebrew (for macOS/Linux):

brew install k3d

Verify the installation:

k3d version

🔹 Basic Usage

2️⃣ Create a Cluster

k3d cluster create my-cluster
  • This creates a single-node cluster with k3s running inside a Docker container.

To create a multi-node cluster (1 master, 2 workers):

k3d cluster create my-cluster --agents 2

3️⃣ Check the Cluster Status

kubectl cluster-info
kubectl get nodes
  • If kubectl is not installed, K3d will automatically configure kubectl for you.

4️⃣ Deploy an Application

Deploy a simple Nginx pod:

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --type=LoadBalancer --port 80

List services:

kubectl get svc

5️⃣ Delete a Cluster

k3d cluster delete my-cluster

🔹 Advanced Usage

Customizing Cluster Ports

To expose ports for local access, use:

k3d cluster create my-cluster --port "8080:80@loadbalancer"

This maps port 8080 on localhost to port 80 in the cluster.

Using a Custom Config File

Instead of CLI flags, you can define a cluster configuration:

apiVersion: k3d.io/v1alpha4
kind: Simple
metadata:
  name: my-cluster
servers: 1
agents: 2
ports:
  - port: 8080:80
    nodeFilters:
      - loadbalancer

Create the cluster with:

k3d cluster create --config my-cluster.yaml

🔹 Use Cases

Local Kubernetes Development – Test Kubernetes workloads locally without using Minikube or KinD.
CI/CD Pipelines – Run Kubernetes clusters in ephemeral environments for testing.
Multi-Node Cluster Simulation – Useful for testing distributed applications.
Experimenting with Kubernetes Networking & Security – Try out service meshes, network policies, and security configurations.


🔹 Comparison: K3d vs Minikube vs KinD

FeatureK3dMinikubeKinD (Kubernetes in Docker)
InstallationEasy (single binary)Requires hypervisorEasy (Docker-based)
Resource UsageLow (lightweight)HighMedium
Multi-Node SupportYesLimitedYes
CI/CD IntegrationExcellentModerateGood
SpeedFastSlowModerate

Conclusion

K3d is an excellent choice for developers who need a fast, lightweight Kubernetes cluster in Docker. It simplifies Kubernetes testing, integrates well with CI/CD pipelines, and is great for local development.

Would you like me to guide you on a specific use case, such as integrating K3d into CI/CD or setting up specific workloads? 🚀

0
Subscribe to my newsletter

Read articles from Nayan Tank directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nayan Tank
Nayan Tank

Hi there! I'm a DevOps engineer. My expertise lies in deploying, automating, and maintaining infrastructure, as well as optimizing delivery pipelines. I've worked with a range of tools, including AWS, Docker, Kubernetes, and Ansible. In my spare time, I love experimenting with new technologies.