Automating Kubernetes Cluster Deployment with Ansible: A Step-by-Step Guide

mdshamsfiroz
3 min readOct 31, 2024

--

In today’s fast-paced DevOps world, automation is key. Ansible, a powerful automation tool, can significantly simplify the process of setting up a Kubernetes or OpenShift cluster. In this blog post, we’ll walk through the steps to create a Kubernetes cluster using Ansible.

Prerequisites

Before we begin, ensure you have:

  1. Ansible (version 2.9 or later) installed on your control node
  2. SSH access to your target nodes
  3. Basic understanding of Kubernetes concepts

Step 1: Set Up Your Ansible Environment

First, create a directory for your Ansible project:

mkdir k8s-ansible-deploy
cd k8s-ansible-deploy

Create an ansible.cfg file:

[defaults]
inventory = ./inventory
host_key_checking = False

Step 2: Create the Inventory File

Create an inventory file named inventory with your target nodes:

[masters]
master ansible_host=192.168.1.10
[workers]
worker1 ansible_host=192.168.1.11
worker2 ansible_host=192.168.1.12
[all:vars]
ansible_user=your_ssh_user

Step 3: Create the Playbook

Create a file named deploy-k8s.yml with the following content:

---
- hosts: all
become: yes
tasks:
- name: Install required packages
apt:
name:
- docker.io
- apt-transport-https
- curl
state: present
update_cache: yes
- name: Add Kubernetes apt-key
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: Add Kubernetes repository
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
- name: Install Kubernetes packages
apt:
name:
- kubelet
- kubeadm
- kubectl
state: present
update_cache: yes
- hosts: masters
become: yes
tasks:
- name: Initialize Kubernetes cluster
command: kubeadm init --pod-network-cidr=10.244.0.0/16
args:
creates: /etc/kubernetes/admin.conf
- name: Create .kube directory
file:
path: $HOME/.kube
state: directory
mode: '0755'
- name: Copy admin.conf to user's kube config
copy:
src: /etc/kubernetes/admin.conf
dest: $HOME/.kube/config
remote_src: yes
- name: Install Flannel network
command: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
- name: Get join command
command: kubeadm token create --print-join-command
register: join_command
- name: Copy join command to local file
local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"
- hosts: workers
become: yes
tasks:
- name: Copy the join command to server location
copy: src=join-command dest=/tmp/join-command.sh mode=0777
- name: Join the node to cluster
command: sh /tmp/join-command.sh

Step 4: Run the Playbook

Execute the playbook with the following command:

ansible-playbook -i inventory deploy-k8s.yml

Understanding the Playbook

Our playbook performs the following key tasks:

  1. Installs necessary packages on all nodes (Docker, kubeadm, kubelet, kubectl)
  2. Initializes the Kubernetes cluster on the master node
  3. Sets up networking using Flannel
  4. Generates a join command for worker nodes
  5. Joins worker nodes to the cluster

Customizing Your Deployment

You can easily customize this playbook for your specific needs:

  • Change the network plugin (e.g., Calico instead of Flannel)
  • Add additional Kubernetes configurations
  • Include post-installation tasks like deploying monitoring tools

Best Practices

  1. Use Ansible Vault to encrypt sensitive information
  2. Implement proper error handling and idempotency in your tasks
  3. Consider using Ansible roles for more complex deployments
  4. Regularly update your Kubernetes components

Conclusion

Using Ansible to deploy a Kubernetes cluster offers several advantages:

  • Reproducibility: Your cluster configuration is version-controlled and easily reproducible
  • Scalability: Easily add new nodes to your cluster by updating the inventory and re-running the playbook
  • Flexibility: Customize the deployment process to fit your specific requirements

While this guide provides a basic setup, it serves as a solid foundation for more complex Kubernetes deployments. As you become more comfortable with this approach, you can expand the playbook to include advanced features and integrations.Remember, Kubernetes is a complex system, and production deployments often require additional considerations for security, high availability, and performance tuning. Always refer to the official Kubernetes documentation and best practices when planning your production deployments.

So, whether you’re a tech enthusiast, a professional, or just someone who wants to learn more, I invite you to follow me on this journey. Subscribe to my blog and follow me on social media to stay in the loop and never miss a post.

Together, let’s explore the exciting world of technology and all it offers. I can’t wait to connect with you!”

Connect me on Social Media: https://linktr.ee/mdshamsfiroz

Happy coding! Happy learning!

Happy clustering with Ansible and Kubernetes!

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet