Launching a Multi-Node Kubernetes Cluster using Ansible!

Source: Exoscale

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

  1. Configuring the Yum Repository for EPEL Packages (Extra software for future use).
  2. Configuring the Yum Repository for Docker Repository.
  3. Configuring the Yum Repository for Kubernetes Repository.
  4. Installing Docker.
  5. Installing Kubernetes.
  6. Stopping Firewall daemon.
  7. Disabling SELinux.
  8. Starting the Docker services.
  9. Installing python3 for docker-py Module and Pip3.
  10. Installing docker-py.
  11. Changing the Cgroup of docker from cgroupfs to systemd.
  12. Disabling Swap permanently after reboot.
  13. Adding Master & slave IP’s in the /etc/hosts file.
  14. Installing IpRoute-TC Package.
  15. Reloading systemd driver.
  16. Restarting Docker services.
  17. Starting & Enabling Kubelet services.
  18. Disabling the swap in the current scenario.
  19. 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

  1. Run the kubeadm command for the initialization.
  2. Create a directory, copy some files in that, & change the ownership.
  3. 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.

Command to be executed after Initialization the Kubernetes Master! [Image by Author]

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.

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).

I hope my blog explains each and everything related to the Multi-Node Kubernetes Cluster setup with all the required code & explanation. Thank you so much for investing your time in reading my blog & boosting your knowledge!

--

--

--

Big Data Enthusiast, have a demonstrated history of delivering large and complex projects. Interested in working in the field of AI and Data Science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A template for beautiful REST APIs using Flask, Swagger UI, Streamlit, Docker and Gunicorn.

TextBlock: It’s a Java String With Superpowers

Don’t forget to use CRAP!

3 Steps to Visual Testing for PDF Files [with Code] — Applitools Blog

Want A Thriving Business? Focus On Valid Html!

notes: A Journey Through MV Wonderland w/ Florina Muntenescu

Makefile to Run Administrative Task

The mystic ‘Kali’ Linux

Get the Medium app

Harshit Dawar

Harshit Dawar

Big Data Enthusiast, have a demonstrated history of delivering large and complex projects. Interested in working in the field of AI and Data Science.

More from Medium

Newsletter of Carlos Santana — Issue #39

Backup an entire Kubernetes cluster using Velero to AWS S3

Rancher Introduction: Manage All of Your Kubernetes Clusters as One

What You Might Not Know About Kubernetes