If you’re looking to deploy and manage containerized applications simply and efficiently, MicroK8s may be just what you need. MicroK8s is a lightweight, fast, easy-to-install version of Kubernetes that runs locally on your computer or server.
In this article, we’ll provide a step-by-step guide to installing MicroK8s, adding your account to the MicroK8s group, enabling the DNS addon, enabling and accessing the Kubernetes dashboard, and deploying an example application.
Requirements
This tutorial uses a Ubuntu Server 22.04 with the Snap package manager pre-installed. Feel free to use any distro where Snap is supported or can be installed.
Installing MicroK8s
The first step in getting started with MicroK8s is to install it. Fortunately, this is a straightforward process that can be done using the snap package manager on Ubuntu, Debian, and other Linux distributions that support it. To install MicroK8s, run the following command:
sudo snap install microk8s --classic
This will download and install the latest stable release of MicroK8s on your system. Once installed, you’ll see a confirmation message like the one below. The result shows the installed version of microk8s.
Let’s confirm the microk8s status by running this command.
sudo microk8s status
The result shows the microk8s service status, the high-availability state, and the enabled and disabled addons. We’ll cover more about addons later.
At this point, you’ve completed the MicroK8s install process.
Adding Your Account to the MicroK8s Group
By default, the MicroK8s commands require sudo permissions to run. To avoid this, you can add your user account to the MicroK8s group, which grants you permission to run MicroK8s commands without sudo. To add your account to the MicroK8s group, run the following command:
sudo usermod -aG microk8s $USER
After running this command, log out and log back in for the changes to take effect, or run the following command.
newgrp microk8s
You can now run MicroK8s commands without using sudo. For example, the command below lists all nodes.
microk8s kubectl get nodes
Enabling the DNS Addon
The DNS addon is an important component of MicroK8s that provides name resolution for Kubernetes services. It is recommended to enable the DNS addon to ensure that your applications can communicate with each other. To enable the DNS addon, run the following command:
microk8s enable dns
This will start the DNS addon and make it available to your applications.
Enabling and Accessing the Kubernetes Dashboard
The Kubernetes dashboard is a web-based interface that allows you to manage your Kubernetes cluster and applications. To enable the dashboard, run the following command:
microk8s enable dashboard
Now that the Dashboard is enabled, let’s first retrieve the token we’ll use to log in. To do so, run the below command in the terminal.
microk8s kubectl create token default
Copy the token and keep it safe for use later.
Next, let’s find out the kubernetes-bashboard service IP address.
microk8s kubectl get services --namespace kube-system
As you can see below, the dashboard’s IP address is 10.152.182.223, and the port number is 443.
The above IP address you see is the Cluster IP, which is only accessible inside the cluster. To make the dashboard accessible outside the cluster, let’s create a port forwarding rule on the host.
Before you run the below command, update these two items:
- Replace the LOCALPORT with any available port number in your host.
- Replace HOST-IP-ADDRESS with the host’s IP address (IP address of the computer where your installed MicroK8s).
microk8s kubectl port-forward \ -n kube-system service/kubernetes-dashboard LOCALPORT:443 \ --address HOST-IP-ADDRESS
In the below example, I’m forwarding the host IP address 192.168.203.145 and port 20443 to the kubernetes-dashboard service.
microk8s kubectl port-forward \ -n kube-system service/kubernetes-dashboard 20443:443 \ --address 192.168.203.145
You’ll see a result similar to the one below. This result indicates that the port forwarding is active.
Open a web browser and navigate to the port forwarded address like so:
https://192.168.20.145:20443
Select Token, paste your token into the Enter token field, and click Sign in.
And you’ve now logged in to the MicroK8s Kubernetes Dashboard.
You can now navigate the dashboard and discover your Kubernetes environment. You can view the services.
The namespaces.
And nodes, among many others.
Deploying Applications
To deploy an application, you must create a deployment and expose it as a service. Here’s an example of deploying the HTTPD application:
First, create a deployment called webserver using the httpd image.
microk8s kubectl create deployment webserver --image=httpd
Next, run the following command to expose the webserver service on port 8080.
microk8s kubectl expose deployment webserver --port=80 --type=NodePort
Now, let’s confirm that the webserver service is up.
microk8s kubectl get svc webserver
Based on the below result, you can access the webserver service using the 10.152.183.81 IP address on port 80.
You can now access this webserver service using the following URLs.
- HTTP://CLUSTER-IP
- HTTP://LOCALHOST:30631
- HTTP://HOST-IP:30631
curl HTTP://10.152.183.81 curl HTTP://localhost:30631 curl HTTP://192.168.203.145:30631
But, if your host is headless and using a browser locally is impossible, you can create a port forwarding to access the service remotely. In this example, let’s forward port 8080 on the host to the webserver service.
microk8s kubectl port-forward \ -n default service/webserver 8080:80 \ --address 192.168.203.145
You can now access it remotely via your choice of the web browser.
Conclusion
In conclusion, MicroK8s is a powerful tool for managing containerized applications, offering a lightweight and easy-to-use solution for developers and system administrators.
By following the steps outlined in this guide, you can quickly get started with MicroK8s, including installing it, adding your account to the MicroK8s group, enabling the DNS addon, enabling and accessing the Kubernetes dashboard, and deploying an example application.
With MicroK8s, you can streamline your development and deployment processes and take advantage of the power and flexibility of Kubernetes. So why not give it a try today and see what it can do for you?