X
Popular Searches

How to Run Your Own Kubernetes Cluster With MicroK8s

Graphic showing the Kubernetes logo

MicroK8s is a lightweight single-package Kubernetes distribution developed by Canonical, best known for the Ubuntu operating system. MicroK8s follows upstream Kubernetes releases and focuses on providing an effortless installation and management experience. Here’s how to get started running your own cluster.

Installation

MicroK8s is available on Linux, Windows, and macOS. A graphical Windows installer is offered on the project’s website. macOS users should use Brew while Linux users are served by snaps.

Follow the separate Snap installation steps if you’re missing snapd on your system. Then use the snap install command to add MicroK8s:

sudo snap install microk8s --classic

This installs the latest MicroK8s version. You can select a specific release using the --channel flag:

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

This will give you a version of MicroK8s that’s based on Kubernetes v1.19.

Advertisement

Installation can take a while to complete as Kubernetes services start up. Running microk8s status --wait-ready lets you check progress so you know when MicroK8s is ready to use.

Interacting With Your Cluster

MicroK8s bundles its own version of the Kubectl command-line tool. Use microk8s kubectl to interact with your cluster, appending a regular kubectl command:

sudo microk8s kubectl get all --all-namespaces

This command surfaces all the resources in your cluster.

Next create a basic manifest to deploy:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - name: http
          containerPort: 80
          protocol: TCP

Apply the manifest to your cluster:

sudo microk8s kubectl apply -f ./manifest.yaml

MicroK8s will create a new Pod running the NGINX web server. It’ll show up when you re-run microk8s kubectl get all.

MicroK8s is compatible with all Kubectl commands. If you want to drop the microk8s prefix, you can use a “real” kubectl binary instead:

sudo microk8s kubectl config view --raw > $HOME/.kube/config

This exports the MicroK8s connection information into your Kubernetes configuration file, allowing plain kubectl commands to reach your cluster.

Most MicroK8s commands require superuser access. For long-term use, add your user account to the microk8s group so you don’t need to use sudo each time:

sudo usermod -aG microk8s $USER

Accessing Docker Images

MicroK8s can’t access your local Docker images automatically. If you want to deploy an image you’ve just built, manually import it to the MicroK8s registry by exporting it as a tar archive.

docker save my-image:latest > my-image.tar
microk8s ctr image import my-image.tar
Advertisement

Now your Pods can successfully reference the image. You can list all known images in the MicroK8s registry with the microk8s ctr images ls command.

Beyond simple use on your local machine, you should set up a dedicated Docker image registry to hold your images. Push images there and grant MicroK8s pull access so it can retrieve them from your storage. The steps to follow depend on your registry’s configuration.

Enabling the Kubernetes Dashboard

The Kubernetes web dashboard is bundled with MicroK8s but is disabled by default. You can activate it by enabling its addon, then navigating to the IP address of the dashboard service.

microk8s enable dashboard

The service might take a few moments to start. Run microk8s kubectl get services --namespace kube-system and wait for the kubernetes-dashboard service to show up. Note down its CLUSTER-IP and visit its address in your browser. The Kubernetes dashboard should appear, letting you interact with your resources using a graphical interface.

Using More Addons

MicroK8s ships with several other optional addons for common use cases. The dns addon adds support for DNS deployment, storage provides a default storage class based on host directories, and ingress offers an ingress controller that lets you create Ingress routing resources.

Advertisement

The registry addon is a special service that deploys a ready-to-use Docker registry on localhost:32000. Using the registry provides a private storage space which you can push your images to, before using them in your Kubernetes Pods. Similarly, the istio addon launches an Istio mesh to provide service discovery functions.

An exhaustive description of these addons is provided in the MicroK8s documentation. You can view all the available addons in your installation by running microk8s enable without any arguments. Only the bare minimum Kubernetes functionality is active by default as MicroK8s focuses on providing a streamlined experience.

Managing Your Cluster

All cluster management functions are provided by the microk8s command. Running microk8s status shows an overview of your cluster, including whether it’s running and the list of active addons.

To shutdown your cluster, run microk8s stop. This will stop all services, taking everything offline. You restart the cluster with the microk8s start command. It might take a few moments for all your services to get running.

You can view your cluster connection details by running microk8s config. This emits a Kubectl-compatible YAML file, ready to use with other tools.

Running microk8s reset will reset your cluster to its initial post-installation state. This is a destructive time-consuming operation that will destroy all your resources. It facilitates experimentation by letting you teardown everything, without removing the MicroK8s snap. Kubernetes storage volumes will be retained by default; you can delete them too by adding the --destroy-storage flag.

Multi-Node Clusters

Although MicroK8s started as a single-node project, you can now add secondary nodes to your cluster. Additional nodes give you redundancy, as well as increased workload capacity. Each node requires its own fully isolated environment – either a separate physical machine, VM, or container. You can’t run two MicroK8s worker instances in the same environment.

Advertisement

First, run microk8s add-node on your original node. This will become the controlling master that runs the Kubernetes control plane.

microk8s add-node

The command creates a provisional registration for the new node. It’ll output a microk8s join command which will join the node to the control plane. The joining process will take a few seconds to complete. The node must be able to reach your master over the displayed network address.

Once the node’s been added, it will be eligible to host your Pods. You can view all available nodes by running microk8s kubectl get nodes.

Removing a node from the cluster is an exercise in reversal. First, run microk8s leave on the target node. Then run microk8s remove-node 10.108.0.1 on the master, substituting in the IP address of the removed node. This will notify all other nodes of the removal. The target node will revert to a regular standalone MicroK8s installation, becoming its own master.

Conclusion

MicroK8s is a minimal Kubernetes distribution that’s easily self-hosted on your own hardware. It’s fully CNFC-compliant and comes with built-in addons for the official Kubernetes dashboard, Ingress routing, and the Istio service mesh. These help you quickly set up your own production-ready cluster capable of matching the managed Kubernetes offerings from public cloud providers.

Advertisement

MicroK8s supports a particularly broad range of hardware and deployment architectures. It works on developer workstations, in highly available production environments, and on the Raspberry Pi. This flexibility lets you use exactly the same technology in development and production, even down to the Kubernetes implementation.

Compared to rival projects such as Minikube, MicroK8s excels at running in resource-constrained environments with minimal overheads. It benefits from its own containerization, using the snap package format, so there’s no heavy VM to run. This leaves you with more capacity to host your workloads and other developer tools.

While MicroK8s began life as a developer-centric project, targeting local testing and prototyping, it’s now billed as a production-ready Kubernetes distribution with a proven record for closely following upstream releases. Clusters can be upgraded to new Kubernetes releases using the snap refresh microk8s command.

James Walker James Walker
James Walker is a CloudSavvy IT contributor. He is the founder of Heron Web, a UK-based digital agency providing bespoke software development services to SMEs. He has experience managing complete end-to-end web development workflows with DevOps, CI/CD, Docker, and Kubernetes. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.