Lightweight K3d Kubernetes cluster
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
2) installed kubectl command-line tool
Install k3d
curl -s https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
Create Dev cluster base config
vim dev-cluster-config.yaml
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
Get k3d cluster list
k3d cluster list
Get Kubernetes config
cat ~/.kube/config
Get cluster info
kubectl cluster-info
Get the cluster nodes
kubectl get nodes
Under the hood of Docker
docker image ls
docker ps --format “table {{.Image}}\t{{.Ports}}\t{{.Names}}”
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
kubectl create service clusterip my-deployment-svc --tcp=80:80
Check Kubernetes objects
kubectl get all
Create Ingress controller
vim ingress.yaml
kubectl apply -f ingress.yaml
Check the connection from host machine
curl -i http://localhost
curl -i -k https://localhost
Check the connection to Kubernetes API
curl -i -k https://localhost:6443
Remove K3d cluster
k3d cluster list
k3d cluster rm dev-cluster
GitHub repository: https://github.com/tiborpetroczy/k3d-cluster