We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – Running Local Kubernetes Cluster in Docker Containers

Learn how to – Running Local Kubernetes Cluster in Docker Containers.

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.

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:           20.10.19
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        d85ef84
 Built:             Thu Oct 13 16:46:58 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.19
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       c964641
  Built:            Thu Oct 13 16:44:47 2022
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.8
  GitCommit:        9cd3357b7fd7218e4aec3eae239db1f68a5a6ec6
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Step 2: Install kind tool on Linux / 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.16.0 go1.19.1 linux/amd64

Step 3: Run Kubernetes cluster in Docker containers (“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:

Creating cluster "kind" ...
 âś“ Ensuring node image (kindest/node:v1.25.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!  

You can view a running container with docker ps command.

$ sudo docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                       NAMES
f0c2eff57d05   kindest/node:v1.25.2   "/usr/local/bin/entr…"   3 minutes ago   Up 3 minutes   127.0.0.1:34989->6443/tcp   kind-control-plane

Step 4: 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
Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.3", GitCommit:"434bfd82814af038ad94d62ebe59b133fcb50506", GitTreeState:"clean", BuildDate:"2022-10-12T10:57:26Z", GoVersion:"go1.19.2", Compiler:"gc", Platform:"linux/amd64"}
Kustomize Version: v4.5.7

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   11m   v1.25.2

$ 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. I recommend you visit K8s Concepts page to use interactive guides available.

Also check:

Cart

Your Cart is Empty

Back To Shop