How to deploy Kubernetes using Ansible?
sobes.tech AI
Answer from AI
To set up Kubernetes using Ansible, a playbook is usually created to automate the installation and configuration of cluster components on target servers. The main steps are:
-
Prepare the inventory — a list of hosts divided into groups (e.g., master, worker).
-
Install dependencies — install Docker or another container runtime, and necessary packages.
-
Install kubeadm, kubelet, kubectl — install the main Kubernetes tools.
-
Initialize the cluster on the master node — run
kubeadm initwith the required parameters. -
Configure the network — install a network plugin (e.g., Calico, Flannel).
-
Join worker nodes to the cluster — execute
kubeadm joinon workers.
An example of a simplified Ansible playbook fragment for installing kubeadm on all nodes:
- hosts: all
become: yes
tasks:
- name: Install Docker
apt:
name: docker.io
state: present
- name: Add Kubernetes repository
apt_repository:
repo: 'deb http://apt.kubernetes.io/ kubernetes-xenial main'
state: present
- name: Install kubeadm, kubelet, kubectl
apt:
name:
- kubeadm
- kubelet
- kubectl
state: present
update_cache: yes
- name: Disable swap
command: swapoff -a
when: ansible_swaptotal_mb > 0
# Further separate playbook or tasks for master initialization and worker join
For more complex scenarios, ready-made roles from Ansible Galaxy (e.g., geerlingguy.kubernetes) can be used, which cover the entire process with configurations and optimizations.