Onur Yasarlar
Search…
How to Install and Use K3D

Getting Started

I have discovered K3D a bit late but it is one of the best tools for local Kubernetes development It does not run your Kubernetes cluster inside a virtual machine just like Minikube does. It uses Docker and allows you to create multi-node clusters. You may want to check their official page for more information.
I created a sample project to install and configure K3D for my local Kubernetes development and also added some sample deployments with ingress service.

Prerequisites

Docker and Helm need to be installed in your local development environment. Please do not proceed with the installation before installing Docker and Helm.

Installation

I followed the official documentation to installK3Don my local development environment but made a few changes. I skipped the deployment of Traefik because K3D install V1 and I wanted to install the latest version of Traefik. You can also prefer another ingress controller like nginx.
You can install the environment by running a single script - deploy-cluster.sh and the script itself is quite self-explanatory:
deploy-cluster.sh
1
# VARIABLES
2
CLUSTER_NAME="mycluster"
3
SERVER_COUNT=1
4
NODE_COUNT=3
5
TRAEFIK_LABEL="app.kubernetes.io/instance=traefik"
6
7
# Create Cluster
8
k3d cluster create ${CLUSTER_NAME} --api-port 127.0.0.1:6443 \
11
--k3s-server-arg "--no-deploy=traefik" --agents ${NODE_COUNT} --servers ${SERVER_COUNT}
12
13
# Install Traefik V2
14
helm repo add traefik https://containous.github.io/traefik-helm-chart
15
helm install traefik traefik/traefik
16
17
# Wait until Traefik deployment is finished
18
while [[ $(kubectl get pods -l "${TRAEFIK_LABEL}" -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]
19
do
20
sleep 1
21
done
22
23
# Expose Traefik Dashboard
24
kubectl port-forward $(kubectl get pods --selector "app.kubernetes.io/name=traefik" --output=name) 9000:9000 &
25
echo "You can reach Traefik dashbaord by: http://localhost:9000/dashboard/"
26
27
# Set cluster context to new cluster
28
kubectl config use-context k3d-${CLUSTER_NAME}
29
30
# Show cluster info
31
kubectl cluster-info
Copied!
You may want to change the global variables for your own environment. Once the script is running you can check the Traefik Dashboard by visiting http://localhost:9000/dashboard/#/.

Example Deployment

You can check one of the example deployments in the main project directory and apply it via kubectl apply -f <sample-directory> command. If you do not edit anything in the YAML definitions, it will create a host-based ingress rule for the deployment. If you check the Traefik HTTP services, you will find your rule as below:
Please note that if you want to use the host-based ingress rule, you will have to add the FQDN to your /etc/hosts file.
Example deployment output will look like this:
1
$ kubectl get svc,ingress,deployment vault
2
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
3
service/vault ClusterIP 10.43.182.154 <none> 8200/TCP 65m
4
5
NAME CLASS HOSTS ADDRESS PORTS AGE
6
ingress.networking.k8s.io/vault <none> vault.onur.lab 80 65m
7
8
NAME READY UP-TO-DATE AVAILABLE AGE
9
deployment.apps/vault 1/1 1 1 65m
10
Copied!