Tuesday 31 May 2022

Create K3D Cluster with Local Docker Registry

 K3D is the lightweight wrapper allowing us to run K3S Ranchers minimal Kubernetes deployment easily in local environments. Developers can quickly create a cluster, test the apps in containers with Kubernetes locally. Advantage is it is really really simple and faster to get a Kubernetes cluster created, and deleted using K3D, giving the opportunity to the developers to play around with Kubernetes as much as they want, without a worry. This is a great way to develop with Kubernetes as if you are using a central development Kubernetes cluster, such as Azure AKS, it takes time for you to get the cluster up and running if you made a mess with it while development. Instead K3D let's you have your own development Kubernetes cluster running locally in your machine, which you can destroy and create faster when ever you want or need.

So, what do you need. You need WSL 2 Running in your windows 10 machine as a pre requisite. To get WSL 2 setup with Ubuntu 20.4 Distro you can use the PowerShell Script here. When you run the script you have to reboot your machine couple of times and rerun the script again to continue the installation.

Once you have your machine ready with WSL 2 up and running, it is required to setup docker and docker compose in it. K3D is running Kubernetes cluster on top of docker as containers. Get kubectl installed as well as it will come handy later on.

Open WSL terminal or open a WSL Terminal with Visual Studio Code. To install K3D client run the command below.

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

Now type k3d in your terminal and you will see all the help you need to get started.


You can use the command below to create your K3D Kubernetes cluster with a local K3D docker registry associated to it.

k3d cluster create mycluster --api-port 127.0.0.1:6443 -p "8081:80@loadbalancer" --agents 2 --registry-create mycluster-k3d-registry

When you execute the command above your cluster will be created and ingress to the cluster will be configured via port 8081 within few seconds. you can use a different port if required. Custer will be created and your kube config file will be updated with the cluster context. You can execute kubectl cluster-info to see the cluster state once it is created.


Running  k3d registry list will give you the registry information.


k3d cluster list will give you the cluster information.


If you list namespaces with kubectl get namespace you will see the namespace list.


Deleting your cluster is as easy as you created it. You just need to run k3d cluster delete mycluster and your cluster will be gone in seconds. As we now have a development machine where we can get a Kubernetes cluster created within seconds and deleted on demand, let's explore what how we can test applications using the cluster in next posts. 

No comments:

Popular Posts