Use kind to quickly build local k8s clusters

Posted by linux1880 on Sun, 06 Mar 2022 14:58:42 +0100

What is Kind?

The composition of k8s cluster is complex. If it is deployed manually, it is easy to make mistakes and the time cost is high. The Kind tool introduced in this paper can quickly establish available k8s clusters and reduce the learning threshold for beginners.
Kind is the abbreviation of Kubernetes In Docker. As the name suggests, it seems to mean putting k8s in docker. Yes, the basic principle of kind to create a k8s cluster is to prepare the image of k8s nodes in advance and start the container through docker to simulate k8s nodes, so as to form a complete k8s cluster. It should be noted that the cluster created by kind can only be used for development, learning and testing, not for production environment.

What are the characteristics of kind?

  • It is very fast to create and start k8s clusters with low resource consumption.
  • Support the creation of multi node k8s clusters, including high availability mode.
  • kind supports Linux, macOS and Windows
  • It is one of the k8s cluster installation methods certified by CNCF

How do I install kind?

kind is a binary program. You can download the corresponding version and increase the Execution Authority:

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
chmod +x ./kind
mv ./kind /usr/bin/kind
kind version

How to create a new k8s cluster through kind?

kubectl is a client command tool that interacts with k8s, so you need to install this tool first.
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
Then you can quickly create k8s clusters through one line of command:
root@e5pc-vm-01:~# kind create cluster --name myk8s-01

 

Creating cluster "myk8s-01" ...
 ✓ Ensuring node image (kindest/node:v1.21.1) đŸ–ŧ 
 ✓ Preparing nodes đŸ“Ļ  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ī¸ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-myk8s-01"
You can now use your cluster with:

kubectl cluster-info --context kind-myk8s-01

Have a nice day! 👋

So far, we have got an available k8s cluster:

root@e5pc-vm-01:~# kubectl get nodes
NAME                     STATUS   ROLES                  AGE   VERSION
myk8s-01-control-plane   Ready    control-plane,master   66s   v1.21.1

Inside story of kind creating k8s clusters

First view the additional docker images and containers:
root@e5pc-vm-01:~# docker images
REPOSITORY     TAG       IMAGE ID       CREATED        SIZE
ubuntu         latest    2b4cba85892a   2 days ago     72.8MB
kindest/node   <none>    32b8b755dee8   9 months ago   1.12GB

 

root@e5pc-vm-01:~# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                       NAMES
b4bde05b0190   kindest/node:v1.21.1   "/usr/local/bin/entr..."   12 minutes ago   Up 12 minutes   127.0.0.1:42267->6443/tcp   myk8s-01-control-plane
The creation process is roughly as follows: first obtain the image kindest / node: v1 21.1, and then start the container myk8s-01-control-plane. The container started is the master node of this k8s cluster. Obviously, this cluster has only the master node.
At the same time, the kind create cluster command also writes the connection information of the newly created k8s cluster into the kubectl configuration file of the current user (root), so that kubectl can interact with the cluster. The content of the configuration file is as follows:
root@e5pc-vm-01:~# cat  .kube/config 
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: xxxx
    server: https://127.0.0.1:42267
  name: kind-myk8s-01
contexts:
- context:
    cluster: kind-myk8s-01
    user: kind-myk8s-01
  name: kind-myk8s-01
current-context: kind-myk8s-01
kind: Config
preferences: {}
users:
- name: kind-myk8s-01
  user:
    client-certificate-data: xxxx
    client-key-data: xxxx

The architecture diagram of kind creating k8s cluster is as follows:

Full usage of kind program

  • kind create cluster
    • --Image specifies the name of the node image. The default is kindest/node
    • --Name specifies the name of the cluster to be created
    • --config kind-example-config.yaml
    • --kubeconfig string specifies the file path of the generated kubeconfig. The default is $kubeconfig or $home / kube/config
    • Common: kind create cluster --config=xxxcfg --name=xxxname
  • kind delete cluster xxxx
  • kind get clusters: view all the k8s clusters created by kind
  • kind get kubeconfig --name cluster name [the -- name parameter must be filled in, and the default name is kind]
  • kind completion
  • kind export kubeconfig --name=xxx --kubeconfig=kubeconfigpath
    • Export kubeconfig connection configuration file of k8s cluster in kind

Advanced use of kind

1. Create k8s cluster from configuration file

The cluster creation operation shown above is the simplest single master cluster. Of course, kind can create a more complete cluster. This requires the configuration file mode to create a cluster:

root@e5pc-vm-01:~# cat kindcfg-tccc.biz.yml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker

root@e5pc-vm-01:~# kind create cluster --name myk8s-02 --config  ./kindcfg-tccc.biz.yml

2. Set the new k8s cluster to load the image from the private http repository

The configuration file is as follows:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
  [plugins."io.containerd.grpc.v1.cri".registry.mirrors."harbor.xxxx.top:60666"]
    endpoint = ["http://harbor.xxxx.top:60666"]
nodes:
- role: control-plane
- role: worker

3. Complete configuration file example

Address: https://raw.githubusercontent.com/kubernetes-sigs/kind/main/site/content/docs/user/kind-example-config.yaml

# this config file contains all config fields with comments
# NOTE: this is not a particularly useful config file
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
# patch the generated kubeadm config with some extra settings
kubeadmConfigPatches:
- |
  apiVersion: kubelet.config.k8s.io/v1beta1
  kind: KubeletConfiguration
  evictionHard:
    nodefs.available: "0%"
# patch it further using a JSON 6902 patch
kubeadmConfigPatchesJSON6902:
- group: kubeadm.k8s.io
  version: v1beta2
  kind: ClusterConfiguration
  patch: |
    - op: add
      path: /apiServer/certSANs/-
      value: my-hostname
# 1 control plane node and 3 workers
nodes:
# the control plane node config
- role: control-plane
# the three workers
- role: worker
- role: worker
- role: worker

summary

kind is a very helpful tool for learning and testing k8s clusters. It starts quickly with low resource consumption, and it is also one of the installation tools compatible with CNCF certification. You can rest assured to use it.  

reference material