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
Senior Platform Engineer & CNCF Certified Kubestronaut
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)
- OS Pre-Flight Role (
prereq): Disables swap, loadsoverlayandbr_netfilterkernel modules, and enables IPv4 packet forwarding. - Container Runtime Role (
containerd): Installscontainerd, generates/etc/containerd/config.toml, and enablesSystemdCgroup = true. - Control Plane Role (
master): Runskubeadm init, configures~/.kube/config, installs the Calico CNI network plugin, and captures the join command. - Worker Node Role (
worker): Executes the dynamically generatedkubeadm jointoken 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
Install an External Ceph Distributed Storage Cluster Using Ansible
Provision a self-healing, multi-node Ceph distributed storage cluster with Ansible to deliver replicated block (RBD), shared file (CephFS), and object storage for Kubernetes.
Deploy Self-Hosted Enterprise GitLab on Ubuntu with Ansible
Automate the deployment, TLS configuration, and day-2 tuning of a self-hosted GitLab instance on Ubuntu servers using idempotent Ansible playbooks.
How to Setup High-Availability Load Balancing with HAProxy & Keepalived
Configure redundant Active/Passive HAProxy load balancers with Keepalived VRRP floating Virtual IPs (VIP) on Ubuntu 24.04 to eliminate single points of failure.