Home Deploy a kubernetes cluster using ansible
Post
Cancel

Deploy a kubernetes cluster using ansible

Deploying a Kubernetes cluster can be a daunting task, but with the right tools and guidance, it becomes manageable and efficient. In this blog post, I walk you through the process of setting up a Kubernetes cluster with single master and two worker nodes using Ansible role.

This setup is perfect for development and testing environments, providing a solid foundation to explore Kubernetes’ powerful orchestration capabilities. I will cover everything from preparing your environment to executing the Ansible playbook, ensuring you have a running cluster ready for your applications by the end of this guide.

Prerequisites:

Before we begin, there are a few prerequisites we need to address. Here’s what you’ll need:

  • 3 ubuntu 22.04 virtual machines
  • 2 vCPUs & 4 GiB memory per node
  • A host with Ansible installed

First, you’ll need to fork and clone the repo.While you’re at it, give it a ⭐ too!

1
git clone https://github.com/MBN02/Ansible.git

Next, create a ansible.cfg file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#example ansible.cfg file

[defaults]
inventory       = /Users/mohan.kumar.bn/inventory
command_warnings=False
host_key_checking = false
forks = 20
serial = 20
callback_whitelist = timer, profile_tasks
gathering = smart
stdout_callback = yaml
color = true

[ssh_connection]
pipelining = True

Creata an inventory file with your vm’s ip address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[control_plane]
master-node ansible_host=192.168.X.A #repalce the ip

[workers]
worker-node1 ansible_host=192.168.X.B #repalce the ip
worker-node2 ansible_host=192.168.X.C #repalce the ip

[all:vars]
ansible_python_interpreter=/usr/bin/python3

[control_plane:vars]
ansible_ssh_private_key_file= /Users/mohan.kumar.bn/.ssh/id_rsa
ansible_user=root

[workers:vars]
ansible_ssh_private_key_file= /Users/mohan.kumar.bn/.ssh/id_rsa
ansible_user=root

Next, create a playbook called setup_kubernetes.yml

1
2
3
4
5
6
---
- name: Setup Kubernetes Cluster
  hosts: all
  become: true
  roles:
    - setup-kuberntes-cluster

The final step is to execute the ansible role to bootstrap the cluster.

1
2
#ansible-playbook -i inventory playbook.yaml
ansible-playbook setup_kubernetes.yml

Once done, login to controlplane node and run the following command to confirm if the cluster is created successfully.

1
kubectl get nodes
This post is licensed under CC BY 4.0 by the author.