MicroK8s - Kubernetes for developers

Set up a local Kubernetes installation using MicroK8s

Following my initial post on local Kubernetes options and a deep dive on Minikube it is time to take MicroK8s for a spin. I have been looking forward to this for a while, so let’s get started.

Preparation

First things first, as always, I want my MacBook up to date. At the time of writing, it runs macOS Big Sur version 11.2.1 and Xcode version 12.4. Time to update the rest:

brew update
brew upgrade
brew upgrade --cask

In addition, make sure there are no issues. If there are, resolve them first:

brew doctor

Should give the following output when all is ready:

Your system is ready to brew.

Should you run into any issues, I recommend to fix these first. Google is your friend…

If you are running windows, chocolatey is a decent alternative to brew.

Install Multipass

Multipass makes it easy to work with Ubuntu VMs on a Mac or Windows workstation. Install it using:

brew install --cask multipass

After a little while, the installation completes:

🍺  multipass was successfully installed!

For more info on Multipass, check the docs.

Create an Ubuntu VM

TIme to create an Ubuntu VM to install MicroK8s on. Let’s give it some resources as well.

multipass launch --name k8s-master --cpus 4 --mem 16G --disk 50G

After a little while, a new VM is created:

Launched: k8s-master

Open a shell into our new VM

multipass shell k8s-master

And make sure it is up to date:

sudo su
apt update
apt upgrade

Install MicroK8s

Time to install MicroK8s:

sudo snap install microk8s --classic --channel=1.19/stable

And make sure the networking runs smoothly:

sudo iptables -P FORWARD ACCEPT

MicroK8s creates a group to enable seamless usage of commands which require admin privilege. Add your user to this group:

sudo usermod -a -G microk8s $USER
sudo chown -f -R $USER ~/.kube

Re-enter the session so the group update takes place:

su - $USER

Make sure that the cluster was created successfully:

microk8s status --wait-ready

Create an alias for kubectl:

alias kubectl='microk8s kubectl'

For more info on MicroK8s, check the docs.

First deployment

Deploy a simple application:

kubectl create deployment hello --image=akleinloog/hello:v1

Verify that the pod is up and running:

kubectl get pods

And expose it:

kubectl expose deployment hello --port=80 --type=NodePort

See at which port it is available:

kubectl get services

This should give a response similar to:

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.152.183.1    <none>        443/TCP        15m
hello        NodePort    10.152.183.70   <none>        80:31984/TCP   3s

Which tells us that the hello app is exposes at port 31984, so we can test it using:

curl localhost:31984

Which should give output similar to:

Go Hello 1 from hello-656897b96f-dvwc7 on GET ./

On your host machine, list the multipass VMs:

multipass list

Which should give output similar to:

k8s-master              Running           192.168.64.52    Ubuntu 20.04 LTS
                                          10.1.235.192

Which tells us that we can access the app running in our Kubernetes instance using:

curl 192.168.64.52:31984

Or by simply pointing our browser to http://192.168.64.52:31984

Next Steps…

To get more familiar with Kubernetes, follow some basic tutorials here and find some more here. Again, google is your friend, there is plenty of content out there.

In addition, you can install some readily available MicroKs add ons like Istio or Knative and play around with those. Last but not least, extend your single node cluster with additional nodes!

As for me, my next step will be to setup a Rancher instance and create a managed local cluster.

comments powered by Disqus