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:

  • Kube controller
  • cubenode1
  • kubenode2
  • kubenode3

It is also important to change the hostname for each node. Remember the command to change a hostname on Linux is:

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:

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:

On each node, you should also open specific firewall ports with the following commands:

You may need to add another port (on all machines) with the commands

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:

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:

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:

Next, we generate a configuration file and store it in ~/.kube/ with the command:

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.

Group Made with Sketch.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *