This page looks best with JavaScript enabled

A Practical Guide to kind

 ·  ☕ 3 min read

1. Project Overview

kind is a tool for managing Kubernetes clusters using containers. Project address https://github.com/kubernetes-sigs/kind .

It is mainly used for:

  • Local development environments
  • Temporary environments for learning
  • Automated testing

2. Installing kind

  • macOS
1
brew install kind
  • Linux
1
2
curl -Lo /usr/local/bin/kind https://kind.sigs.k8s.io/dl/v0.21.0/kind-linux-amd64
chmod +x /usr/local/bin/kind

3. Creating a kind Cluster

If you have a PROXY configured locally, it is recommended to reset the environment variables before creating:

1
2
export https_proxy=http://x.x.x.x:7890
export http_proxy=http://x.x.x.x:7890

The local proxy is usually set to http://127.0.0.1:7890, but kind cannot reach it, so it needs to be changed to the current host’s IP address http://x.x.x.x:7890 . Otherwise, when deploying applications, you will get an error saying the image cannot be pulled.

3.1 Single Node

  • Create a single-node cluster
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
kind create cluster --image=kindest/node:v1.23.6 --name=dev

Creating cluster "dev" ...
 ✓ Ensuring node image (kindest/node:v1.23.6) 🖼
 ✓ Preparing nodes 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
Set kubectl context to "kind-dev"
You can now use your cluster with:

kubectl cluster-info --context kind-dev

Have a nice day! 👋
  • View the cluster
1
2
3
4
kubectl get node

NAME                STATUS   ROLES                  AGE   VERSION
dev-control-plane   Ready    control-plane,master   71s   v1.23.6

3.2 Multiple Nodes

  • Edit the config file dev-multi-node.yaml
1
2
3
4
5
6
7
8
9
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    image: kindest/node:v1.23.6
  - role: worker
    image: kindest/node:v1.23.6
  - role: worker
    image: kindest/node:v1.23.6
  • Create a multi-node cluster
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
kind create cluster --config dev-multi-node.yaml --name=dev-multi-node

Creating cluster "dev-multi-node" ...
 ✓ Ensuring node image (kindest/node:v1.23.6) 🖼
 ✓ Preparing nodes 📦 📦 📦
 ✓ Writing configuration 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Set kubectl context to "kind-dev-multi-node"
You can now use your cluster with:

kubectl cluster-info --context kind-dev-multi-node

Thanks for using kind! 😊
  • View the cluster
1
2
3
4
5
6
kubectl get node

NAME                           STATUS   ROLES                  AGE     VERSION
dev-multi-node-control-plane   Ready    control-plane,master   2m45s   v1.23.6
dev-multi-node-worker          Ready    <none>                 2m23s   v1.23.6
dev-multi-node-worker2         Ready    <none>                 2m23s   v1.23.6

4. kind Cluster Lifecycle Management

  • View clusters
1
2
3
4
kind get clusters

dev
dev-multi-node
  • Switch clusters

Note the kind-{cluster name} format of the context here.

1
kubectl cluster-info --context kind-dev
  • Delete a cluster
1
kind delete cluster --name dev-multi-node

5. Mapping Ports to the Host

1
2
3
4
5
6
7
8
9
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
  - role: control-plane
    extraPortMappings:
      - containerPort: 30000
        hostPort: 8000
        listenAddress: "0.0.0.0"
        protocol: tcp

When creating a kind cluster, add the extraPortMappings parameter to map container ports to host ports. Here the kind cluster’s port 30000 is mapped to port 8000 on the local machine.

6. Loading Images into the Cluster

1
kind load docker-image test:v1 --name dev

Load a locally built image into the kind cluster.

7. Configuring the Network

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  ipFamily: ipv4
  apiServerPort: -1
  apiServerAddress: 127.0.0.1
  podSubnet: "10.244.0.0/16"
  serviceSubnet: "10.96.0.0/16"
  disableDefaultCNI: false
  kubeProxyMode: "iptables"
  dnsSearch:
    - cluster.local

8. Configuring the Runtime

1
2
3
4
5
6
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
  - |-
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry-1.docker.io"]
      endpoint = ["https://docker.nju.edu.cn"]    

Here a domestic image registry mirror is configured for containerd.

9. Enabling FeatureGates

1
2
3
4
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
featureGates:
  "AdmissionWebhookMatchConditions": true

Refer to https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/ to enable FeatureGates.

10 Mounting Host Directories into the Cluster

1
2
3
4
5
6
7
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraMounts:
    - hostPath: /Users/shaowenchen/kind-host
        containerPath: /host

Mount the host directory /Users/shaowenchen/kind-host to /host on the specified node of the kind cluster.

11. References


微信公众号
WRITTEN BY
微信公众号