Set Up Local Kubernetes Cluster with KIND (Single node as well as multi node)

Kind is a tool built for running local Kubernetes clusters using Docker containers as nodes. kind was primarily designed for testing Kubernetes itself, but it is actually quite useful for creating a Kubernetes environment for local development, QA, or CI/CD

A fully functioning environment using kind includes a few different components. For our purposes, we will install the following list of software.

  1. Docker: (To create KINDcontroller and worker node For k8s cluster)
  2. The kubectl tool: (To communicate with K8s Cluster)
  3. kind: (Commnad line tool by which you can create single node or multinode cluster)
  4. A local Docker registry: (To push and pull custom images)
  5. An Ingress controller. (To access deployed application to outside word)

We can use these steps to create a repeatable script to setup a local Kubernetes cluster whenever you need it.

The kind project stands for “Kubernetes in Docker”. As such, you will need to install Docker to get started. This is typically environment specific, and you may need to consult the Docker documentation if you get stuck
Older versions of Docker were called docker,, or docker-engine. If these are installed you should start by getting rid of them:
$ sudo apt-get remove docker docker-engine containerd runc
update the apt repository given the new Docker source, and install Docker.
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli

kind does not strictly require kubectl, but because we are aiming to setup a fully functioning development environment we are going to install kubectl to be able to do some basic Kubernetes functions on our cluster.
$ curl -LO "$(curl -s"
$ chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Now we can finally install kind. Kind publishes binaries to Github. These can be installed through Homebrew on a Mac, or by downloading the release for Linux distributions.
$ curl -Lo ./kind
$ chmod +x ./kind
$ mv ./kind /usr/local/bin

Some Usufull kind commands to create clusters:
Generic Command
$ kind create cluster--image <provide_Kind_image_name> --name <name_of_cluste> — config <custom_config_file>
Some Example to use kind command:
1. To Create single node cluster with default cluster name is kind and default updated kind image:
$ kind create cluster
2. To Create single node cluster with cluster name as developmet and with version of kind image is 1.18.8
$ kind create cluster --image kindest/node:v1.18.8 --name development
3. To ctreate multi node cluster with kind-config.yaml file:
$ kind create cluster --config kind-config.yaml
Create kind-config.yaml and inject following line of code for multi node cluster :

kind: Cluster
— role: control-plane
— role: worker
— role: worker

4. To display kubeconfig of current running cluster:
$ kind get kubeconfig --name <name_of_cluster>
5. To delete cluster :
$ kind delete cluster --name <name_of_cluster>

Create local docker registry:
One of the challenges in using Kubernetes for local development is getting local Docker containers you create during development to your Kubernetes cluster. Configuring this correctly allows Kubernetes to access any Docker images you create locally when deploying your Pods and Services to the cluster you created using kind. There are a few ways to solve this problem, but the one I prefer is to create a local Docker registry that your Kubernetes cluster can access. This version most closely matches a production deployment of Kubernetes.
The following example creates a Docker registry called kind-registry running locally on port 5000.
$ docker run -d — restart=always -p 5000:5000 — name kind-registry registry:2
Now that we have a registry created, we can configure kind to use this registry for pulling container images during deployments. We do this using the configuration below .

kind: ClusterapiVersion: |-[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:5000"]endpoint = ["http://kind-registry:5000"]

We can save this file as local-registry.yaml and then create the cluster using kind:
$ kind create cluster — config local-registry.yaml
The last step we have is to connect the kind cluster’s network with the local Docker registry’s network:
$ docker network connect “kind” “kind-registry”

Configure Ingress Controller:
kubectl apply -f

Putting Everything togather in a script and create single node or multinode cluster:

#!/bin/shset -o errexitreg_name='kind-registry'reg_port='5000'k8s_version='1.18.8'# Start a local Docker registry (unless it already exists)running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"if [ "${running}" != 'true' ]; thendocker run \-d --restart=always -p "${reg_port}:5000" --name "${reg_name}" \registry:2fi# Create a kind cluster# - Configures containerd to use the local Docker registry# - Enables Ingress on ports 80 and 443cat <<EOF | kind create cluster --image kindest/node:v${k8s_version} --config=-kind: ClusterapiVersion: |-[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${reg_port}"]endpoint = ["http://${reg_name}:${reg_port}"]nodes:- role: control-planekubeadmConfigPatches:- |kind: InitConfigurationnodeRegistration:kubeletExtraArgs:node-labels: "ingress-ready=true"extraPortMappings:- containerPort: 80hostPort: 80protocol: TCP- containerPort: 443hostPort: 443protocol: TCPEOF#ADD woker node to cretae multi node cluster . Without worker node it will creates a single node cluster- role: worker# Connect the local Docker registry with the kind networkdocker network connect "kind" "${reg_name}" > /dev/null 2>&1 &# Deploy the nginx Ingress controllerkubectl apply -f

1.Save the script and make it executable and run it local machine.
2. This script will take mode the 5 minutes to set up a cluster.
3. You can view the docker container running through the dokcer ps command : $ docker ps
4. View the three running nodes with kubectl commads: $kubect get nodes 5. We can modify /etc/hosts on the host to direct traffic to the kind cluster’s ingress controller. We’ll need to get the IP address of our kind node’s Docker container first by running:
$ docker container inspect $(docker ps -a | awk ‘{print $NF}’ | grep control-plane) — format ‘{{ .NetworkSettings.Networks.kind.IPAddress }}’ Then add an entry to /etc/hosts with the IP address found that looks like:
Finally, we can curl
6. Kubeconfig File Locaion: ~/.kube/config


All Related Files with Exaples of k8s manifest file:

All Related Files:

The Senior Devops