2 - kubectl create mini-cluster

Posted: 19/5/2024

In my last post, I mentioned that I had set up a simple 2-node cluster for my CKA prep and because I currently don’t have a proper lab cluster set up at home I figured I could make a post about how you can spin up your own lab cluster.

There are several options for deploying Kubernetes cluster. You can use different cloud providers who are offering managed Kubernetes, you can set it up on VMs in the cloud or on-prem and you can also opt for deployment on your local PC. In this post and in the following one, I will demonstrate two relatively simple ways to create a cluster. You will learn how to deploy a Minikube cluster, a simple Kubernetes cluster consisting of only one node that acts as both a control plane node and a worker node. In the next one, you will learn how to deploy two node Kubernetes clusters on virtual machines. The latter option is a bit harder to set up but in the end, provides you with a cluster that is much more similar to a cluster you would come across in a production setting.

Minikube

For Minikube installation, I will be using Ubuntu VM which I am running with Virtualbox on a Windows PC. If you are using a different OS you should take a look at the minikube docs, but the installation process is very similar. Also if you are using macOS with an ARM64-based processor you should probably look into a different environment as you will not be able to run a lot of container images as most of them are currently built only for x84 architecture.

The only prerequisite for Minikube installation is a container or VM manager such as Docker, QEMU, Virtualbox, Podman… We will install Docker as its installation is very straightforward and requires the least amount of configuration afterward.

Install Docker

When installing Docker you have two options. You can install the Docker engine or Docker desktop. Docker engine is a client-server application, that provides you with a dockerd server that runs as a daemon process and a CLI client docker, while Docker desktop is built on top of the Docker engine and extends it with a GUI. As I usually feel more comfortable with using CLI I will install the Docker engine, but you can also go with a Docker desktop if you prefer GUIs.

If you are not using a fresh VM you might want to uninstall any conflicting packages by running the following command otherwise you can feel free to skip it.

for pkg in docker.io docker-doc docker-compose docker-compose-v2 podman-docker containerd runc; do sudo apt-get remove $pkg; done

Before we can install Docker with apt package manager we have to add Docker repository.

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
 
# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Now we can run apt-get install command to install the latest version of Docker:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

To verify successful installation we can run:

sudo docker run hello-world

If the installation was successful you will get the output similar to the one down below:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
c1ec31eb5944: Pull complete
Digest: sha256:a26bff933ddc26d5cdf7faa98b4ae1e3ec20c4985e6f87ac0973052224d24302
Status: Downloaded newer image for hello-world:latest
 
Hello from Docker!
This message shows that your installation appears to be working correctly.

Because by the default Docker daemon binds to Unix socket which is owned by the root user the current configuration requires you to run docker command with sudo.

To use Docker command without sudo we have to create docker group and add user to it. This way the Docker daemon will create a Unix socket that is accessible to users that are added to a docker group.

Create a docker group:

sudo groupadd docker

Add your user to the docker group:

sudo usermod -aG docker $USER

We can now logout and login manually or login with newgrp command:

newgrp docker

If you have set up everything correctly you should now be able to run docker commands without sudo:

docker run hello-world

At this point you should have a working installation of Docker engine and you should be able to run Docker commands without sudo. If you got stuck somewhere you should take a look at official Docker documentation referenced down below.

Install minikube

With Docker installed we can move Minikube installation. The installation process is very simple as we now only have to install Minikube binary.

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64

We can now start our first Kubernetes cluster by running:

minikube start

After a couple of seconds you cluster should be up and running. You can check this by running kubectl get pods -A command which list every pod in all namespaces.

kubectl get pods -A

If this is your first time using kubectl CLI, you might get an error that it is not installed. If you are using Linux you have to run the following commands to install it, otherwise check out the Kubernetes docs on how to install it on macOs or Windows.

# download kubectl binary with curl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
 
# download checksum and validate the binary
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check # expected output kubectl: OK
 
# install kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
 
# if you don't have the root access you can also install it to the ~/.local/bin
# chmod +x kubectl
# mkdir -p ~/.local/bin
# mv ./kubectl ~/.local/bin/kubectl
# export PATH="$PATH:$HOME/.local/bin"

When the kubectl is installed you should get output similar to the one down below:

NAMESPACE              NAME                                        READY   STATUS    RESTARTS      AGE
kube-system            coredns-7db6d8ff4d-t2wgg                    1/1     Running   2 (31m ago)   36m
kube-system            etcd-minikube                               1/1     Running   2 (31m ago)   36m
kube-system            kube-apiserver-minikube                     1/1     Running   2 (31m ago)   36m
kube-system            kube-controller-manager-minikube            1/1     Running   2 (31m ago)   36m
kube-system            kube-proxy-c9mdl                            1/1     Running   2 (31m ago)   36m
kube-system            kube-scheduler-minikube                     1/1     Running   2 (31m ago)   36m
kube-system            storage-provisioner                         1/1     Running   3 (31m ago)   36m
kubernetes-dashboard   dashboard-metrics-scraper-b5fc48f67-954vt   1/1     Running   1 (31m ago)   32m
kubernetes-dashboard   kubernetes-dashboard-779776cb65-k6xbd       1/1     Running   1 (31m ago)   32m

If you are more of a GUI person, Minikube can also provide you with a simple Web interface that you can start up by running:

minikube dashboard

When the dashboard is up, you should get redirected to the main dashboard page in your web browser which should look something like this:

Screenshot_646.png

Summary

In this post we have looked at how to install Docker and how to deploy your first Kubernetes cluster. The cluster of course is not production grade, but it is sufficient to act as your local playground/lab.

Resources

Docker docs - https://docs.docker.com

Minikube docs - https://minikube.sigs.k8s.io/docs/