Want to deploy Kubernetes clusters on the fly? Yes, it is possible to run Kubernetes directly under Docker!
To deploy Kubernetes locally, the solutions until now were to install Docker Desktop or minikube. We will see here how to deploy Kubernetes in Docker using a great tool called kind. We'll see how to install it on your laptop and push a little bit on the CI side using Travis CI.
For the CI, let's take the following scenario: you have a YAML manifest of an application that you want to test on different versions of Kubernetes. It's going to be complicated to have a real cluster for each version with breaking changes.
Why not Minikube?
Minikube was the initial way to deploy a Kubernetes cluster locally for testing purposes. It is possible to configure a master and multiple nodes using virtual machines. Each VM embeds a runtime container:
A VM takes longer to start. This is all the more important in a CI context where several jobs have to run in a chain. The shorter the execution time of the job the better! Finally, what if we replace the virtualization layer with a containerization layer?
Kubernetes In Docker (Kind)
“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.” from https://kind.sigs.k8s.io/
Kind is a CLI that interacts with a docker daemon. It creates a "node" container creating the necessary components for Kubernetes like etcd, kubelet, addons:
In this part, we’re going to see how to install
kind in local and then create a cluster. Then we’ll export the recipe to Travis CI configuration.
First of all install golang :
$ brew install golang $ go version go version go1.16 darwin/amd64
Then kind is super easy to install:
$ GO111MODULE="on" go get sigs.k8s.io/[email protected]
Creating a cluster with Kind
$ kind create cluster Creating cluster "kind" ... ✓ Ensuring node image (kindest/node:v1.20.2) 🖼 ✓ 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 Thanks for using kind! 😊 $ kubectl cluster-info --context kind-kind Kubernetes control plane is running at https://127.0.0.1:50968 KubeDNS is running at https://127.0.0.1:50968/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. $ kubectl run busybox --context kind-kind --image=busybox pod/busybox created $ kubectl get pods NAME READY STATUS RESTARTS AGE busybox 0/1 Completed 0 7s
Use Kubernetes In a CI Pipeline (Travis CI)
The configuration reuses what we have seen before:
All is working like a charm:
Kind offers a significantly faster startup speed compared to Minikube. It is very straightforward to install and create Kubernetes clusters. For all these points, it is a good fit for CI practices. You can also use it to replace the Kubernetes feature of Docker for Desktop if you are looking for a minimal solution.