
Kubernetes in Docker(kind)
Reference: https://kind.sigs.k8s.io/
As per official documentation
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.
Prerequisites
To install kind, you need to meet the following prerequisites
Installation
To download and install kind on Linux based system.
curl -Lo ./kind "https://kind.sigs.k8s.io/dl/v0.9.0/kind-$(uname)-amd64"
chmod +x ./kind
mv ./kind /usr/local/bin/kind
- Once the kind is installed, you can verify its version using.
kind version
kind v0.9.0 go1.15.2 linux/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 is going to pull the docker image(kindest/node:v1.19.1) to setup the cluster for you.
kind create cluster
Creating cluster "kind" ...
โ Ensuring node image (kindest/node:v1.19.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-kind
Thanks for using kind! ๐
- Once this done we have kube config ready for us
ls -l ~/.kube/config
-rw------- 1 plakhera staff 5608 Oct 9 09:23 /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
- You can even verify the docker container it’s running, by executing the below command
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4bf12cd3dc13 kindest/node:v1.21.1 "/usr/local/bin/entrโฆ" 4 days ago Up 2 days 127.0.0.1:49713->6443/tcp my-kind-cluster-control-plane
9db63205451e kindest/node:v1.21.1 "/usr/local/bin/entrโฆ" 4 days ago Up 2 days my-kind-cluster-worker
c92dd6d3c367 kindest/node:v1.21.1 "/usr/local/bin/entrโฆ" 4 days ago Up 2 days my-kind-cluster-worker2
- 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
Kubernetes control plane is running at https://127.0.0.1:41917
KubeDNS is running at https://127.0.0.1:41917/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
- kind has created a one node cluster for you.
kubectl get nodes
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready master 44s v1.19.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"]
kind get clusters No kind clusters found.
- To delete the above cluster
kind delete clusters kind
Deleted clusters: ["kind"]
kind get clusters
No kind clusters found.
- Now the above cluster is created by fetching the latest image of kind from dockerhub, if you want to install the specific version
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:
kubectl cluster-info --context kind-my-kind-cluster
Have a nice day! ๐
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 my-example-config.yaml
Creating cluster "my-kind-cluster" ...
โ Ensuring node image (kindest/node:v1.19.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
Thanks for using kind! ๐
- 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 master 2m17s v1.19.1
my-kind-cluster-worker Ready <none> 101s v1.19.1
my-kind-cluster-worker2 Ready <none> 101s v1.19.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 - role: worker
- Nothing change on the command end and you need to run the same command
kind create cluster --name my-kind-cluster --config my-example-config.yaml Creating cluster "my-kind-cluster" ... โ Ensuring node image (kindest/node:v1.19.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-kind-cluster" You can now use your cluster with:
kubectl cluster-info --context kind-my-kind-cluster
Thanks for using kind! ๐
- Yay your multi-node cluster is up and running
kubectl get nodes
NAME STATUS ROLES AGE VERSION
my-kind-cluster-control-plane Ready master 3m4s v1.19.1
my-kind-cluster-control-plane2 Ready master 2m35s v1.19.1
my-kind-cluster-worker Ready <none> 97s v1.19.1
my-kind-cluster-worker2 Ready <none> 97s v1.19.1
my-kind-cluster-worker3 Ready <none> 97s v1.19.1
- As you can see along with multi-node cluster it’s also setup load balancer for you. You can verify load balancer configuration by
> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
be68932ea592 kindest/haproxy:v20200708-548e36db "/docker-entrypoint.โฆ" 2 minutes ago Up 2 minutes 127.0.0.1:51857->6443/tcp multi-node-cluster-external-load-balancer
> docker exec -it be68932ea592 sh
/ # 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
45 root 0:00 ps -ef
/ # 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 multi-node-cluster-control-plane multi-node-cluster-control-plane:6443 check check-ssl verify none resolvers docker resolve-prefer ipv4
server multi-node-cluster-control-plane2 multi-node-cluster-control-plane2:6443 check check-ssl verify none resolvers docker resolve-prefer ipv4
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.
Tag:devops, kind, kubernetes