Six tools to Simplify Kubernetes Journey — Day 1 — Kubernetes in Docker(KinD)

kind is a tool for running local Kubernetes clusters using Docker container “nodes”. kind was primarily designed for testing Kubernetes itself, but may be used for local development or CI.

Kind stands for Kubernetes in Docker. It uses Docker container to run Kubernetes nodes.

  • Supports Multi-node
  • HA Control plane
  • You can build and run Kubernetes from source
  • Boots your Kubernetes cluster in ~30sec(Single Node)

Prerequisites

To install kind, you need to meet the following prerequisites.

  • Docker should be up and running
  • Kubectl kind doesn’t require kubectl, but it’s require to perform cluster operations

Installation

  • To download and install kind please follow the below steps
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
chmod +x ./kind
mv ./kind /usr/local/bin
  • Once the kind is installed, you can verify its version using
kind version
kind v0.11.1 go1.16.4 darwin/amd64

Create your first Kubernetes cluster

  • The easiest way to create your one node Kubernetes cluster is by executing the below command. Behind the scene, kind will pull the docker image(kindest/node:v1.21.1, This image contain everything Kubelet, Kubeadm, docker, systemd, core images such as etcd, coredns, pause, kube-apiserver etc) to setup the cluster for you.

Kind Architecture(Reference: https://www.youtube.com/watch?v=tT-GiZAr6eQ)

kind create cluste
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.21.1) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:
kubectl cluster-info --context kind-kindHave a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#communityr
  • Once this done we have kube config ready for us
ls -l ~/.kube/config
-rw------- 1 plakhera staff 11079 Oct 19 07:19 /Users/plakhera/.kube/config
  • You can verify the image it has downloaded
docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
kindest/node <none> d372b674475a 4 months ago 1.1GB
  • To verify the docker container created by kind
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ee9b29f5a52 kindest/node:v1.21.1 "/usr/local/bin/entr…" 5 minutes ago Up 4 minutes 127.0.0.1:63254->6443/tcp kind-control-plane
  • By default, kind will switch to the context of the cluster you have created, but if you want to switch to the particular context.
kubectl config use-context kind-kind
  • kind has created a one node cluster for you.
kubectl get nodes
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready control-plane,master 7m v1.21.1
  • In case if you don’t pass — name argument, kind will create a cluster with default name set to kind.
  • To verify the cluster created by kind
kind get clusters        
kind
  • To delete the above cluster
kind delete clusters kind                   
Deleted clusters: ["kind"
  • Now the above cluster is created by fetching the latest image of kind from dockerhub; if you want to install the specific version, give your cluster some specific name pass — name and — image flag.
kind create cluster --name my-kind-cluster --image kindest/node:v1.19.7
Creating cluster "my-kind-cluster" ...
✓ Ensuring node image (kindest/node:v1.19.7) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-my-kind-cluster"
You can now use your cluster with:
  • To list all the supported version
  • We have created a one-node cluster(both control-plane(master) and worker in the same node). A multi-node cluster can be created by passing a simple configuration file.
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
  • The above configuration file will create one control-plane and two worker nodes.
  • To create a cluster using the above configuration, please pass — config to the configuration.
kind create cluster --name my-kind-cluster --config  single-node-master.yaml
Creating cluster "my-kind-cluster" ...
✓ Ensuring node image (kindest/node:v1.21.1) 🖼
✓ Preparing nodes 📦 📦 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining worker nodes 🚜
Set kubectl context to "kind-my-kind-cluster"
You can now use your cluster with:
kubectl cluster-info --context kind-my-kind-cluster
Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂
  • As you can see your three node cluster is up and running
kubectl get nodes
NAME STATUS ROLES AGE VERSION
my-kind-cluster-control-plane Ready control-plane,master 60s v1.21.1
my-kind-cluster-worker Ready <none> 32s v1.21.1
my-kind-cluster-worker2 Ready <none> 32s v1.21.1
  • My favorite feature is that if you want to create a multi-node cluster with multiple control planes (master) and worker nodes, it is easily doable via bind. You need to modify the definition by adding more than one control-plane to the definition.
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: control-plane
- role: worker
- role: worker
  • Nothing change on the command end and you need to run the same command
kind create cluster --name my-multinode-kind-cluster --config multi-node-master.yaml
Creating cluster "my-multinode-kind-cluster" ...
✓ Ensuring node image (kindest/node:v1.21.1) 🖼
✓ Preparing nodes 📦 📦 📦 📦
✓ Configuring the external load balancer ⚖️
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
✓ Joining more control-plane nodes 🎮
✓ Joining worker nodes 🚜
Set kubectl context to "kind-my-multinode-kind-cluster"
You can now use your cluster with:
kubectl cluster-info --context kind-my-multinode-kind-cluster
Thanks for using kind! 😊
  • As you can see along with multi-node cluster it’s also setup load balancer for you.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6947e2baaef9 kindest/haproxy:v20200708-548e36db "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 127.0.0.1:64593->6443/tcp my-multinode-kind-cluster-external-load-balancer
  • You can verify load balancer configuration by logging into the container
docker exec -it  6947e2baaef9 sh
  • Verify the running processes
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 haproxy -sf 7 -W -db -f /usr/local/etc/haproxy/haproxy.cfg
33 root 0:00 haproxy -sf 7 -W -db -f /usr/local/etc/haproxy/haproxy.cfg
37 root 0:00 sh
46 root 0:00 ps -ef
  • Check the configuration file
/ # cat /usr/local/etc/haproxy/haproxy.cfg
# generated by kind
global
log /dev/log local0
log /dev/log local1 notice
daemon
resolvers docker
nameserver dns 127.0.0.11:53
defaults
log global
mode tcp
option dontlognull
# TODO: tune these
timeout connect 5000
timeout client 50000
timeout server 50000
# allow to boot despite dns don't resolve backends
default-server init-addr none
frontend control-plane
bind *:6443
default_backend kube-apiservers
backend kube-apiservers
option httpchk GET /healthz
# TODO: we should be verifying (!)
server my-multinode-kind-cluster-control-plane my-multinode-kind-cluster-control-plane:6443 check check-ssl verify none resolvers docker resolve-prefer ipv4
server my-multinode-kind-cluster-control-plane2 my-multinode-kind-cluster-control-plane2:6443 check check-ssl verify none resolvers docker resolve-prefer ipv4

Other Alternatives

I plan to create a separate blog mentioning the pros and cons of using minikube vs K3s vs kind.

Wrapping Up

Kind is super powerful, and in this blog, I barely scratch the surface. If you don’t want to spend hours building Kubernetes cluster or $$$ in building cluster in the cloud, kind is a tool for you.

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP