Add nodes to your MicroK8s Kubernetes cluster
Kubernetes is hard. There’s no escaping that. With so many moving parts, all of which have to be connected, and complicated manifestations, the technology can be quite daunting. If you dive in too quickly, you can become so overwhelmed that you’re not sure which way to go1
That is why it is important to approach Kubernetes slowly and step by step. In this Kubernetes 101 series, we’ve previously talked about deploying MicroK8s on a single server, powered by Rocky Linux.
This time 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. Make sure you’ve followed the tutorial “Kubernetes 101: The easiest route to install Kubernetes on Rocky Linux” and make sure you do this on all the nodes you need for the cluster. Once you’ve completed these steps on each machine, you’re ready to add nodes to the cluster.
For my cluster I have three nodes and one controller. The IP address scheme looks like this:
- 192.168.1.100 Kube controller
- 192.168.1.101 cubenode1
- 192.168.1.102 kubenode2
- 192.168.1.103 kubenode3
It is also important to change the hostname for each node. Remember the command to change a hostname on Linux is:
sudo hostnamectl set-hostname NAME
sudo hostnamectl set–hostname NAME |
Where NAME is the hostname you want to give the machine. In our case, those hostnames are:
- Kube controller
- Kubenode1
- Kubenode2
- Kubenode3
Edit the Hosts file on the nodes
The first thing we’re going to do is edit the hosts file on each machine. Open that file with:
At the bottom of the file we add:
192.168.1.100 kubecontroller 192.168.1.101 kubenode1 192.168.1.102 kubenode2 192.168.1.103 kubenode3
192.168.1.100 Kube controller 192.168.1.101 Kubenode1 192.168.1.102 Kubenode2 192.168.1.103 Kubenode3 |
Save the file and close it with the [Ctrl]+[X] Keyboard shortcut.
Open the necessary firewall ports
In order for the nodes to communicate with the controller, you must open the necessary firewall ports. This is done on the controller with the commands:
sudo firewall-cmd –add-port={25000/tcp,16443/tcp,12379/tcp,10250/tcp,10255/tcp,10257/tcp,10259/tcp} –permanent sudo firewall-cmd –reload
sudo firewall–cmd —add–port={25000/tcp,16443/tcp,12379/tcp,10250/tcp,10255/tcp,10257/tcp,10259/tcp} —permanently sudo firewall–cmd —reload |
On each node, you should also open specific firewall ports with the following commands:
sudo firewall-cmd –add-port={25000/tcp,10250/tcp,10255/tcp} –permanent sudo firewall-cmd –reload
sudo firewall–cmd —add–port={25000/tcp,10250/tcp,10255/tcp} —permanently sudo firewall–cmd —reload |
You may need to add another port (on all machines) with the commands
sudo firewall-cmd –add-port=19001/tcp –permanent sudo firewall-cmd –reload
sudo firewall–cmd —add–port=19001/tcp —permanently sudo firewall–cmd —reload |
Generate the Add Node command
Still, you need to run a command on the controller which then generates the required add command. That command is:
The output contains the full command to run on each node to be merged. That command looks something like this:
microk8s joins 192.168.1.100:25000/87636eo7d6r80787576t37o08ft2f2ac/979uj65e74b6
microk8s participate 192.168.1.100:25000/87636eo7d6r80787576t37o08ft2f2ac/979uj65e74b6 |
After running 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:
microk8s kubectl gets nodes
microk8s kubectl to get nodes |
You should see that all three nodes are now part of the cluster.
Make this a little easier
As you can see, with MicroK8s you use the kubectl command through the microk8s command. That’s a bit more typing than you’d care to handle. Fortunately, you can set up 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:
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/
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 pl kubectl /usr/local/bin/ |
Next, we generate a configuration file and store it in ~/.kube/ with the command:
microk8s configuration > ~/.kube/config
microk8s configuration > ~/.Kube/configuration |
You should now be able to use the kubectl command without typing microk8s
. For example, to view the newly added nodes, the command would now be:
You don’t have to go this route. If you don’t mind the extra typing, microk8s kubectl
is a perfect way to go.
Remove a node from the cluster
If you happen to need to remove a node from your cluster, all you need to do is log into that node and run the command:
Add more nodes later
You are not just limited to the nodes you add this round. Once upon a time I just saved the join command to a file and copied and pasted it at any time. That doesn’t work anymore because the join token is time sensitive. Still, you can add and remove nodes as needed to increase or decrease the size of your cluster. Whenever you need to join a new node, you just need to run the join command like so:
You receive the same join statement you ran on the first nodes, just with a different join token. If you try to run the same join command again as before, you will get an invalid token command. So make sure you remember the add-node command so you can add more nodes later.
And that’s all there is to linking nodes to a controller to create your first Kubernetes cluster through MicroK8s.