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.
Installing
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:
language: go
go:
- '1.15.x'
services:
- docker
jobs:
include:
- stage: 'Kind example'
before_script:
- curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
- GO111MODULE="on" go get sigs.k8s.io/[email protected]
- kind create cluster
- kubectl config use-context kind-kind
- kubectl create sa default
script:
- kubectl run busybox --image=busybox
- kubectl get pods
All is working like a charm:

Conclusion
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.
Resources
