Quickly set up local k8s cluster using kind

Posted by proctk on Sun, 06 Mar 2022 18:24:17 +0100

Python WeChat Subscription Applet Course Video


Python Actual Quantitative Transaction Finance System


What is Kind?

The composition of the k8s cluster is complex, error-prone and time-consuming if deployed manually. The Kind tool described in this paper can quickly set up an available k8s cluster and reduce the learning threshold for beginners.
Kind is an abbreviation for Kubernetes In Docker. As the name implies, it seems to mean putting k8s in docker. Yes, the basic principle of kind's k8s cluster creation is: prepare a mirror of the k8s node in advance, start the container through docker to simulate the k8s node, thus forming a complete k8s cluster. It is important to note that kind creates clusters that can only be used for development, learning, testing, and so on, not for production environments.

What are the characteristics of kind?

  • Creating and starting k8s clusters is fast and consumes less resources.
  • Supports the creation of multinode k8s clusters, including highly available modes.
  • kind supports Linux, macOS and Windows
  • It is one of the CNCF certified k8s cluster installations

How do I install type?

kind is represented as a binary program that downloads the corresponding version and increases execute privileges:

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 do I create a new k8s cluster with kind?

kubectl is a client command tool that interacts with k8s, so it needs to be installed 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 a k8s cluster with one line of commands:

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 you have 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

kind creates the inside of the k8s cluster

First look at the additional docker images and containers:

root@e5pc-vm-01:~# docker images
ubuntu         latest    2b4cba85892a   2 days ago     72.8MB
kindest/node    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>6443/tcp   myk8s-01-control-plane

The creation process is probably as follows: Get the mirror kindest/node:v1 first. 21.1, and then start the container myk8s-01-control-plane, which is the master node of this k8s cluster, apparently only the master node.
The kind create cluster command also writes the connection information for this newly created k8s cluster to the kubectl configuration file of the current user (root) so that kubectl can interact with the cluster. The configuration file is as follows:

root@e5pc-vm-01:~# cat .kube/config 
apiVersion: v1
- cluster:
    certificate-authority-data: xxxx
  name: kind-myk8s-01
- context:
    cluster: kind-myk8s-01
    user: kind-myk8s-01
  name: kind-myk8s-01
current-context: kind-myk8s-01
kind: Config
preferences: {}
- name: kind-myk8s-01
    client-certificate-data: xxxx
    client-key-data: xxxx

kind creates the schematic diagram of the k8s cluster as follows:

Complete usage of kind program

  • kind create cluster
  • - image specifies node mirror name, default is kindest/node
  • - name Specifies the name of the cluster created
  • –config kind-example-config.yaml
  • - kubeconfig string specifies the file path of the generated kubeconfig. Default at $KUBECONFIG or $HOME/.kube/config
  • Common: kind create cluster --config=xxxcfg --name=xxxname
  • kind delete cluster xxxx
  • kind get clusters: see kind create all k8s clusters
  • kind get kubeconfig --name cluster name [must fill in the - name parameter, default name is kind]
  • kind completion
  • kind export kubeconfig --name=xxx --kubeconfig=kubeconfigpath
  • Export the kubeconfig connection profile for the k8s cluster in kind

Advanced usage of kind

1. Create k8s cluster from configuration file

The cluster creation shown earlier is the simplest single master cluster, and kind can certainly create a more complete cluster. This requires the use of profile mode to create clusters:

root@e5pc-vm-01:~# cat kindcfg-tccc.biz.yml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
- 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 up a new k8s cluster to load mirrors from a private http repository

The configuration file is as follows:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
- |-
    endpoint = ["http://harbor.xxxx.top:60666"]
- 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
- |
 apiVersion: kubelet.config.k8s.io/v1beta1
 kind: KubeletConfiguration
 nodefs.available: "0%"
# patch it further using a JSON 6902 patch
- group: kubeadm.k8s.io
  version: v1beta2
  kind: ClusterConfiguration
  patch: |
 - op: add
 path: /apiServer/certSANs/-
 value: my-hostname
# 1 control plane node and 3 workers
# the control plane node config
- role: control-plane
# the three workers
- role: worker
- role: worker
- role: worker


kind is a very helpful tool for learning and testing k8s clusters. It starts quickly, consumes less resources, and is one of the CNCF certified compatible installation tools that you can use with confidence.

Reference material

Topics: Machine Learning AI computer