Ansible Playbook install Kubernetes

Creating an Ansible playbook to install Kubernetes involves several tasks, as Kubernetes is a complex system with multiple components. Below is a simplified example playbook to establish a primary Kubernetes cluster. Note that this is a basic example and might need modifications to suit your environment and needs. It assumes you’re installing a cluster with a single master node and one or more worker nodes.

Playbook template for creating a Kubernetes cluster using Ansible

Create the playbook file, for example, “kubernetes-playbook.yml

---
- name: Install Kubernetes Cluster
  hosts: all
  become: yes

  tasks:
    - name: Update and upgrade packages
      apt:
        update_cache: yes
        upgrade: dist

    - name: Install Docker
      apt:
        name: docker.io
        state: present

    - name: Start and enable Docker
      systemd:
        name: docker
        state: started
        enabled: yes

    - name: Install kubeadm, kubelet, and kubectl
      apt:
        name: "{{ item }}"
        state: present
      loop:
        - kubeadm
        - kubelet
        - kubectl

    - name: Initialize Kubernetes master
      command: kubeadm init --pod-network-cidr=10.244.0.0/16
      args:
        creates: /etc/kubernetes/admin.conf

    - name: Copy kubeconfig for root user
      command: "{{ item }}"
      loop:
        - mkdir -p /root/.kube
        - cp -i /etc/kubernetes/admin.conf /root/.kube/config

    - name: Install Flannel network plugin
      command: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
      args:
        creates: /etc/kubernetes/manifests/kube-flannel.yaml

    - name: Allow scheduling on the master node (not recommended for production)
      command: kubectl taint nodes --all node-role.kubernetes.io/master-

    - name: Get join command for worker nodes
      command: kubeadm token create --print-join-command
      register: join_command

    - name: Display join command for worker nodes
      debug:
        var: join_command.stdout_lines

# Define your worker nodes in the inventory file

This playbook covers the basic steps to install a Kubernetes cluster using Kubeadm. Please note that this is a simplified example and does not cover advanced configurations, security considerations, high availability, etc. Also, adjust the playbook to your environment, including the appropriate hosts and network configurations.

Before running this playbook, ensure you have the proper inventory file with your target hosts, and modify any configuration parameters as needed. Review the latest official documentation for Kubernetes and Ansible to ensure you follow best practices and use the most up-to-date information.

Sample inventory file

Create a file, for example, inventory.txt

[masters]
master-node ansible_host=your_master_node_ip ansible_user=your_ssh_user

[workers]
worker-node-1 ansible_host=your_worker_node_1_ip ansible_user=your_ssh_user
worker-node-2 ansible_host=your_worker_node_2_ip ansible_user=your_ssh_user

[all:vars]
ansible_ssh_private_key_file=/path/to/your/private/key.pem

Ensure you’ve set up SSH key-based authentication between your Ansible control machine and the target nodes. Also, ensure you’ve set up the necessary DNS or host file entries for hostname resolution.

Running the playbook

Run the playbook using the ansible-playbook command. Replace the playbook YAML file and inventory file with the name of your playbook file and inventory file.

ansible-playbook -i inventory.txt kubernetes-playbook.yml

Runtime

Enter SSH Passphrase or Password (If Required):
If you’ve set up SSH key-based authentication, you might not be prompted for a password or passphrase. However, if you’re using password-based authentication, you’ll be prompted to enter the SSH password for the target hosts.

Ansible Executes Tasks


Ansible will start executing the tasks defined in the playbook on the target hosts. It will display the progress and results of each task.

Review Playbook Output:


Review the output in the terminal to ensure that the tasks are executed successfully. If there are any errors or issues, Ansible will provide relevant information.

Playbook Completion:


Once Ansible completes the playbook execution, it will display a summary of the tasks executed and the status of each task.