Creating a MicroK8s Cluster

How to create a MicroK8s cluster (hint, it is much easier than you may think...)

Posted by Muddy on September 24, 2022

What is MicroK8s?

MicroK8s is an amazing offering from Canonical (makers of Ubuntu). It is a lightweight Kubernetes Cluster that runs inside of a snap package. It is build to support Production workloads and can be put into an HA cluster with minimal effort.

Although it is a smaller, lightweight Kubernetes, it is quite robust and polished.

Requirements

By default, you can install this on any Ubuntu machine (18.04, 20.04, 22.04) and other distros if you install the Snap store manually. I prefer Ubuntu for this, as I have found it more stable.

Installing MicroK8s

Installing MicroK8s is easy on a fresh Ubuntu Install. For this post, I will be using Ubuntu 22.04 (latest version).

  $ sudo snap install microk8s --classic --channel=1.24
microk8s (1.24/stable) v1.24.4 from Canonical✓ installed

You need to give your user permissions to interact with MicroK8s and store kube config files.

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

Once you have done that, you can check it's status with:

  $ microk8s status --wait-ready
    microk8s is running
    high-availability: no
      datastore master nodes: 127.0.0.1:19001
      datastore standby nodes: none
    addons:
      enabled:
        ha-cluster           # (core) Configure high availability on the current node
      disabled:
        community            # (core) The community addons repository
        dashboard            # (core) The Kubernetes dashboard
        dns                  # (core) CoreDNS
        gpu                  # (core) Automatic enablement of Nvidia CUDA
        helm                 # (core) Helm 2 - the package manager for Kubernetes
        helm3                # (core) Helm 3 - Kubernetes package manager
        host-access          # (core) Allow Pods connecting to Host services smoothly
        hostpath-storage     # (core) Storage class; allocates storage from host directory
        ingress              # (core) Ingress controller for external access
        mayastor             # (core) OpenEBS MayaStor
        metallb              # (core) Loadbalancer for your Kubernetes cluster
        metrics-server       # (core) K8s Metrics Server for API access to service metrics
        prometheus           # (core) Prometheus operator for monitoring and logging
        rbac                 # (core) Role-Based Access Control for authorisation
        registry             # (core) Private image registry exposed on localhost:32000
        storage              # (core) Alias to hostpath-storage add-on, deprecated

Yay! That is it, we have a functioning cluster!

Enabling Add Ons

Add Ons in MicroK8s do numerous things, not all of them will be needed in our case. To see the full list of Add Ons and what they do, I recommend checking out the MicroK8s Docs.

The Add Ons we're going to install are:

  1. dns - needed for DNS resolution inside of the cluster, each pod and service gets a DNS address inside of the cluster.
  2. ingress - needed to expose web applications, installs Nginx Ingress controller which will be exposed on ports 80 and 443.
  3. hostpath-storage - needed for storing persistent files, stores files on local disk (Node OS Disk)

Other Add Ons worth noting:

  1. Metallb - Virtual Load balancer that can place a virtual NIC on your network that listens for traffic, this means if you have more than 1 node, you can assign 1 ip address and it will load balance all the nodes.
  2. Prometheus - Monitoring and metrics within the cluster. This installs the Prometheus Operator, which can be useful if you want to deploy additional Prometheus servers

To enable, simply run:

$ microk8s enable dns ingress hostpath-storage
Infer repository core for addon dns
Infer repository core for addon ingress
Infer repository core for addon hostpath-storage
Enabling DNS
Applying manifest
serviceaccount/coredns created
configmap/coredns created
deployment.apps/coredns created
service/kube-dns created
clusterrole.rbac.authorization.k8s.io/coredns created
clusterrolebinding.rbac.authorization.k8s.io/coredns created
Restarting kubelet
DNS is enabled
Enabling Ingress
ingressclass.networking.k8s.io/public created
namespace/ingress created
serviceaccount/nginx-ingress-microk8s-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-microk8s-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-microk8s-role created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-microk8s created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-microk8s created
configmap/nginx-load-balancer-microk8s-conf created
configmap/nginx-ingress-tcp-microk8s-conf created
configmap/nginx-ingress-udp-microk8s-conf created
daemonset.apps/nginx-ingress-microk8s-controller created
Ingress is enabled
Enabling default storage class.
WARNING: Hostpath storage is not suitable for production environments.

deployment.apps/hostpath-provisioner created
storageclass.storage.k8s.io/microk8s-hostpath created
serviceaccount/microk8s-hostpath created
clusterrole.rbac.authorization.k8s.io/microk8s-hostpath created
clusterrolebinding.rbac.authorization.k8s.io/microk8s-hostpath created
Storage will be available soon.

We now have a functional MicroK8s cluster! You should see some Pods running, if you run the command:

$ microk8s kubectl get pods -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   coredns-66bcf65bb8-czms5                   1/1     Running   0          2m54s
kube-system   calico-node-klp4c                          1/1     Running   0          25m
kube-system   calico-kube-controllers-7dfd687fdb-zbdgj   1/1     Running   0          25m
kube-system   hostpath-provisioner-78cb89d65b-c5xq5      1/1     Running   0          97s
ingress       nginx-ingress-microk8s-controller-cbjn9    1/1     Running   0          97s

You will also see a 404 page if you visit the IP address of your server:
![](__GHOST_URL__/content/images/2022/09/image.png)
So this means Ingress is working as well! Wooo! 

## Conclusion

We have installed MicroK8s, enabled the necessary Add Ons, and tested to make sure it was working. We are now ready for some workloads! 

In the next post, I will show you how to install FreshRSS using Helm and MicroK8s.