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 non-root user and log out from the current session
$ sudo usermod -aG docker $USER
Log back in and verify the Docker commands as 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
apiVersion: k3d.io/v1alpha4
kind: Simple
metadata:
name: dev
servers: 1
agents: 2
image: rancher/k3s:v1.25.2-k3s1
ports:
- port: 30000-30100:30000-30100
nodeFilters:
- server:*
registries:
create:
name: dev
host: 0.0.0.0
hostPort: "5000"
options:
k3s:
extraArgs:
- arg: --disable=traefik
nodeFilters:
- server:*
$ 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 Dockerfile
FROM nginx:1.23
COPY index.html /usr/local/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
apiVersion: v1
kind: Pod
metadata:
labels:
run: app
name: app
spec:
containers:
- image: dev:5000/app:v1
name: app
ports:
- containerPort: 80
$ kubectl create -f app-pod.yml
$ kubectl get pods
Expose the pod to a node port service
$ cat app-nodeport.yml
apiVersion: v1
kind: Service
metadata:
labels:
run: app
name: app-nodeport
spec:
ports:
- port: 80
targetPort: 80
nodePort: 30000
selector:
run: app
type: NodePort
$ kubectl create -f app-nodeport.yml
$ kubectl get svc
Verify the webpage using browser or curl
$ curl http://localhost:30000
Top comments (8)
Hi, nice post : would mind also add (k)native install to your post ? To achieve serverless deployments ?
Thank you! I will check about (k)native.
Great !
Nice and simple!
Small typo :
kubectl create -f app.yml
should be
kubectl create -f app-pod.yml
Thank you, corrected the typo.
Great 👍 one
Thank you!
Amazing post. Thank you.