Installing kubernetes (microk8) cluster on multipass vm’s

Olawepo Olayemi
4 min readAug 19, 2021

From my previous post charmed by multipass, we created vms using multipass and we saw how we can easily provision vms. In this post we will be looking at how we can install kubernetes clusters on multipass vm’s.

I recently needed to deploy some edge solution that happens to run on kubernetes and I found the installation and management a little complex and heavy. So I am on the search for alternatives and that is what lead me to microk8.

Microk8 a lightweight single-package Kubernetes distribution is another amazing product from canonical the maker of ubuntu. MicroK8s follows upstream Kubernetes releases and focuses on providing an effortless installation and management experience.

Enough of merry-go-rounding, let get to the topic by creating kubernetes clusters;

Creating master and worker vm nodes

First we need to create vms using multipass.

multipass launch -m 8Gb -d 150G -c 4 -n pmaster 
multipass launch -m 8Gb -d 150G -c 4 -n pworker1

The above command will create two vms with name pmaster and pworker, so to access the vm, run multipass shell pmaster and on each vm run the followings:

ubuntu@pmaster:~$ sudo snap install microk8s --classic --channel=1.18/stable

make the user a sudoer, disable firewall within the vm and enable k8 dashboard and storage.

ubuntu@pmaster:~$ sudo usermod -a -G microk8s ubuntu
ubuntu@pmaster:~$ sudo chown -f -R ubuntu ~/.kube
ubuntu@pmaster:~$ cd ~/.kube && sudo microk8s config > config
ubuntu@pmaster:~$ sudo ufw allow in on cni0 && sudo ufw allow out on cni0
ubuntu@pmaster:~$ sudo ufw default allow routed
ubuntu@pmaster:~$ sudo microk8s enable dns dashboard storage

Add nodes

on node pmaster run

ubuntu@pmaster:~$ sudo microk8s add-node

if all goes well you should see:

ubuntu@pmaster:~$ microk8s add-node
Join node with: microk8s join 10.161.97.44:25000/XfuIVOeppTgUXTFIZHEWxYLEIOygygyX

If the node you are adding is not reachable through the default interface you can use one of the following:
microk8s join 10.161.97.44:25000/XfuIVOeppTgUXTFIZHEWxYLEIOygygyX
microk8s join 10.1.32.0:25000/XfuIVOeppTgUXTFIZHEWxYLEIOygygyX

Copy the command above and execute it on the worker node:

ubuntu@pworker1:~$ microk8s join 10.161.97.44:25000/XfuIVOeppTgUXTFIZHEWxYLEIOygygyX

check to see if worker node joins the cluster successfully

ubuntu@pmaster:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
pmaster Ready <none> 39m v1.18.18
pworker1 Ready <none> 30m v1.18.18

We can also perform all the steps above using bash script, which will setup the vms and create the the clusters for us ( this script is tested only on kubernetese 1.18).
create a file create_kube_vms.sh and copy the script below into it;

#!/bin/bashrun_command_on_node () {
node_name=$1
command=$2
multipass exec -v ${node_name} -- ${command}
}
echo "== creating vms master and worker"
multipass launch -m 8Gb -d 150G -c 4 -n pmaster
multipass launch -m 8Gb -d 150G -c 4 -n pworker
HOST_DIR_NAME=${PWD}
echo "[task 1]== mount host drive with installation scripts =="
multipass mount ${HOST_DIR_NAME} pmaster
multipass mount ${HOST_DIR_NAME} pworker
echo "[task 2]== installing microk8s =="
run_command_on_node "pmaster" "${HOST_DIR_NAME}/install_microk8s.sh ${HOST_DIR_NAME}"
echo "*** installing kuberbetes on worker node ***"
echo "[task 1]== installing microk8s =="
run_command_on_node "pworker" "${HOST_DIR_NAME}/install_microk8s.sh ${HOST_DIR_NAME}"

Create another file called install_microk8s.sh with the contents bellow;

HOST_DIR_NAME=$1
NET=10.0.0 # internal subnet of virtual machines
IP="$(hostname | sed -e 's/[^0-9]*//')"
IF="$(ip -o -4 route show to default | awk '{print $5}')"
OUT="$(ip -o -4 route show to default | awk '{print $9}')"
# configure static cd - comment this if aassigned automatically
printf "network:\n version: 2\n renderer: networkd\n ethernets:\n $IF:\n addresses:\n - $NET.$IP/24" | tee /etc/netplan/90-static.yaml
sudo netplan apply
sudo apt install nfs-common -y
sudo swapoff -a
sudo snap install microk8s --classic --channel=1.18/stable
# add user to microk8s sudoers group
sudo usermod -a -G microk8s ubuntu
sudo chown -f -R ubuntu ~/.kube
(cd ~/.kube && sudo microk8s config > config) & disown
sudo ufw allow in on cni0 && sudo ufw allow out on cni0
sudo ufw default allow routed
sudo snap install helm --classic
# helm repo
helm repo add stable https://charts.helm.sh/stable
helm repo add jetstack https://charts.jetstack.io
helm repo add bitnami https://charts.bitnami.com/bitnami
# add kubect as alias
sudo snap alias microk8s.kubectl kubectl
if test "$IP" = "1"
then sudo microk8s enable dns dashboard storage helm
sudo rm -rf ${HOST_DIR_NAME}/join_node.sh
JOINCMD=$(sudo microk8s add-node | sed '/microk8s/p' | head -1)
echo "${JOINCMD##Join node with: }" >> ${HOST_DIR_NAME}/join_node.sh
sudo chmod a+x ${HOST_DIR_NAME}/join_node.sh
sudo microk8s config | sed -e "s|server: https://$NET.1:16443|server: https://$OUT:16443|" >/etc/kubeconfig
else
sudo microk8s enable dns dashboard storage helm
sudo ${HOST_DIR_NAME}/join_node.sh &
BACK_PID=$!
while kill -0 $BACK_PID ; do
echo "still trying to join..."
sleep 10
done
fi

executing create_kube_vms.sh should create our master and worker nodes with kubernetes installed.
from your terminal execute multipass shell pmaster and then run kubectl get nodes

if you see nodes listed below, welcome to kubernetes!!

ubuntu@pmaster:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
pmaster Ready <none> 39m v1.18.18
pworker1 Ready <none> 30m v1.18.18

For microk8, kubernetes commands starts with microk8. For example to get pods you usedmicrok8 kubectl get pods, while native kubernetes you need to runkubectl get pods. To be able to do the same without microk8sprefix run this sudo snap alias microk8.kubectl kubectl .

You can also had shortcuts to your kube installation by appending the the commands below to your bash_profile.

alias kc='kubectl'
alias kclf='kubectl logs --tail=200 -f'
alias kcgs='kubectl get service -o wide'
alias kcgd='kubectl get deployment -o wide'
alias kcgp='kubectl get pod -o wide'
alias kcgn='kubectl get nodes'

With this in your bash_profile run command source ~/.bash_profileand then you can now run commands like kcgn to get nodes.

I hope this helps you to get started with setting up kubernetes clusters, please do not forget to clap and follow. My next post we will be deploying an app on kubernetes. in the mean time enjoy the kube land :-D

--

--

Olawepo Olayemi

SW Generalist, Test Automation engineer, Technology enthusiast and entrepreneur. I Love Python and brewing my own beer (IPA usually).