Lab Deployment of K8s using Ansible Part 2

A brief recap from part 1

  • Deployed 4x VMs, with ubuntu 20.04
  • Created an inventory list of the nodes
  • Update the VM’s
  • Deployed the prerequisites
  • Deployed Docker-ce

Deploy Non-Root user

#########################################################
# K8 Playbooks: Docker user config
#########################################################
---

- hosts: K8_Lab01
  become: yes
  tasks:
    - name: create the 'vclab' user
      user: name=vclab append=yes state=present createhome=yes shell=/bin/bash

    - name: allow 'vclab' to have passwordless sudo
      lineinfile:
        dest: /etc/sudoers
        line: 'vclab ALL=(ALL) NOPASSWD: ALL'
        validate: 'visudo -cf %s'

    - name: set up authorized keys for the ubuntu user
      authorized_key: user=vclab key="{{item}}"
      with_file:
        - ~/.ssh/id_rsa.pub

Add K8 repositories

#################################################
# Deploy K8s
#################################################
---
- hosts: K8_Lab01
  become: true
  tasks:

    - name: add Kubernetes apt-key for APT repository
      apt_key:
        url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
        state: present

    - name: add Kubernetes APT repository
      apt_repository:
        repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
        state: present
        filename: 'kubernetes'

    - name: Update apt and install kubelet
      apt: update_cache=yes name=kubelet state=latest

    - name: Update the repository cache and update/install package "kubelet" to latest version
      apt:
        name: kubelet
        state: latest
        update_cache: yes

    - name: update/install package "kubeadm" to latest version
      apt:
        name: kubeadm
        state: latest
        
- hosts: K8_Lab01_CT
  become: true
  tasks:
    - name: update/install package "kubectl" to latest version
      apt:
        name: kubectl
        state: latest

Disable Swap

sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo swapoff -a
#########################################################
# K8 Playbooks: Disable Swap
#########################################################
---
- hosts: K8_Lab01
  become: true
  tasks:
    - name: Disable SWAP since kubernetes (1/2)
      shell: |
        swapoff -a


    - name: Disable SWAP in fstab (2/2)
      replace:
        path: /etc/fstab
        regexp: '^([^#].*?\sswap\s+sw\s+.*)$'
        replace: '# \1'

Deploy Master Configuaration

#################################################
# Deploy K8 Master Config
#################################################
---

- hosts: lab-vc-ct01
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 >> cluster_initialized.txt
      args:
        chdir: $HOME
        creates: cluster_initialized.txt

    - name: create .kube directory
      become: yes
      become_user: vclab
      file:
        path: $HOME/.kube
        state: directory
        mode: 0755

    - name: copy admin.conf to user's kube config
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /home/vclab/.kube/config
        remote_src: yes
        owner: vclab

    - name: install Pod network
      become: yes
      become_user: vclab
      shell: kubectl apply -f  https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml >> pod_network_setup.txt
      args:
        chdir: $HOME
        creates: pod_network_setup.txt

Deploy Worker Configuaration

#########################################################
# K8 Playbooks: Worker Config
#########################################################
---

- hosts: lab-vc-ct01
  become: yes
  gather_facts: false
  tasks:
    - name: get join command
      shell: kubeadm token create --print-join-command
      register: join_command_raw

    - name: set join command
      set_fact:
        join_command: "{{ join_command_raw.stdout_lines[0] }}"


- hosts: K8_Lab01_Workers
  become: yes
  tasks:
    - name: join cluster
      shell: "{{ hostvars['lab-vc-ct01'].join_command }} >> node_joined.txt"
      args:
        chdir: $HOME
        creates: node_joined.txt
---
  - import_playbook: k8-usersetup.yml
  - import_playbook: k8-aptupdate.yml
  - import_playbook: k8-dockerprerequistes.yml
  - import_playbook: k8-deploydocker-ce.yml
  - import_playbook: k8-deployk8.yml
  - import_playbook: k8-disableswap.yml
  - import_playbook: k8-masterconfig.yml
  - import_playbook: k8-workerconfig.yml