The Little Tool To Run K8S In Docker Easily

Guillaume Vincent
Guillaume Vincent
Photo by Simone Viani / Unsplash
Photo by Simone Viani / Unsplash
Table of Contents
Table of Contents

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:

Minikube Architecture Diagram
Minikube Architecture Diagram

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:

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.

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
.travis.yaml

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

kind


Great! Next, complete checkout for full access to Getbetterdevops
Welcome back! You've successfully signed in
You've successfully subscribed to Getbetterdevops
Success! Your account is fully activated, you now have access to all content
Success! Your billing info has been updated
Your billing was not updated