Kubernetes 101: Deploy Portainer to a MicroK8s Cluster

Portainer is a powerhouse container management platform that is not only perfectly at home with Docker deployments but with Kubernetes environments as well. However, as anyone who’s ever dealt with Kubernetes will tell you…it’s hard. Given how many moving pieces are to be had with a Kubernetes cluster, unless you really know what you’re doing, you’ll find managing such an environment can be an absolute nightmare.
Thankfully, there are GUIs like Portainer that go a very long way to simplify your Kubernetes experience. Yes, you still have to have an understanding of how Kubernetes works — you need to know about namespaces, pods, networks, applications, ingresses, and much more. Imagine having a web-based GUI to clear away some of that confusion so you can actually get to the important part…deploying your applications and services.
That’s what Portainer does and with the help of MicroK8s, you can deploy Portainer in such a way as to make managing Kubernetes exponentially easier.
Let me show you how.
Requirements
To make this work, you’ll need at least three machines running Ubuntu Server 22.04 (because that’s my go-to server distribution). Yes, you can do this on another Linux distribution, but you’ll need to modify the installation procedure. The steps for deploying MicroK8s and Portainer are the same, so once you get beyond the MicroK8s installation, everything here will work as it stands.
Step 1: Install MircoK8s
The first thing we must do is install MicroK8s. There is, however, a trick. Portainer will not work with any version of MicroK8s earlier than 1.24. To add to that issue, I’ve found versions later than 1.24 don’t reliably cluster. Because of that, we’ll install version 1.24 of MicroK8s. Here are the steps.
Edit the Hosts Files
The first thing you’ll want to do is edit the host files on all three machines. To open the file for editing, issue the command:
1 |
sudo nano /etc/hosts |
At the bottom of that file, add the following (making sure to use your IP addresses and hostnames):
1 2 3 |
192.168.1.70 k8s1 192.168.1.71 k8s2 192.168.1.72 k8s3 |
Save and close the file. Make sure you edit the hosts file the same way on all three machines.
Change the Hostnames
You’ll also need to change the hostname for each machine. We’ll stick with our convention above. On each machine, you’ll run a command like this:
1 |
sudo hostnamectl set-hostname k8s1 |
Do the same thing on each machine, using k8s2, and k8s3 as the other hostnames. Once you’ve done that, log out of each machine and log back in.
Set the Correct Timezone
You will also need to make sure all machines are on the same (correct) timezone. For this, you’ll run a command like this:
1 |
sudo timedatectl set-timezone America/Kentucky/Louisville |
Make sure to substitute America/Kentucky/Louisville for your timezone, which can be found with the command:
1 |
sudo timedatectl list-timezones |
Install MicroK8s
MicroK8s can be easily installed on Ubuntu Server with snap. On each machine, you’ll issue the command:
1 |
sudo snap install microk8s --channel=1.24/stable --classic |
Next, add your user to the MicroK8s group with:
1 |
sudo usermod -aG microk8s $USER |
Log out and log back in.
Finally, change the ownership of the .kube folder with:
1 |
chown -f -R $USER ~/.kube |
With MicroK8s installed on all three machines, you’re ready to create the cluster.
Cluster Your Machines
On the controller (k8s1), issue the command:
1 |
microk8s add-node |
The output will include the join command that you will run on all your nodes. The command will look something like this:
1 |
microk8s join 192.168.1.70:25000/5c4af12af72cef10c631e5db00e3b3c5/5786c9b07835 |
Back on the controller, verify the nodes have successfully joined with the command:
1 |
microk8s kubectl get nodes |
The output should look something like this:
1 2 3 |
k8s1 Ready <none> 9d v1.24.8-2+1dda18a15eea38 k8s2 Ready <none> 9d v1.24.8-2+1dda18a15eea38 k8s3 Ready <none> 9d v1.24.8-2+1dda18a15eea38 |
You’re now ready to deploy Portainer to the cluster.
Deploying Portainer to the MicroK8s
On the controller you must first enable a few add-ons, which can be done with the following commands:
1 2 3 4 5 6 |
microk8s enable dns microk8s enable ha-cluster microk8s enable ingress microk8s enable metrics-server microk8s enable rbac microk8s enable hostpath-storage |
Next, you must enable the MicroK8s community repository with the command:
1 |
microk8s enable community |
Finally, you can enable Portainer with:
1 |
microk8s enable portainer |
Give Portainer a minute or so to finish deploying. To check on the status of Portainer, issue the following command:
1 |
microk8s kubectl get pods -n portainer |
Once Portainer is listed in a running state, open your default web browser and point it to either http://SERVER:30777 or https://SERVER:30799 (where SERVER is the IP address or domain of the hosting server).
Portainer will prompt you to create a new admin user. Once you’ve done that, make sure to select the local environment and you’ll then be greeted by the Portainer Dashboard (Figure 1), clearly showing you have a Kubernetes environment to work with.
-
Figure 1: Portainer is now ready for your first Kubernetes deployment.
And that’s all there is to deploying the Portainer management platform for a Kubernetes environment. Thanks to MicroK8s, this is a great way to get up to speed with Kubernetes without being overwhelmed by too many commands or the nightmare that can be deploying Kubernetes itself.