Sunday, November 17, 2024
Google search engine
HomeGuest BlogsRunning Local Kubernetes Cluster in Docker Containers

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.

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:           24.0.5
 API version:       1.43
 Go version:        go1.20.6
 Git commit:        ced0996
 Built:             Fri Jul 21 20:35:45 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.6
  Git commit:       a61e2b4
  Built:            Fri Jul 21 20:35:45 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.22
  GitCommit:        8165feabfdfe38c65b599c4993d227328c231fca
 runc:
  Version:          1.1.8
  GitCommit:        v1.1.8-0-g82f18fe
 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.20.0 go1.20.4 linux/amd64

Step 3: Run Kubernetes cluster using Docker containers

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.27.3) 🖼
 ✓ 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.27.3   "/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: v1.28.0
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   11m   v1.27.3

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

Kubernetes Books:

Also check:

RELATED ARTICLES

Most Popular

Recent Comments