Question: Can Kubernetes run inside Docker Container?. 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 Engine

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:           27.0.3
 API version:       1.46
 Go version:        go1.21.11
 Git commit:        7d4bcd8
 Built:             Sat Jun 29 00:02:23 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.0.3
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       662f78c
  Built:            Sat Jun 29 00:02:23 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.18
  GitCommit:        ae71819c4f5e67bb4d5ae76a6b735f29cc25774e
 runc:
  Version:          1.7.18
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Step 2: Install kind tool

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.23.0 go1.21.10 linux/amd64

Step 3: Running Kubernetes in Docker

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:

Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.30.0) 🖼
 ✓ 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! 😊

You can view a running container with docker ps command.

$ sudo docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                       NAMES
9c9b662da1f2   kindest/node:v1.30.0   "/usr/local/bin/entr…"   40 seconds ago   Up 37 seconds   127.0.0.1:38187->6443/tcp   kind-control-plane

Step 4: Install kubectl CLI 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
Client Version: v1.30.2
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3

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 5: Using kubectl with Kind

If $KUBECONFIG environment variable is not set, the default cluster access configuration is stored in ${HOME}/.kube/config.

Copy this file to your user accounf.

mkdir ~/.kube
sudo cp /root/.kube/config ~/.kube
sudo chown $USER .kube/config

Test kubectl configuration.

$ kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:34989
CoreDNS is running at https://127.0.0.1:34989/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

$ kubectl config  get-clusters
NAME
kind-kind

$ kubectl config  get-contexts
CURRENT   NAME        CLUSTER     AUTHINFO    NAMESPACE
*         kind-kind   kind-kind   kind-kind

$ kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
kind-control-plane   Ready    control-plane   85s   v1.30.0

$ kubectl get namespaces 
NAME                 STATUS   AGE
default              Active   11m
kube-node-lease      Active   11m
kube-public          Active   11m
kube-system          Active   11m
local-path-storage   Active   11m

$ kubectl get pods -n kube-system
NAME                                         READY   STATUS    RESTARTS   AGE
coredns-565d847f94-4f859                     1/1     Running   0          11m
coredns-565d847f94-65vfd                     1/1     Running   0          11m
etcd-kind-control-plane                      1/1     Running   0          11m
kindnet-wqm6j                                1/1     Running   0          11m
kube-apiserver-kind-control-plane            1/1     Running   0          11m
kube-controller-manager-kind-control-plane   1/1     Running   0          11m
kube-proxy-vx7tv                             1/1     Running   0          11m
kube-scheduler-kind-control-plane            1/1     Running   0          11m

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

Kubernetes Books:

Also check:

LEAVE A REPLY

Please enter your comment!
Please enter your name here