Kind is a project I like very much and have been participating in. I plan to write a series of articles related to Kind. This is the second one.

At the same time, this article can also be used as a supplement to the fourth section of “Kubernetes from hand to practice”

background

Kind is short for Kubernetes In Docker. As the name implies, it is a tool that uses Docker container as Node and deploys Kubernetes to it. Many cloud native infrastructure projects, including Kubernetes itself, now use Kind for their OWN E2E tests or introductory examples of projects.

By default, Kind is used to create Kubernetes cluster. You only need to install Kind and execute Kind Create Cluster. Kind will automatically download the required Docker image and start the cluster.

However, in some cases, we will also need to start the Kubernetes cluster in an offline environment. In this article I will show you two ways to create Kubernetes clusters using Kind in an offline environment.

Use pre-built images

With each release, Kind builds and publishes the default image, which is currently hosted on Docker Hub. It is recommended that you use images that specify shasum in each ReleaseNote.

When you want to create a cluster using Kind’s pre-built image in an offline environment, you can download the image in advance on any internet-connected machine or target machine, and copy it to the target machine where you want to create the cluster.

If you already have Docker installed on your machine, you can use the Docker pull command to download the image:

(MoeLove) ➜ ~ docker pull kindest/node: v1.17.0 @ sha256:9512 edae126da271b66b990b6fff768fbb7cd786c7d39e86bdf55906352fdf62
sha256:9512edae126da271b66b990b6fff768fbb7cd786c7d39e86bdf55906352fdf62: Pulling from kindest/node
cc5a81c29aab: Pull complete 
81c62728355f: Pull complete 
ed9cffdd962a: Pull complete 
6a46f000fce2: Pull complete 
6bd890da28be: Pull complete 
0d88bd219ffe: Pull complete 
af5240f230f0: Pull complete 
Digest: sha256:9512edae126da271b66b990b6fff768fbb7cd786c7d39e86bdf55906352fdf62
Status: Downloaded newer image for kindest/node@sha256:9512edae126da271b66b990b6fff768fbb7cd786 c7d39e86bdf55906352fdf62 docker. IO/kindest/node: v1.17.0 @ sha256:9512 edae126da271b66b990b6fff768fbb7cd786c7d39e86bdf55906352fdf62
Copy the code

Next, you can use the Docker save command to save the image as a tar:

(MoeLove) ➜ ~ docker save - o kind. V1.17.0. Tar kindest/node: v1.17.0 @ sha256:9512 edae126da271b66b990b6fff768fbb7cd786c7d39e86bdf55906352fdf62
Copy the code

If you think the image is too large and you already have Gzip installed, you can use the following command to save the image as a gzip compressed package.

(MoeLove) ➜ ~ docker save kindest/node: v1.17.0 @ sha256:9512 edae126da271b66b990b6fff768fbb7cd786 c7d39e86bdf55906352fdf62 | gzip > kind. V1.17.0. Tar. GzCopy the code

On the target machine, you can use the docker load command to load the image into the Docker daemon’s store:

(MoeLove) ➜ ~ docker load - I kind. V1.17.0. Tar the Loaded image ID: sha256: ec6ab22d89efc045f4da4fc862f6a13c64c0670fa7656fbecdec5307380f9cb0
# or➜ ~ docker load -i kind. V1.17.0. Tar. Gz the Loaded image ID: sha256: ec6ab22d89efc045f4da4fc862f6a13c64c0670fa7656fbecdec5307380f9cb0
Copy the code

At this point, you can use the following command to create a cluster using an imported image:

(MoeLove) ➜ ~ kind the create cluster - image kindest/node: v1.17.0 @ sha256:9512 edae126da271b66b990b6fff768fbb7cd786c7d39e86bdf55906352fdf62
Creating cluster "kind". ✓ Ensuring node image (Kindest /node:v1.17.0) 🖼 Preparing nodes 📦 ✓ Writing configuration 📜 ✓ Starting control-plane 🕹️ ✓ Installing CNI 🔌 ✓ Installing StorageClass 💾 Set kubectl context to"kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/# community 🙂
Copy the code

PS: In the process of downloading Docker images, if you don’t have Docker installed, you can also choose to use a tool like Skopeo.

Build your own image

If you are developing for Kubernetes, or the Kind maintenance team has not released a pre-built image for a particular version of Kubernetes, you can build your own image.

It is important to note that the network and some disk space are required to build the image. You can use the method described in the previous section offline after you have built the image.

Building a Base Image

You need to have the kind source code ready in advance

(MoeLove) ➜  ~ mkdir -p $GOPATH/ SRC/sigs. K8s. IO (MoeLove) ➜ ~cd $GOPATH/ SRC /sigs.k8s. IO (MoeLove) ➜ ~ gitclone https://github.com/kubernetes-sigs/kind
Copy the code

Now you can use the following command to build the base image.

(MoeLove) ➜  ~ kind build base-image --image kindest/base:latest --source $GOPATH/src/sigs.k8s.io/kind/images/base
Building base image in: /tmp/kind-base-image147474678 Starting Docker build ... . Successfully tagged kindest/base:latest Docker build completed.Copy the code

After building a Base image, you need to build a Node image based on this image.

Building a Node Image

To use this method, you need to prepare the Kubernetes source code in advance. You can refer to the Kubernetes project instructions to download the Kubernetes source code, or copy the source code to the corresponding location on the target machine.

(MoeLove) ➜  ~ mkdir -p $GOPATH/ SRC/k8s. IO (MoeLove) ➜ ~cd $GOPATH/ SRC /k8s. IO (MoeLove) ➜ ~ gitclone https://github.com/kubernetes/kubernetes
Copy the code

You can then use the following command to build the Node image

(MoeLove) ➜  ~ kind build node-image --base-image kindest/base:latest --image kindest/node:latest --kube-root $GOPATH/src/k8s.io/kubernetes Starting to build Kubernetes +++ [0205 11:21:38] Verifying Prerequisites.... . Image build completed.Copy the code

Once the image is successfully built, you can use the image to create the cluster.

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

kubectl cluster-info --context kind-kind

Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/# community 🙂
Copy the code

Check cluster node information

(MoeLove) ➜ ~ kubectl get node NAME STATUS ROLES AGE VERSION kind-Control-plane Ready master 40s B294079458a72 v1.18.0 - alpha. 0.3222 + 4Copy the code

The cluster version is the current version of kubernetes source code.

conclusion

This article introduced two types of ways to create Kubernetes clusters using Kind: using Kind pre-published images and building images using Kubernetes source code.

For both methods, the images contain all the resources needed to create a Kubernetes cluster. No additional downloads are required. (This is the biggest difference from the previous version)

series

  • Build your local Kubernetes cluster with Kind (version 0.2.0, partially out of date)

Recommended reading

  • How to Test a Kubernetes PR with KIND

You can subscribe to my official account [MoeLove] via the following QR code.