k3d is a lightweight wrapper to run k3s (minimal Kubernetes distribution from Rancher Labs) in Docker.
By using k3d, we can create single and multi node k3s clusters in Docker for local development purpose.
In this blog, we are going to use Ubuntu 20.04 LTS for setting up k3d.
Install Docker using the convenience script
$ curl -fsSL https://get.docker.com -o get-docker.sh$ sudo sh get-docker.sh
Add your user to the docker group to execute commands as a non-root user and log out from the current session
$ sudo usermod -aG docker $USER
Log back in and verify the Docker commands as a non-root user
$ docker info$ docker container run hello-world
Download and install k3d
$ curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash$ k3d version
Download and install kubectl
$ curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"$ sudo install kubectl /usr/local/bin/kubectl$ kubectl version --client
Create the cluster using the below configuration file
$ cat config.yml
$ k3d cluster create --config config.yml
Check the cluster details and nodes
$ kubectl cluster-info$ kubectl get nodes
Create a sample index.html file
$ echo “Hello from Pod!” > index.html
Build a custom Docker image using the below Dockerfile
$ cat DockerfileFROM nginx:1.23COPY index.html /usr/share/nginx/html$ docker image build -t app:v1 .
Tag the custom built image and push to the container registry created by k3d
$ docker image tag app:v1 localhost:5000/app:v1$ docker image push localhost:5000/app:v1
Deploy a pod using the custom built image
$ cat app-pod.yml
$ kubectl create -f app-pod.yml$ kubectl get pods
Expose the pod to a node port service
$ cat app-nodeport.yml
$ kubectl create -f app-nodeport.yml$ kubectl get svc
Verify the webpage using browser or curl
$ curl http://localhost:30000