K8s system requirement configuration using Ansible

PART2:

pic credit: tenor.com

So far in part1 of the Infrastructure setup, we have manually worked on the configuration part of the ansible controller machine. Now we are going to automate by using ansible playbooks to accomplish the rest of Infra setup for K8s cluster installation. If you are looking to run one playbook for all the part 2 execution, please use playbook.

step1: change the hostname of all the k8s target machines.

$ # execute the below snippet in k8s workdir and execute the playbook

cat <<EOF> infra-prereq-part2.yaml
- name: "Infra pre-req setup part2"
  hosts: all
  become: yes
  tasks:
     - name: "change the hostname"
       lineinfile:
          regexp: '.*'
          line: "{{inventory_hostname}}"
          path: /etc/hostname  
EOF

$ ansible-playbook -i inventory infra-prereq-part2.yaml --ask-become-pass
BECOME pass: "enter your sudo pass here..."
Step2: Add the host entries to /etc/hosts file.

Here we are going to use the "template" module, so we will create the template folder under the current workdir.

$ mkdir templates

jinja2 Template format looks as below and ends with "j2" extension.

cat <<EOF> templates/hosts.j2
{% for host in groups['k8s'] %}
{{ hostvars[host]['ansible_host'] }}   {{hostvars[host].inventory_hostname}}
{% endfor %}
EOF

$ # execute the below snippet and run the playbook to add the host entries in all the targets.

cat <<EOF> hosts_file.yaml
- name: hosts file change
  become: yes
  hosts: all
  tasks:
     - name: "hosts entry"
       template:
           src: hosts.j2
           dest: /etc/hosts
           backup: yes
     - name: "Add default values back"
       blockinfile:
               insertbefore: BOF
               marker: " "
               block: |
                  127.0.0.1   localhost controller
                  ::1         localhost controller
               path: /etc/hosts
EOF   

ansible-playbook -i inventory hosts_file.yaml --ask-become-pass
prompt: BECOME pass: "enter your sudo pass here..."

Step3: Final System pre-requisites that need to be addressed before the CRI and K8s install.

$ # Execute the below snippet and apply the playbook for final results.

cat <<EOF> system-final-prereq.yaml
name: Final system pre-req's
  hosts: all
  become: yes
  tasks:
   - name: disable firewall service
     service:
        name: firewalld
        state: stopped
        enabled: false

   - name: Disable SWAP
     shell: |
       swapoff -a

   - name: Disable SWAP in fstab
     lineinfile:
       path: /etc/fstab
       regexp: 'swap'
       state: absent

   - name: disable SELinux
     command: setenforce 0
     ignore_errors: yes

   - name: disable SELinux on reboot
     selinux:
       state: disabled

   - name: Letting iptables see bridged traffic
     shell: cat <<EOF | sudo tee  /etc/sysctl.d/k8s.conf \
                      net.bridge.bridge-nf-call-ip6tables = 1 \
                      net.bridge.bridge-nf-call-iptables = 1 \
                      EOF

   - name: apply the iptables changes
     shell: sysctl --system

   - name: Add the Yum reposiory to Database
     yum_repository:
        name: Kubernetes
        description: K8s YUM repository
        baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
        gpgkey: https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
        gpgcheck: yes
        repo_gpgcheck: yes
        enabled: yes

   - name: Reboot the targets after the swap and selinux
     reboot:
EOF

 ansible-playbook -i inventory system_prereq.yaml --ask-become-pass 


Successfully, we have completed the system pre-requisites and we can now move to Container Runtime, LB setup, and K8s master-slave setup.

Previous: Step1 Infra                                                                   Next: Docker runtime install

Comments