Add Nodes to Your MicroK8s Kubernetes Cluster

Kubernetes is hard. There’s no way around that. With so many moving pieces, all of which have to interconnect, and complicated manifests, the technology can be rather daunting. Should you dive in too fast, you might become overwhelmed to the point where you’re not sure which way to go1
That’s why it’s important to approach Kubernetes slowly and with a one-step-at-a-time approach. In this Kubernetes 101 series, we’ve previously covered deploying MicroK8s to a single server, powered by Rocky Linux.
This time around we’re going to add nodes to our first Kubernetes machine to create a cluster. These nodes are all managed by the original machine, which I call the controller. You’ll want to make sure to have followed the “Kubernetes 101: The easiest route to installing Kubernetes on Rocky Linux” tutorial and make sure to do this on all of the nodes you need for the cluster. Once you’ve taken care of those steps on every machine, you’re then ready to start adding nodes to the cluster.
For my cluster, I’ll have three nodes and one controller. The IP address scheme looks like this:
- 192.168.1.100 kubecontroller
- 192.168.1.101 kubenode1
- 192.168.1.102 kubenode2
- 192.168.1.103 kubenode3
It’s also important to change the hostname for each node. Remember, the command to change a hostname on Linux is:
1 |
sudo hostnamectl set-hostname NAME |
Where NAME is the hostname you want to give the machine. In our case, those hostnames will be:
- kubecontroller
- kubenode1
- kubenode2
- kubenode3
Edit the Hosts File on the Nodes
The first thing we’ll do is edit the hosts file on every machine. Open that file with:
1 |
sudo nano /etc/hosts |
At the bottom of the file, we’ll add:
1 2 3 4 |
192.168.1.100 kubecontroller 192.168.1.101 kubenode1 192.168.1.102 kubenode2 192.168.1.103 kubenode3 |
Save and close the file with the [Ctrl]+[X] keyboard shortcut.
Open the Necessary Firewall Ports
In order for the nodes to be able to communicate to the controller, you must open the necessary firewall ports. This is done on the controller with the commands:
1 2 |
sudo firewall-cmd --add-port={25000/tcp,16443/tcp,12379/tcp,10250/tcp,10255/tcp,10257/tcp,10259/tcp} --permanent sudo firewall-cmd --reload |
On each node, you will also need to open specific firewall ports with the following commands:
1 2 |
sudo firewall-cmd --add-port={25000/tcp,10250/tcp,10255/tcp} --permanent sudo firewall-cmd --reload |
You might also need to add one more port (on all machines) with the commands
1 2 |
sudo firewall-cmd --add-port=19001/tcp --permanent sudo firewall-cmd --reload |
Generate the Add Node Command
Still, on the controller, you must run a command that will then generate the required add command. That command is:
1 |
microk8s add-node |
The output will include the full command to be run on each node to be joined. That command will look something like this:
1 |
microk8s join 192.168.1.100:25000/87636eo7d6r80787576t37o08ft2f2ac/979uj65e74b6 |
After you’ve run the join command on all nodes, give it a few minutes to make sure everything is up and running. You can then check the nodes from the controller with the command:
1 |
microk8s kubectl get nodes |
You should see all three nodes are now part of the cluster.
Making This a Bit Easier
As you can see, with MicroK8s, you’re using the kubectl command via the microk8s command. That’s a bit more typing than you might want to deal with. Fortunately, you can set MicroK8s to use the system version of the kubectl command. To do that we first install the kubectl command on Rocky Linux with the following:
1 2 3 |
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl sudo chmod +x kubectl sudo mv kubectl /usr/local/bin/ |
Next, we generate a config file and save it in ~/.kube/ with the command:
1 |
microk8s config > ~/.kube/config |
You should now be able to use the kubectl command without the need to type microk8s
. For example, to view the newly-added nodes, the command would now be:
1 |
kubectl get nodes |
You don’t have to go this route. If you don’t mind the extra typing, microk8s kubectl
is a perfectly find way to go.
Removing a Node from the Cluster
On the off-chance you need to remove a node from you cluster, all you have to do is log in to that node, and issue the command:
1 |
microk8s leave |
Adding More Nodes Later
You are not just limited to the nodes you add this round. Once upon a time I used to just save the join command to a file and copy/paste it anytime I needed. That no longer works because the join token is time sensitive. Even so, you can add and remove nodes as needed to increase or decrease the size of your cluster. Any time you need to join a new node, all you have to do is run the join command, like so:
1 |
microk8s add-node |
You’ll receive the same join command you ran on the initial nodes, only with a different join token. If you try to rerun the same join command as you did early, you will get an invalid token command. So make sure you remember the add-node command so you can join more nodes later on.
And that’s all there is to joining nodes to a controller to create your first Kubernetes cluster by way of MicroK8s.