Skip to main content
MK
Mohan Kumar BN Platform Engineer · Kubestronaut
Infrastructure Automation
· 7 min read

Kubernetes Cluster Deployment with Ansible

Automate the end-to-end provisioning of a multi-node Kubernetes cluster (containerd runtime, kubeadm control plane, Calico CNI, and worker registration) using Ansible.

Mohan Kumar BN

Mohan Kumar BN

Senior Platform Engineer & CNCF Certified Kubestronaut

▶ Companion Video Walkthrough (@mkbn-tech) Open on YouTube ↗
Watch video tutorial for Kubernetes Cluster Deployment with Ansible

Zero-Touch Kubernetes Cluster Bootstrapping

Manually configuring kernel modules, sysctl bridging parameters, containerd SystemdCgroup, and kubeadm join tokens across multiple Linux hosts is error-prone. Using Ansible, we codify the entire cluster lifecycle into repeatable roles.

Playbook Architecture (k8s-cluster-with-ansible)

  1. OS Pre-Flight Role (prereq): Disables swap, loads overlay and br_netfilter kernel modules, and enables IPv4 packet forwarding.
  2. Container Runtime Role (containerd): Installs containerd, generates /etc/containerd/config.toml, and enables SystemdCgroup = true.
  3. Control Plane Role (master): Runs kubeadm init, configures ~/.kube/config, installs the Calico CNI network plugin, and captures the join command.
  4. Worker Node Role (worker): Executes the dynamically generated kubeadm join token across all worker hosts.

Key Ansible Task Snippet

- name: Initialize Kubernetes Control Plane with kubeadm
  ansible.builtin.command: >
    kubeadm init
    --pod-network-cidr=192.168.0.0/16
    --apiserver-advertise-address={{ ansible_default_ipv4.address }}
  args:
    creates: /etc/kubernetes/admin.conf

- name: Generate Worker Join Command
  ansible.builtin.command: kubeadm token create --print-join-command
  register: kubernetes_join_command
  changed_when: false

Run ansible-playbook -i inventory site.yml to stand up a clean, CNI-ready Kubernetes cluster in under five minutes.

Related Engineering Blueprints