Lightweight K3d Kubernetes cluster

Tibor Petróczy
3 min readApr 12, 2022

As a developer a completely new Kubernetes environment / cluster is often needed at minimal cost.

From my experience one of the fastest and most convenient solution is to create a k3d cluster from zero.

Prerequisites:
1) installed Docker container runtime on local or virtual machine

Docker version

2) installed kubectl command-line tool

kubectl version

Install k3d

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

Install k3d from source
Check k3d version

Create Dev cluster base config

vim dev-cluster-config.yaml

Base dev cluster config

Configuration aspects:

  • 2 control planes and 2 worker nodes / agents
  • Kubernetes API is available on port 6443 (no other service can run on local machine port 6443)
  • LoadBalancer HTTP endpoint is available on port 80 (no other service can run on local machine port 80)
  • LoadBalancer HTTPS endpoint is available on port 443 (no other service can run on local machine port 443)

Create new k3d cluster with dev-cluster name

k3d cluster create dev-cluster --config dev-cluster-config.yaml

Install k3d with predefined config

Get k3d cluster list

k3d cluster list

k3d cluster list

Get Kubernetes config

cat ~/.kube/config

Kubernetes config

Get cluster info

kubectl cluster-info

Kubernetes cluster info

Get the cluster nodes

kubectl get nodes

Kubernetes nodes

Under the hood of Docker

docker image ls

Downloaded Docker images

docker ps --format “table {{.Image}}\t{{.Ports}}\t{{.Names}}”

Running Docker containers

Notice that you can get 5 containers (loadbalancer, 2 servers / control planes, 2 agents / workers).

Now you can create a demo deployment in your new Kubernetes cluster:

  • Name: my-deployment
  • Image: Nginx latest version
  • Replicas: 3

Create deployment and service

kubectl create deployment my-deployment — image=nginx:latest — replicas=3

Create Kubernetes deployment

kubectl create service clusterip my-deployment-svc --tcp=80:80

Check Kubernetes objects

kubectl get all

Kubernetes objects (deployment, service)

Create Ingress controller

vim ingress.yaml

Ingress definition

kubectl apply -f ingress.yaml

Create Ingress controller
Check the created ingress contorller

Check the connection from host machine

curl -i http://localhost

curl to HTTP

curl -i -k https://localhost

curl to HTTPS

Check the connection to Kubernetes API

curl -i -k https://localhost:6443

curl to API 6443

Remove K3d cluster

k3d cluster list

k3d cluster list

k3d cluster rm dev-cluster

--

--