Deploy a Kubernetes Cluster on Ubuntu Server with Microk8s

Kubernetes is hard. There is no way around that. From start to finish, you’ll find so many roadblocks in your way that, at times, you might feel so inclined as to give up.
Don’t.
There’s actually a very easy way to deploy a Kubernetes cluster that can be used for development purposes or even production.
That method is thanks to Microk8s.
Microk8s was created by Canonical as a simplified means of deploying Kubernetes environments to either commodity hardware or third-party cloud hosts (such as Amazon Web Services, Azure, Rackspace, and Linode). Microk8s is, hands down, the easiest tool for getting up to speed with Kubernetes.
And it’s not even close.
Because it’s so simple, you might think Microk8s is limited to a single-node dev environment. In that thinking, you’d be wrong. The developers of Microk8s have actually made it possible to cluster instances together. Case in point, all it takes is three Microk8s nodes to add failover into the mix.
And that’s exactly what I’m going to do today… show you how to deploy a Kubernetes cluster with the help of Microk8s.
Requirements
I’m going to demonstrate on Ubuntu Server 22.04, but this process will work on any operating system that supports Snap packages. You’ll need three instances of Ubuntu Server that are on the same network and a user with sudo privileges. That’s it… let’s make some magic.
Installing Microk8s
The first thing you’ll do is install Microk8s on each node. So log in to your first node and issue the command:
sudo snap install microk8s --classic
Do the same thing on every one of your nodes. As well, you’ll need to add your user to the microk8s group with the command:
sudo usermod -aG microk8s $USER
Log out and log back in so the changes will take effect. Do this on each node.
Create a new directory with the command:
mkdir ~/.kube
Give that new directory the proper permissions with the following command:
sudo chown -f -R $USER ~/.kube
Huzzah!
Configure Hostnames and Hosts
Before you continue on, you’ll need to configure the hostnames and hosts file on each node. To set the hostname, you issue the command like so:
sudo hostnamectl set-hostname microk8scontroller
You can name the system anything you like (such as microk8scontroller, microk8snode1, microk8snode2, etc.).
Next, you’ll need to configure the hosts file on each machine. Open that file for editing with the command:
sudo nano /etc/hosts
At the bottom of that file, add lines such as (using your hostnames and IP addresses):
1 2 3 |
192.168.1.100 microk8scontroller 192.168.1.101 microk8snode1 192.168.1.102 microk8snode2 |
You’ll need to add the above to each node’s hosts file. Save and close the file.
At this point, if you could verify everything’s up and running so far by running the command on the controller node:
microk8s kubectl describe node
You should see a detailed description of the controller node.
How to Join Nodes
Unlike standard Kubernetes, this process is considerably easier. From the controller node, issue the command:
microk8s add-node
The output of the above will include the join command that will look something like this:
microk8s join 192.168.1.137:25000/d6c656de35d466889c92cebcaca0d843/30b781bb37e8
Head over to your first node and run that join command. When it completes, go to your next node and run the same command. Continue doing this until you’ve joined all of your nodes to the cluster.
You can then check to make sure the nodes have all joined with the command:
microk8s kubectl get nodes
You should see output that looks similar to this:
microk8scontroller Ready <none> 23d v1.24.3-2+63243a96d1c393
microk8snode1 Ready <none> 23d v1.24.3-2+63243a96d1c393
microk8snode2 Ready <none> 23d v1.24.3-2+63243a96d1c393
You can also verify the status of the cluster with the command:
microk8s status
The output from the above command should look something like this:
microk8s is running
high-availability: yes
datastore master nodes: 192.168.1.100:19001
datastore standby nodes: none
It’s important to note that you will only see high-availability listed as yes if you have 3 or more nodes. If you have fewer than three nodes, high-availability will be listed as no. If you’re only using this as a development cluster (and don’t need HA immediately), you can get by with just two nodes. Otherwise, make sure to add more to enjoy that feature.
Deploying Hello World to Your Cluster
As a quick test, let’s deploy the Hello World app to the cluster. On the controller, run the following commands:
microk8s kubectl create deployment hello-world --image=tutum/hello-world:latest
microk8s kubectl expose deployment hello-world --type=NodePort --port=80 --name=hello-world-service
microk8s kubectl port-forward -n default service/hello-world-service 8080:80
The final command will not return your terminal to you, but will display that port forwarding is running with output like this:
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
Handling connection for 8080
Log in to your controller node from a second terminal and issue the command:
curl localhost:8080
And you’ll see output that includes <h1>Hello world!</h1>. Of course, if you’re working on a desktop you can open a browser and point it to this page and you’ll see the Hello World page printed.
And that, my new-to-Kube friends, is how you can quickly deploy a Kubernetes cluster with the help of Microk8s. This is a great method of getting a cluster up and running quickly, so you can spend more time developing and less time administering.