Skip to content

Installing a Kubernetes Cluster using minikube

|
Photo by Eva Elijas from Pexels

minikube is the easiest method to deploy a Kubernetes cluster for testing purpose. You can simply spin up a cluster in minutes on your laptop or workstation and can use it for your testing purpose, PoC or whatever local environment setup.

minikube will need a hypervisor (VirtualBox or KVM) to work, but if you are already inside a virtual machine (and Nested Virtualization is NOT possible), then it is possible to skip the creation of an additional VM layer by using the none driver. But please note, you need to install and setup docker environment in that vm.

Only need to do some tests ?

There is a simple lab to experience and test minikube; use it here.

Installing minikube

In this case, we will use a VM in Google Cloud (you can use local VM using VirtualBox or VMWare workstation).

Setup Docker or Virtualization

Here we are using docker instead of Virtualization (VirtualBox etc.). Hence we need to install docker for the same. Below steps are for Debian and you can refer documentation for other operating systems.(or Install Docker on Debian)

## remove any existing package
$ sudo apt-get remove docker docker-engine docker.io containerd runc

## Update the apt package index and install packages 
## to allow apt to use a repository over HTTPS:
$ sudo apt-get update
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

## Add Docker’s official GPG key:
$ curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg  

$  echo \
  "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

## Install docker and tools
$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io

Install and Configure kubectl

You need kubectl to manage your cluster resources.

## Download kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

## Download the kubectl checksum file and Validate
$ curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
$ echo "$(<kubectl.sha256) kubectl" | sha256sum --check

## Install 
$ sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Note: if you have issue with sudo access or other blockers, try below.

# Make the kubectl binary executable.
chmod +x ./kubectl
# Move the binary in to your PATH.
sudo mv ./kubectl /usr/local/bin/kubectl

Test to ensure the version you installed is up-to-date.

$ kubectl version --client

Download and Install minikube

# download and install package
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
 
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube

# check version
$ minikube version
minikube version: v1.21.0
commit: 76d74191d82c47883dc7e1319ef7cebd3e00ee11

Configure to Run docker without sudo

Add your user to the docker group.

$ sudo usermod -aG docker $USER && newgrp docker

Start minikube

One command, and you will get a kubernetes cluster.

$ sudo minikube start --vm-driver=none --wait=false
😄  minikube v1.5.2 on Debian 9.11
🤹  Running on localhost (CPUs=2, Memory=7483MB, Disk=10013MB) ...
ℹ️   OS release is Debian GNU/Linux 9 (stretch)
⚠️  VM may be unable to resolve external DNS records
🐳  Preparing Kubernetes v1.16.2 on Docker '19.03.5' ...
💾  Downloading kubelet v1.16.2
💾  Downloading kubeadm v1.16.2
🚜  Pulling images ...
🚀  Launching Kubernetes ... 
🤹  Configuring local host environment ...

⚠️  The 'none' driver provides limited isolation and may reduce system security and reliability.
⚠️  For more information, see:
👉  https://minikube.sigs.k8s.io/docs/reference/drivers/none/

⚠️  kubectl and minikube configuration will be stored in /root
⚠️  To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:

    ▪ sudo mv /root/.kube /root/.minikube $HOME
    ▪ sudo chown -R $USER $HOME/.kube $HOME/.minikube

💡  This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
⌛  Waiting for: apiserver
🏄  Done! kubectl is now configured to use "minikube"
💡  For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/

Now we have a running kubernetes cluster and let us see the status.

Verify cluster information

$ kubectl cluster-info

$ kubectl get nodes
NAME       STATUS   ROLES                  AGE   VERSION
minikube   Ready    control-plane,master   15m   v1.20.7

Kubernetes Dashboard

You can enable the dashboard as below. (Take another console to run it)

$ sudo minikube dashboard
🤔  Verifying dashboard health ...
🚀  Launching proxy ...
🤔  Verifying proxy health ...
http://127.0.0.1:39067/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

Usually minikube will open the browser with the dashboard URL if you are with GUI. Access the url and check access to the kubernetes cluster.

If you are running minikube inside a VM

Then accessing Dashboard from host machine (or from your laptop) is bit tricky. By default minikube dashboard will exposed as localhost:PORT. In above example, I have deployed the minikube inside a GCP (Google Cloud Platform) instance and see below how I am accessing the Dashboard.

on minikube VM

$ minikube dashboard
🤔  Verifying dashboard health ...
🚀  Launching proxy ...
🤔  Verifying proxy health ...
🎉  Opening http://127.0.0.1:36959/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
👉  http://127.0.0.1:36959/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

On my Laptop/Workstation

## Open an SSH tunnel 
## eg: ssh -L LOCAL_PORT:localhost:REMOTE_PORT [email protected]_VM_IP
$ ssh -L 36959:localhost:36959 [email protected]
.
.
[email protected]:~$ 

Now, open a browser on your laptop and access the same url (from the output of earlier minikube dashboard command)

http://127.0.0.1:36959/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

Bingo !

Explore Kubernetes and get your hands dirty !

Let’s test some application.

## Deploy an app
$ kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
$ kubectl expose deployment hello-minikube --type=NodePort --port=8080

$ kubectl get services hello-minikube

## let minikube open the browser
$ minikube service hello-minikube

## Alternatively, use kubectl to forward the port:
kubectl port-forward service/hello-minikube 7080:8080

For accessing the application, you can again use the ssh port forwarding method I have explained earlier.

LoadBalancer deployments

To access a LoadBalancer deployment, use the “minikube tunnel” command. Here is an example deployment:

$ kubectl create deployment balanced --image=k8s.gcr.io/echoserver:1.4  
$ kubectl expose deployment balanced --type=LoadBalancer --port=8080

## In another window, start the tunnel to create a routable IP for the ‘balanced’ deployment:

$ minikube tunnel

## To find the routable IP, run this command and examine the EXTERNAL-IP column:

$ kubectl get services balanced

## Your deployment is now available at :8080

Finished Testing ?

Once finished, if you do not need the cluster, simply pause or destroy the cluster.

## Pause Kubernetes without impacting deployed applications
$ minikube pause

## Halt the cluster:
$ minikube stop

Troubleshooting

  1. minikube start exits with error on GUEST_MISSING_CONNTRACK

Error ❌ Exiting due to GUEST_MISSING_CONNTRACK: Sorry, Kubernetes 1.20.7 requires conntrack to be installed in root’s path.

Solution:

sudo apt-get install -y conntrack

Gineesh has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan - iamgini.com)

Gineesh has worked as a Systems Engineer, Automation Specialist, and content author. His primary focus is on Ansible Automation, Containerisation (OpenShift & Kubernetes), and Infrastructure as Code (Terraform). (aka Gini Gangadharan - iamgini.com)

Comments

3 Responses

  1. […] Installing a Kubernetes Cluster using minikube – July 1, 2021 […]

  2. Thanks very much for this, very informative!

    Quick note: for local workstation (i.e. not VM), should use –vm-driver=docker. This also allows non-administrator use. Not sure how new this is, but seems to have been introduced since your article.

  3. Yes, good point.

    Thank you for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: