Kubernetes is great for managing containers, making it easy for developers to deploy and scale applications. For example, you can use Kubernetes to run a website that can handle lots of traffic, manage a big data processing system, or deploy microservices. But setting up a Kubernetes cluster can be tricky, with many steps and settings. This is where Ansible helps. Ansible is a tool that automates the setup process, making it much easier to create a Kubernetes cluster.

In this guide, we’ll show you how to build a Kubernetes cluster using Ansible. We assume that all nodes in the Kubernetes cluster are running Ubuntu Linux.
Prerequisites
Before we start the setup process, make sure you have the following:
- Ansible: Install Ansible on your control machine. This is where you’ll run the Ansible scripts. The control machine can be your local computer or a server.
- Target Nodes: These are the machines where Kubernetes will be installed. You need at least two nodes: one for the master node and one for a worker node. Make sure these nodes are running a compatible Linux distribution.
- SSH Access: Ansible uses SSH to communicate with the target nodes. Ensure you have SSH access set up for all target nodes from your control machine.
Step 1: Ansible Installation
First, set up Ansible on your control machine. The installation process depends on your operating system. For Ubuntu and other Debian-based systems, use these commands:
sudo apt-get update
sudo apt-get install ansible
Step 2: Setup Inventory File
After installing Ansible, configure the inventory file. Create a file named hosts.ini in your working directory. This file lists all target nodes with their IP addresses, organized into groups like masters and workers. Here’s an example:
Step 3: Setup K8s Cluster Playbook
This playbook will help you set up a Kubernetes cluster using Ubuntu systems. It includes tasks for:
- Disabling the firewall
- Turning off swap
- Configuring network settings
- Adding the Docker repository
- Installing Docker and its components
- Adjusting container runtime settings
- Adding the Kubernetes repository
- Installing Kubernetes components (kubelet, kubeadm, kubectl)
Note that kubectl is only installed on nodes identified as masters. The playbook covers everything needed to prepare systems for Kubernetes, focusing on required services, security settings, and essential Kubernetes components.
Create a setup-playbook.yaml file and add the following content:
Execute the playbook by running:
ansible-playbook -i hosts.ini setup-playbook.yml
Step 4: Setup Master Node
This Ansible playbook is for configuring the master node of a Kubernetes cluster. It initializes the Kubernetes cluster with specific network settings, creates a .kube directory, and copies the kube config file to the user’s home directory for cluster management. It also installs the Calico network plugin to handle pod networking, ensuring pods can communicate across nodes. This playbook automates the initial setup, making it easier to deploy a Kubernetes cluster.
Create a file named master-playbook.yml and add the following content:
Execute the playbook with command:
ansible-playbook -i hosts.ini master-playbook.yml
Step 5: Setup Worker Nodes Playbook
This playbook automates the process of adding worker nodes to a Kubernetes cluster. It runs on all nodes, generates a join command on the master node, and sends it to the worker nodes. First, it fetches the join command using kubeadm token create and stores it. Then, it sets a fact with this command for the worker nodes to use. Finally, the worker nodes use this command to join the Kubernetes cluster, bypassing preflight checks. This playbook simplifies adding new nodes to the cluster.
Create a file named worker-playbook.yml and add below content:
Execute the playbook with command:
ansible-playbook -i hosts.ini worker-playbook.yml
Step 5: Verifying the Cluster
Once the playbooks have been executed, you can verify that the Kubernetes cluster is set up correctly.
On the master node, run:
kubectl get nodes
You should see all the nodes listed with their statuses as Ready.
Conclusion
By following these steps, you have successfully set up a Kubernetes cluster using Ansible. This method provides a scalable and repeatable process, making it easier to manage and automate the deployment of Kubernetes clusters. As you get more comfortable with Ansible and Kubernetes, you can customize your playbooks to fit your needs, like adding more worker nodes or setting up advanced features.
2 Comments
kubernetes apt repositry error how to solve the issue
i have same error , are you resolved it ?