Sobes.tech
Middle

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:

  1. Prepare the inventory — a list of hosts divided into groups (e.g., master, worker).

  2. Install dependencies — install Docker or another container runtime, and necessary packages.

  3. Install kubeadm, kubelet, kubectl — install the main Kubernetes tools.

  4. Initialize the cluster on the master node — run kubeadm init with the required parameters.

  5. Configure the network — install a network plugin (e.g., Calico, Flannel).

  6. Join worker nodes to the cluster — execute kubeadm join on 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.