How To run Local Kubernetes Cluster in Docker Containers

Posted on 146 views

Question: Can I run Kubernetes in Docker?. If you want to test Kubernetes without any commitment, the easiest and quickest way is to use Docker Containers. This method doesn’t have many prerequisites and is not resource-intensive. Any decent Linux machine which can run Docker is all that’s required.

install-kubernetes-in-docker-using-kind

We had earlier covered other methods that can help you get a running Lightweight Kubernetes Cluster on Linux. See below.

For Docker Lovers, this method is another option you can explore. The setup has been done on CentOS 7 & Ubuntu Server. But the process should be similar for other Linux distributions.

Step 1: Install Docker

Start by installing Docker runtime engine, this will be used to run all Kubernetes services. Our guides below should be of great help.

For quick install you can use below commands to install Docker:

curl -fsSL get.docker.com -o get-docker.sh
sudo bash get-docker.sh

Confirm if docker is installed properly by running:

$ docker version
Client: Docker Engine - Community
 Version:           20.10.8
 API version:       1.41
 Go version:        go1.16.6
 Git commit:        3967b7d
 Built:             Fri Jul 30 19:54:27 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.8
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.6
  Git commit:       75249d8
  Built:            Fri Jul 30 19:52:33 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.9
  GitCommit:        e25210fe30a0a703442421b0f60afac609f950a3
 runc:
  Version:          1.0.1
  GitCommit:        v1.0.1-0-g4144b63
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Step 2: Install kind Tool on Linuc / macOS

kind is a tool for running local Kubernetes clusters using Docker container “nodes”. kind is primarily designed for testing Kubernetes 1.11+, initially targeting the conformance tests.

Linux:

curl -s https://api.github.com/repos/kubernetes-sigs/kind/releases/latest| grep browser_download_url | grep kind-linux-amd64 | cut -d '"' -f 4  | wget -qi -
chmod a+x kind-linux-amd64
sudo mv kind-linux-amd64 /usr/local/bin/kind

macOS:

# Using brew
brew install kind

# Using Curl
curl -s https://api.github.com/repos/kubernetes-sigs/kind/releases/latest| grep browser_download_url | grep darwin-arm64 | cut -d '"' -f 4  | wget -qi -

Windows:

choco install kind

Check version installed.

$ kind version
kind v0.11.1 go1.16.4 linux/amd64

Step 4: Run local Kubernetes clusters using Docker container “nodes”.

We now have all requirements satisfied. We should be ready to create a local Kubernetes cluster running on Docker containers.

sudo kind create cluster

You should get output like this:

 kind create cluster | sudo tee -a kind_output.txt
Creating cluster "kind" ...
 • Ensuring node image (kindest/node:v1.15.0) 🖼  ...
 ✓ Ensuring node image (kindest/node:v1.15.0) 🖼
 • Preparing nodes 📦  ...
 ✓ Preparing nodes 📦
 • Creating kubeadm config 📜  ...
 ✓ Creating kubeadm config 📜
 • Starting control-plane 🕹️  ...
 ✓ Starting control-plane 🕹️
 • Installing CNI 🔌  ...
 ✓ Installing CNI 🔌
 • Installing StorageClass 💾  ...
 ✓ Installing StorageClass 💾
Cluster creation complete. You can now use the cluster with:

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl cluster-info

You can view a running container with docker ps command.

# docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                  NAMES
d5bd9c025fe6        kindest/node:v1.15.0   "/usr/local/bin/entr…"   27 minutes ago      Up 27 minutes       34213/tcp, 127.0.0.1:34213->6443/tcp   
kind-control-plane

Step 5: Install Kubectl command-line tool

To access the Kubernetes cluster from the command line, you need kubectl command line tool. This can be easily installed by running the command:

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

Make the kubectl binary executable.

chmod +x ./kubectl

Move the binary into your PATH.

sudo mv ./kubectl /usr/local/bin/kubectl

Test to ensure the version you installed is up-to-date:

$ kubectl version
Client Version: version.InfoMajor:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:23:26Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"

For other systems, refer to the official kubectl installation guide.

Configure Autocompletion:

source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc 

For zsh users, run:

source <(kubectl completion zsh)
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc

Step 6: Configure kubectl

Create Kubectl configuration directory:

mkdir ~/.kube

Create kubectl configuration symlink to the Kind generated config file.

ln -s `kind get kubeconfig-path --name="kind"` ~/.kube/config

Test kubectl configuration.

$ kubectl cluster-info 
Kubernetes master is running at https://127.0.0.1:34213
KubeDNS is running at https://127.0.0.1:34213/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

$ kubectl config  get-clusters
NAME
kind

$ kubectl config  get-contexts
CURRENT   NAME                    CLUSTER   AUTHINFO           NAMESPACE
*         [email protected]   kind      kubernetes-admin

$ kubectl get nodes
NAME                 STATUS   ROLES    AGE   VERSION
kind-control-plane   Ready    master   30m   v1.15.0

$ kubectl get namespaces 
NAME              STATUS   AGE
default           Active   31m
kube-node-lease   Active   31m
kube-public       Active   31m
kube-system       Active   31m

$ kubectl get pods -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-5c98db65d4-q7k7w                     1/1     Running   0          31m
coredns-5c98db65d4-svdpf                     1/1     Running   0          31m
etcd-kind-control-plane                      1/1     Running   0          30m
kindnet-nv5gq                                1/1     Running   1          31m
kube-apiserver-kind-control-plane            1/1     Running   0          30m
kube-controller-manager-kind-control-plane   1/1     Running   0          30m
kube-proxy-4n874                             1/1     Running   0          31m
kube-scheduler-kind-control-plane            1/1     Running   0          30m

You’re on the right track to Learning Kubernetes using your local cluster. I recommend you visit K8s Concepts page to use interactive guides available.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.