Easy Kubernetes CI Integration Using Docker

Easy Kubernetes CI Integration Using Docker

Discover Kubernetes IN Docker and create use it in Travis CI project

Guillaume Vincent
Guillaume Vincent

In this article, we’ll have a look at how to deploy a Kubernetes cluster from scratch for a testing strategy. When developing Kubernetes-dependent features as Helm charts for example you need a cluster to test them. We don’t always have a reserved cluster for CI or maybe you want to have isolated tests to avoid side effects.

At the beginning of Kubernetes popularity raise, an easy way to play and learn was to deploy a cluster using Minikube. We’ll see why it is not the most suitable solution for pipelines compared to currently available solutions. Now it is possible to deploy Kubernetes in Docker. We’ll see how to do it locally and then move the recipe to a Travis CI configuration.

Why is Minikube no longer the best choice for CI purposes?

Historically Minikube was the first and easier solution to introduce himself to Kubernetes. It installs a cluster with a single node on your local machine using a virtual machine :

Minikube Architecture Diagram
Minikube Architecture Diagram

A virtual machine takes more spawn time in a pipeline than using a container. This can decrease the user experience and productivity. What could be more frustrating to wait on several attempts to wait for a job to go green? The shorter the job execution time is the less there are concurrent workloads. In the next section, we’ll see how to run a cluster inside Docker to keep a scalable and fast system.

Kubernetes In Docker

Kind is a Golang package and command-line too which means “Kubernetes IN Docker” :

“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 design diagram from https://kind.sigs.k8s.io/docs/design/initial/
Kind design diagram from https://kind.sigs.k8s.io/docs/design/initial/

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.

Installation on local

Install go 1.11+ firstly :

$ brew install golang
$ go version                                                                                                                                                                                                                     
go version go1.16 darwin/amd64

It is very simple to install :

$ GO111MODULE="on" go get sigs.k8s.io/[email protected]                                                                                                                                                                             
go: downloading sigs.k8s.io/kind v0.10.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/spf13/cobra v1.0.0
go: downloading github.com/pkg/errors v0.9.1
go: downloading k8s.io/apimachinery v0.19.2
go: downloading github.com/alessio/shellescape v1.2.2
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading github.com/inconshreveable/mousetrap v1.0.0
go: downloading golang.org/x/sys v0.0.0-20200928205150-006507a75852
go: downloading gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
go: downloading sigs.k8s.io/yaml v1.2.0
go: downloading github.com/BurntSushi/toml v0.3.1
go: downloading github.com/evanphx/json-patch v4.9.0+incompatible
go: downloading github.com/pelletier/go-toml v1.8.1
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/evanphx/json-patch/v5 v5.1.0

Cluster creation on local

$ 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

Travis CI example

The configuration uses the Golang language and reuses the manual steps executed previously on local :

All is working like a charm :

The Travis CI output of the previous configuration file
The Travis CI output of the previous configuration file

Conclusion

Kind offers 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

kind
Cloud-NativeCI/CD

Guillaume Vincent

DevOps Engineer & AWS Certified Solution Architect. Cloud enthusiast and automation addict