Launching a Multi-Node Kubernetes Cluster using Ansible!
This blog will explain the process of launching a Multi-Node Kubernetes cluster using Ansible. The same Ansible code can be used to launch the same cluster on any platform whether it is cloud, bare-metal, etc., the only thing to change for launching the cluster on different platforms is the IP Address of the machines in the specified groups of Master & Worker nodes of Kubernetes.
If you have ever set up a Kubernetes cluster on your own, then you definitely know how painful the task it is, it consumes a lot of time & also it has multiple stages for a perfect configuration of the same.
In this Agile/Automation World, we have to use automation for the task, because it not only saves a lot of time, but it also reduces the chances of mistake/human error which can be occurred by humans. For Example, if there are 100 machines that are to be configured, then there is a very high chance for human error in the configuration process. Therefore, to eliminate the error, we have to adopt automation tools like Terraform, Ansible, etc., based on the requirement/use-case. Here in this case Ansible is used because it is a configuration management tool & we have the use-case associated with configuration management only.
The complete process of the Kubernetes Multi-Node cluster is explained below with all the steps required. Here, Kubeadm is used to configure the cluster, with a kubelet program present on all the nodes for the communication between the PODS, & kubectl for running the Kubernetes Command.
Note: Ansible Playbook is created for this Pipeline & having some knowledge of Ansible would be a plus point in order to understand effectively & completely.
Steps in the Configuration Pipeline
This Configuration will contain 3 files, one for the basic setup for Master & worker Nodes in the cluster, another one for initializing the Master Node, & the last one for running the PODS on the Worker Nodes.
Configuration of the Master & Worker Nodes
- Configuring the Yum Repository for EPEL Packages (Extra software for future use).
- Configuring the Yum Repository for Docker Repository.
- Configuring the Yum Repository for Kubernetes Repository.
- Installing Docker.
- Installing Kubernetes.
- Stopping Firewall daemon.
- Disabling SELinux.
- Starting the Docker services.
- Installing python3 for docker-py Module and Pip3.
- Installing docker-py.
- Changing the Cgroup of docker from cgroupfs to systemd.
- Disabling Swap permanently after reboot.
- Adding Master & slave IP’s in the /etc/hosts file.
- Installing IpRoute-TC Package.
- Reloading systemd driver.
- Restarting Docker services.
- Starting & Enabling Kubelet services.
- Disabling the swap in the current scenario.
- Renaming the Kubernetes Master hostname to master.
Note: Almost all the steps are the same for configuring the Master & Worker Node, but to reduce the complexity I am explaining here only the steps required to configure the Master Node. Even though the complete code link is present at the end of this blog.
Step 1: Configuring the Yum Repository for EPEL Packages!
Important Note: Use the proper indentation in the code as it is YAML Language code. Each & every piece code shown below is the proper indented code, that is why it is possible that some code might cut off from the screen as GitHub Gist has been used. To view complete code in one page, visit the link given at the end of this blog.
Step 2: Configuring the Yum Repository for Docker Repository!
Step 3: Configuring the Yum Repository for Kubernetes Repository!
Step 4: Installing Docker!
Step 5: Installing Kubernetes!
Step 6: Stopping Firewall daemon!
Step 7: Disabling SELinux!
Step 8: Starting Docker Services!
Step 9: Installing python3 for docker-py Module and Pip3!
Step 10: Installing Docker-py!
Step 11: Changing the Cgroup of docker from cgroupfs to systemd!
Step 12: Disabling Swap permanently after reboot!
Step 13: Adding Master & slave IP’s in the /etc/hosts file!
Step 14: Installing IpRoute-TC Package!
Step 15: Reloading systemd driver!
Step 16: Restarting Docker services!
Step 17: Starting & Enabling Kubelet services!
Step 18: Disabling the swap in the current scenario!
Step 19: Renaming the Kubernetes Master hostname to master!
Till here all the basic requirements for the configuration of the Master Node is complete, only the initialization & adding overlay network part is remaining for which a separate file is created, this part is covered ahead in this blog. But, keep in mind, we have to configure the worker nodes also, which is almost the same procedure, I am not covering that part here, because it will unnecessarily increase the complexity of the blog. For this remaining part, I would suggest to visit the link given at the end of this blog.
Steps to Initialize the Master Node
- Run the kubeadm command for the initialization.
- Create a directory, copy some files in that, & change the ownership.
- Add the flannel overlay network for the connection between PODS running on different containers.
Step 1: Running the Kubeadm Command!
Step 2: Creating & Transforming the required Directories!
Step 3: Adding the Flannel overlay network for communication between PODS!
This concludes the 2nd step of the complete Pipeline.
Now, Worker nodes have to be attached to the Master Node for the successful communication & working of Kubernetes, which can be done by running the command obtained after execution of step 1 initializing the master node part. After running that command on each of the worker nodes, they will be added to the managed node list of the Kubernetes Master.
Note: You will not get this output same as this because the IP and token will vary, & also this output is from the manual configuration, but when Ansible run the playbook, my code will print this output corresponding to the cluster on the console, that has to be used.
Running the PODS on the Worker Nodes
This part will run the PODS on the managed node according to your need & strategy, this command can be altered according to the need of launching a POD.
I am right now launching a POD with a webserver form the Docker Image created by me using the Docker File.
Click on the link below to check out the complete code for this Setup from my GitHub Repository.
Contribute to HarshitDawar55/Ansible development by creating an account on GitHub.
Link to My GitHub Ansible Repository (This contains multiple ansible scripts with a complete description of installing Ansible, what is Ansible, & running the ansible-playbooks).