Wednesday 31 July 2019

Kubernetes Deployment Via Ansible

/etc/ansible/playbook/kube-dependencies.yaml

- hosts: all
  become: yes
  tasks:
   - name: install gpg
     apt:
       name: gpg
       state: present
       update_cache: true

   - name: install Docker
     apt:
       name: docker.io
       state: present
       update_cache: true

   - name: Enable service
     service:
       name: docker
       enabled: yes

   - name: start service
     service:
       name: docker
       state: started

   - name: install APT Transport HTTPS
     apt:
       name: apt-transport-https
       state: present

   - name: add Kubernetes apt-key
     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: install kubelet
     apt:
       name: kubelet
       state: present
       update_cache: true

   - name: install kubeadm
     apt:
       name: kubeadm
       state: present

- hosts: master
  become: yes
  tasks:
   - name: install kubectl
     apt:
       name: kubectl
       state: present
       force: yes

/etc/ansible/playbook/master.yaml

- hosts: master
  become: yes
  tasks:
    - name: Disable SWAP since kubernetes can't work with swap enabled (1/2)
      shell: |
        swapoff -a
      when: ansible_swaptotal_mb > 0

    - name: Disable SWAP in fstab since kubernetes can't work with swap enabled (2/2)
      replace:
        path: /etc/fstab
        regexp: '^(.+?\sswap\s+sw\s+.*)$'
        replace: '# \1'

    - 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
      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/.kube/config
        remote_src: yes

    - name: install Pod network
      become: yes
      shell: kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
      args:
        chdir: $HOME
        creates: pod_network_setup.txt

/etc/ansible/playbook/workers.yaml

- hosts: master
  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: workers
  become: yes
  tasks:
    - name: Remove swapfile from /etc/fstab
      mount:
        name: swap
        fstype: swap
        state: absent

    - name: Disable swap
      command: swapoff -a
      when: ansible_swaptotal_mb > 0
    
    - name: join cluster
      shell: "{{ hostvars[groups['master'][0]].join_command }} >> node_joined.txt"
      args:
        chdir: $HOME
        creates: node_joined.txt

/etc/ansible/hosts

[master]
192.168.109.150

[workers]
192.168.109.151

[all:vars]
ansible_python_interpreter=/usr/bin/python3