Deploying kubernetes on containers using kind
Tags: kubernetes, testing, kind
Published: 2021-02-05 11:15:30

_Kind is an installation tool used to deploy kubernetes cluster in containers._
_We all need a simple, easy method to deploy kubernetes for testing and practice._

**From the 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.

####List of Contents
* Installing Docker
* Getting kind binary
* Getting kubectl binary
* Deploying a single node cluster 
* Deploying Multi node Cluster
* Deploying a Multi Master
* Accessing Kubernetes Cluster
* Deploying and Accessing multiple clusters

####Let's Get Started! 

#####For using kind you need to install docker

```
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
sudo systemctl start docker && sudo systemctl enable docker
sudo usermod -aG docker $USER
sudo systemctl restart docker
```
> If you are facing an issue while installing docker please refer [Install Docker](https://docs.docker.com/engine/install/)

Log out and Log in once and fire the command `docker ps` if the command is executed successfully docker is ready for use

####Create a Directory
```
mkdir $HOME/bin/
```
#####Add the following parameters to .bashrc at your home directory
```
export PATH=$PATH:$HOME/bin/
```
#####Getting the kind binary
```
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.10.0/kind-linux-amd64
chmod +x kind
mv kind $HOME/bin/kind
```
Log out and Log in once and fire the command below this should provide you with the current version of the command.
```
kind --version 
```
#####Getting kubectl binary
```
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
mv kubectl $HOME/bin/kubectl
```
Log out and Log in once and fire the command below this should provide you with the current version of the command.
```
kubectl version
```
###Creating cluster using kind:

> **Deploying a single node cluster** 
`<your_cluster_name> replace with a name you wish`
```
kind create cluster --name <your_cluster_name>
```
`If you want to access your cluster Refer to Accessing Kubernetes Cluster below`

> **Deploying a multi node cluster**
kind can be provided with a config file where the number nodes and their roles can be mentioned.

######config.yaml

```
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
```
Fire the command below for deploying the cluster
```
kind create cluster --config config.yaml --name <your_cluster_name>
```
> **A Multimaster kubernetes cluster can be deployed by editing the config.yaml file**
```
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker
```
Fire the command below for the deployment of Multimaster Kubernetes cluster
```
kind create cluster --config config.yaml --name <your_cluster_name>
```

####Accessing The Kubernetes cluster

The file `$HOME/.kube/config` is created by default,do
```
kubectl get nodes
```
####Deploying and accessing multiple clusters

You can specify different names for different clusters
```
kind create cluster --config config.yaml --name <your_cluster_name>
```
This clusters can be managed by using the configuration file that can be generated by using the command
`<Cluster_config_filename> replace this parameter with a filename`
```
kind get clusters \\ This will list all the clusters that are running

kind get kubeconfig --name <your_cluster_name> >> <Cluster_config_filename>

export KUBECONFIG=<Cluster_config_filename>
```

Now your kubectl is configured to use the specific cluster

There are various configurations that can be done while deploying a kubernetes cluster 

A sample configuration file is given below 
you can use these configuration to your advantage
```
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
featureGates:
  # any feature gate can be enabled here with "Name": true
  # or disabled here with "Name": false
  "CSIMigration": true
runtimeConfig:
  "api/alpha": "false"
networking:
  # network configuration for nodes 
  ipFamily: ipv6
  apiServerAddress: "127.0.0.1"
  apiServerPort: 6443
  podSubnet: "10.244.0.0/16"
  serviceSubnet: "10.96.0.0/12"
  disableDefaultCNI: true
  kubeProxyMode: "ipvs"
nodes:
# one node hosting a control plane
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "my-label=true"
- role: worker
  extraMounts:
  # with the extraMounts parameter one can attach persistent volume to the node 
  - hostPath: /path/to/my/files/
    containerPath: /files
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    # optional: set the bind address on the host
    # 0.0.0.0 is the current default
    listenAddress: "127.0.0.1"
    # optional: set the protocol to one of TCP, UDP, SCTP.
    # TCP is the default
    protocol: TCP
- role: worker
  kubeadmConfigPatches:
  - |
    kind: JoinConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "my-label2=true"
- role: worker
  image: kindest/node:v1.16.4@sha256:b91a2c2317a000f3a783489dfb755064177dbc3a0b2f4147d50f04825d016f55
```
*The deployment gets ready in much easier and faster way you can save the docker image and copy it to your local machine for further use.*