DevOps has greatly changed the way software engineers and developers develop and deploy applications. One of the technologies at the heart of this revolution is Kubernetes.

Let's explore how you can install a local instance of Kubernetes on Ubuntu using MicroK8s (MicroKates). With this setup in place, you can easily host container applications in a secure, reliable, and highly scalable manner.

Why Use MicroK8s?

Kubernetes is an open-source platform that helps streamline DevOps operations by automating deployment, scaling, and management of containerized applications. Kubernetes is also popularly known as K8s, the name derived from counting the eight letters between k and s in the name Kubernetes. Think of MicroK8s as a Micro Kubernetes instance.

Most public and private production-grade cloud environments that run Ubuntu use Charmed Kubernetes for managing containerized apps. The challenge with Charmed Kubernetes, however, is that it is resource hungry and requires a good level of knowledge to properly configure.

This is where MicroK8s comes in. MicroK8s is a small and lightweight upstream Kubernetes that can run on a developer's PC, an IoT device, or on edge devices natively without the need for a virtual machine. MicroK8s is cross-platform and can run on both Intel and ARM architectures.

MicroK8s gives you one of the best ways to familiarize yourself with Kubernetes, with minimal friction. It is a certified Kubernetes upstream by the Cloud Native Computing Foundation (CNCF), which ensures that implementations of Kubernetes are conformant and interoperable according to the specified industry standards.

Step 1: Installing MicroK8s on Ubuntu

You can easily install MicroK8s on Ubuntu using the snap command.

        sudo snap install microk8s --classic
    

Alternatively, you can install MicroK8s from the GUI using the Ubuntu Software Center. Simply search for microk8s and then click the Install button.

installing microk8sfrom ubuntu software center

Step 2: Configuring Your Firewall for MicroK8s

To ensure seamless communication between pods and with the internet, you should enable incoming and outgoing communication on the container network interface of your firewall.

A pod is the smallest deployable computing unit in Kubernetes; it can be a single or a group of containers with shared storage and network resources, etc.

        sudo ufw allow in on cni0 && sudo ufw allow out on cni0
    

Then, run the following command:

        sudo ufw default allow routed
    

Learn MoreHow to Configure the Ubuntu Firewall With UFW

Step 3: Adding Your User to the MicroK8s Group

At the moment, you'll have to use the sudo command to run most of the MicroK8s commands. While this is a good security measure on a production server, it might not be necessary on a development machine.

Add your user to the MicroK8s group to avoid using sudo.

        sudo usermod -aG microk8s $USER
    

Also, make your current user the owner of the ~/.kube directory.

        sudo chown -f -R $USER ~/.kube
    

For the changes to take effect, reload the user groups by running the following command:

        newgrp microk8s
    

Learn More: How to Manage User Groups on Linux

Step 4: Enabling Important Addons

By default, MicroK8s contains the bare minimum to achieve a minimal footprint on edge and IoT devices. However, you can install a few important addons to manage MicroK8s services. Enable the DNS, dashboard, and storage add-ons with the microk8s command.

        microk8s enable dns dashboard storage
    

The DNS add-on may be required by other services, so it's recommended to always enable it.

The dashboard add-on gives you a graphical overview of the services in MicroK8s; you can also use it for managing services.

You can view a list of the available MicroK8s add-ons by running the command:

        microk8s status
    

Step 5: Viewing the MicroK8s Dashboard

Now that you've enabled the dashboard add-on, you can easily access the MicroK8s dashboard from your web browser.

First, you need the IP address of the dashboard pod. You can get it by running the command:

        microk8s kubectl get all --all-namespaces
    
output listing microk8s namespaces

From the output of the command above, you'll notice that the kubernetes-dashboard service is running on the IP address 10.152.183.35 and is listening on the TCP port 443. Note that this IP address might be different from the one being used by your dashboard service because it is randomly generated.

In your browser, go to the listed IP address. In this case, it will be https://10.152.183.35:443. Make sure you point to the correct URL.

For security purposes, the Kubernetes dashboard will request you for authentication details to sign in. You can access the default dashboard token by running the following command. Then copy the kubernetes.io/service-account-token presented and paste it in the token input.

        token=$(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)
microk8s kubectl -n kube-system describe secret $token
kubernetes dashboard authentication

The dashboard presents you with an overview of the MicroK8s workloads, services, configuration, clusters, etc.

kubernetes dashboard on ubuntu linux

Step 6: Creating a Service With MicroK8s

To see MicroK8s in action, let's create a microbot service deployment that contains two pods. Create the pod using the kubectl command:

        microk8s kubectl create deployment microbot --image=dontrebootme/microbot:v1
    

Scale the microbot pod using the command below.

        microk8s kubectl scale deployment microbot --replicas=2
    

Create a service in order to expose the microbot deployment.

        microk8s kubectl expose deployment microbot --type=NodePort --port=80 --name=microbot-service
    

We've now deployed two microbot pods under the name service/microbot-service. You can view the details of the microbot service and other services by running the following command:

        microk8s kubectl get all --all-namespaces
    
command output showing microk8s services

You'll notice that the microbot service is running on a NodePort, therefore, you can access it via the browser on your PC. In this case, the service is running on the randomly generated port 30353.

In your browser, go to the URL http://localhost:30353, where 30353 is the port number listed in the output above. Make sure you are using the correct port number.

microk8s microbot service running in the browser

Kubernetes vs. Docker: Which One Should You Choose?

This guide has looked at how to install a minimal footprint of Kubernetes locally on your PC or edge device. Kubernetes is part of many developers' workflow and is widely used for deploying high-scale containerized applications in production.

Kubernetes is used for managing or orchestrating a group of containers, e.g. docker containers, with ease. Standalone Docker, on the other hand, is mainly used for managing single containers. The choice of whether to use Docker or Kubernetes will depend on your team's competencies and the size of the software to deploy to production.