How to create a local Kubernetes cluster


Hi there!
If you start your way in Kubernetes, you might ask how to create a local cluster. It’s necessary for studying. I’m going to tell you that in the article.

I chose Minikube for this. With Minikube, you can set up a single-node Kubernetes cluster. I’ll be enough for most of your exercises. Before starting, you need to install the required tools:

  • Minikube
  • kubectl
  • Docker

Installing them depends on your system, so I believe you can easily find the needed approaches. And we will go directly to set up the cluster.
Hopefully, it’s straightforward with Minikube. We need to start the single-node cluster:

$ minikube start

After a few seconds, we’ll see the successful result:

😄  minikube v1.23.0 on Darwin 11.6
🎉  minikube 1.24.0 is available! Download it:
💡  To disable this notice, run: 'minikube config set WantUpdateNotification false'

✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.22.1 on Docker 20.10.8 ...
🔎  Verifying Kubernetes components...
    ▪ Using image kubernetesui/metrics-scraper:v1.0.4
    ▪ Using image
    ▪ Using image kubernetesui/dashboard:v2.1.0
🌟  Enabled addons: default-storageclass, storage-provisioner, dashboard
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Congrats! Now you have your own single-node Kubernetes cluster on the local machine.
I could end the article at this point, but let’s try to interrupt with our cluster.

The cluster is working now, but no images are there. If we want to launch an application on the cluster, we need to create a deployment with a pod. For example, I’ll use the image from K8s in Action book by Marko Luksa: luksa/kubia. It’s an image with a simple application that listens to HTTP requests on 8080 port.

$ kubectl run kubia --image=luksa/kubia --port=8080

In a few moments, the kubia app will be launched. You can track its status via command: kubectl get pods. But we still cannot send requests to it because the application locates in an isolated environment. So to communicate with it, we need to create a balancer.

$ kubectl expose deployment kubia --type=LoadBalancer --name kubia-http

The balancer needs to obtain an address. But it will not be able to do it until we create a tunnel to minikube. You can see this if you look at the status of the balancer:

$ kubectl get services
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes   ClusterIP        <none>        443/TCP          48d
kubia-http   LoadBalancer   <pending>     8080:30660/TCP   48d

The External-IP column has a value <pending>. It means that our balancer is trying to get an address. To solve this problem, let’s create a tunnel:

$ minikube tunnel

After that, the balancer will get an address, and we’ll be able to send requests:

$ curl localhost:8080 
You've hit kubia-85d8bcbbf8-rd2rx

And let’s do something more. For example, add more replicas for our application. It’s also so easy and could be performed with a single command:

$ kubectl scale deployment kubia --replicas=4

This command will set the number of replicas to 4. If you try to send several requests, they’ll be handled by different pods.

Now you know how to set up a local Kubernetes cluster and how to interact with it! Thanks for reading.