DEV Community

Cover image for Building a local Kubernetes cluster using k3d
Unni P
Unni P

Posted on • Edited on • Originally published at Medium

Building a local Kubernetes cluster using k3d

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Log back in and verify the Docker commands as non-root user

$ docker info

$ docker container run hello-world
Enter fullscreen mode Exit fullscreen mode

Download and install k3d

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

$ k3d version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Create the cluster using the below configuration file

$ cat config.yml
Enter fullscreen mode Exit fullscreen mode
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:*
Enter fullscreen mode Exit fullscreen mode
$ k3d cluster create --config config.yml
Enter fullscreen mode Exit fullscreen mode

Check the cluster details and nodes

$ kubectl cluster-info

$ kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Create a sample index.html file

$ echo "Hello from Pod!" > index.html
Enter fullscreen mode Exit fullscreen mode

Build a custom Docker image using the below Dockerfile

$ cat Dockerfile
Enter fullscreen mode Exit fullscreen mode
FROM nginx:1.23
COPY index.html /usr/local/nginx/html
Enter fullscreen mode Exit fullscreen mode
$ docker image build -t app:v1 .
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Deploy a pod using the custom built image

$ cat app-pod.yml
Enter fullscreen mode Exit fullscreen mode
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: app
  name: app
spec:
  containers:
  - image: dev:5000/app:v1
    name: app
    ports:
    - containerPort: 80
Enter fullscreen mode Exit fullscreen mode
$ kubectl create -f app-pod.yml
Enter fullscreen mode Exit fullscreen mode
$ kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Expose the pod to a node port service

$ cat app-nodeport.yml
Enter fullscreen mode Exit fullscreen mode
apiVersion: v1
kind: Service
metadata:
  labels:
    run: app
  name: app-nodeport
spec:
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30000
  selector:
    run: app
  type: NodePort
Enter fullscreen mode Exit fullscreen mode
$ kubectl create -f app-nodeport.yml
Enter fullscreen mode Exit fullscreen mode
$ kubectl get svc
Enter fullscreen mode Exit fullscreen mode

Verify the webpage using browser or curl

$ curl http://localhost:30000
Enter fullscreen mode Exit fullscreen mode

Top comments (8)

Collapse
 
michelsylvestre profile image
Michel Sylvestre

Nice and simple!
Small typo :
kubectl create -f app.yml

should be

kubectl create -f app-pod.yml

Collapse
 
iamunnip profile image
Unni P

Thank you, corrected the typo.

Collapse
 
adriens profile image
adriens

Hi, nice post : would mind also add (k)native install to your post ? To achieve serverless deployments ?

Collapse
 
iamunnip profile image
Unni P

Thank you! I will check about (k)native.

Collapse
 
adriens profile image
adriens

Great !

Collapse
 
tanmaygi profile image
Tanmay Shukla

Great 👍 one

Collapse
 
iamunnip profile image
Unni P

Thank you!

Collapse
 
didof profile image
Francesco Di Donato

Amazing post. Thank you.