From e5f8da5d8386eb8cd079fedb25f7de1d9ec7001a Mon Sep 17 00:00:00 2001 From: VR Date: Mon, 30 Jan 2023 10:01:15 +0000 Subject: [PATCH] Added K3s install/uninstall playbooks for 3 node PI4 cluster --- home_linux/config_files/nextcloud-pv.yml | 18 +++ home_linux/config_files/nextcloud-pvc.yml | 15 ++ home_linux/get_facts.yml | 13 ++ home_linux/home_inventory.yml | 7 + home_linux/install_k3s.yml | 162 ++++++++++++++++++++++ home_linux/remove_k3s.yml | 26 ++++ 6 files changed, 241 insertions(+) create mode 100644 home_linux/config_files/nextcloud-pv.yml create mode 100644 home_linux/config_files/nextcloud-pvc.yml create mode 100644 home_linux/get_facts.yml create mode 100644 home_linux/install_k3s.yml create mode 100644 home_linux/remove_k3s.yml diff --git a/home_linux/config_files/nextcloud-pv.yml b/home_linux/config_files/nextcloud-pv.yml new file mode 100644 index 0000000..0914d64 --- /dev/null +++ b/home_linux/config_files/nextcloud-pv.yml @@ -0,0 +1,18 @@ +## nextcloud.persistentvolume.yml +--- +apiVersion: v1 +kind: PersistentVolume +metadata: + name: "nextcloud-seagate" + labels: + type: "local" +spec: + storageClassName: "manual" + capacity: + storage: "50Gi" + accessModes: + - ReadWriteOnce + hostPath: + path: "/mnt/seagate/_NEXTCLOUD" +--- + diff --git a/home_linux/config_files/nextcloud-pvc.yml b/home_linux/config_files/nextcloud-pvc.yml new file mode 100644 index 0000000..2e8a053 --- /dev/null +++ b/home_linux/config_files/nextcloud-pvc.yml @@ -0,0 +1,15 @@ +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + namespace: "nextcloud" + name: "nextcloud-seagate" +spec: + storageClassName: "manual" + accessModes: + - ReadWriteOnce + resources: + requests: + storage: "50Gi" +--- + diff --git a/home_linux/get_facts.yml b/home_linux/get_facts.yml new file mode 100644 index 0000000..db5c9ae --- /dev/null +++ b/home_linux/get_facts.yml @@ -0,0 +1,13 @@ +--- +- name: Get the status of the RPI servers + hosts: all + roles: + - common + become: yes + gather_facts: yes + + tasks: + + - name: Print facts + ansible.builtin.debug: + var: ansible_facts \ No newline at end of file diff --git a/home_linux/home_inventory.yml b/home_linux/home_inventory.yml index 56e21c5..99d79ef 100644 --- a/home_linux/home_inventory.yml +++ b/home_linux/home_inventory.yml @@ -5,6 +5,13 @@ all: 192.168.0.51: 192.168.0.52: 192.168.0.53: + kube_masters: + hosts: + 192.168.0.51: + kube_workers: + hosts: + 192.168.0.52: + 192.168.0.53: docker: hosts: 192.168.0.50: diff --git a/home_linux/install_k3s.yml b/home_linux/install_k3s.yml new file mode 100644 index 0000000..3072fe8 --- /dev/null +++ b/home_linux/install_k3s.yml @@ -0,0 +1,162 @@ +--- +- name: Install K3s on Raspberries + hosts: kubernetes + roles: + - common + become: yes + gather_facts: yes + vars: + k3s_global_env: + - key: K3S_KUBECONFIG_MODE + value: 644 + - key: INSTALL_K3S_EXEC + value: --disable traefik --disable servicelb + k3s_worker_env: + - key: K3S_URL + value: "https://{{ groups.kube_masters[0] }}:6443" + + tasks: + + # Make sure "cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory" is added to the end + # of /boot/cmdline.txt before running this playbook + - name: Add install parameters for all nodes to /etc/environment + ansible.builtin.lineinfile: + dest: /etc/environment + state: present + regexp: "^{{ item.key }}=" + line: "{{ item.key }}={{ item.value }}" + with_items: "{{ k3s_global_env }}" + when: ('kube_masters' in group_names) + + - name: Reset SSH connection to make new environment variables available + ansible.builtin.meta: reset_connection + + - name: Download K3s install script + ansible.builtin.get_url: + url: https://get.k3s.io + dest: ./install-k3s.sh + mode: '0755' + + - name: Install K3s on master node + ansible.builtin.command: + cmd: ./install-k3s.sh + become: no + when: ('kube_masters' in group_names) + + - name: Save cluster token to file on master + ansible.builtin.shell: sudo cat /var/lib/rancher/k3s/server/node-token > token + when: ('kube_masters' in group_names) + register: token + + - name: Copy token to local controller + ansible.builtin.fetch: + dest: buffer/ + src: ./token + flat: yes + when: ('kube_masters' in group_names) + run_once: yes + + - name: Copy token file to workers + ansible.builtin.copy: + src: buffer/token + dest: ./token + when: ('kube_workers' in group_names) + + - name: Load token as host variable on workers + ansible.builtin.command: + cmd: cat token + register: token + when: ('kube_workers' in group_names) + + - name: Add token to worker node's /etc/environment + ansible.builtin.lineinfile: + dest: "/etc/environment" + state: present + line: "K3S_TOKEN={{ token.stdout }}" + when: ('kube_workers' in group_names) + + - name: Add install parameters for worker node to /etc/environment + ansible.builtin.lineinfile: + dest: "/etc/environment" + state: present + regexp: "^{{ item.key }}=" + line: "{{ item.key }}={{ item.value }}" + with_items: "{{ k3s_worker_env }}" + when: ('kube_workers' in group_names) + + - name: Reset SSH connection to make new environment variables available + ansible.builtin.meta: reset_connection + + - name: Install K3s on worker nodes + ansible.builtin.command: + cmd: ./install-k3s.sh + become: no + when: ('kube_workers' in group_names) + + - name: Cleanup install files + ansible.builtin.command: + cmd: rm install-k3s.sh token + + - name: Fix permissions for config file on master node + ansible.builtin.file: + path: /etc/rancher/k3s/k3s.yaml + owner: "{{ ansible_user }}" + group: "{{ ansible_user }}" + when: ('kube_masters' in group_names) + + - name: Generate config file in ~/.kube/config + ansible.builtin.shell: cat /etc/rancher/k3s/k3s.yaml > ~/.kube/config + become: no + when: ('kube_masters' in group_names) + + - name: Fix permissions for config file on master node + ansible.builtin.file: + path: ~/.kube/config + mode: '0600' + become: no + when: ('kube_masters' in group_names) + + - name: Replace localhost references in config file + ansible.builtin.replace: + path: ~/.kube/config + regexp: '(127\.0\.0\.1)' + replace: "{{ ansible_host }}" + become: no + when: ('kube_masters' in group_names) + + - name: Get K3s service status + ansible.builtin.command: + cmd: sudo systemctl status k3s + register: status + when: ('kube_masters' in group_names) + + - name: Print K3s service status + ansible.builtin.debug: + var: status.stdout_lines + when: ('kube_masters' in group_names) + + - name: Pause for 10 seconds to allow cluster setup to complete + ansible.builtin.pause: + seconds: 10 + + - name: Get K3s cluster nodes + ansible.builtin.command: + cmd: kubectl get nodes + register: status + when: ('kube_masters' in group_names) + + - name: Print K3s cluster nodes + ansible.builtin.debug: + var: status.stdout_lines + when: ('kube_masters' in group_names) + + - name: Get K3s cluster status + ansible.builtin.command: + cmd: kubectl get all -A -o wide + register: status + when: ('kube_masters' in group_names) + + - name: Print K3s cluster status + ansible.builtin.debug: + var: status.stdout_lines + when: ('kube_masters' in group_names) diff --git a/home_linux/remove_k3s.yml b/home_linux/remove_k3s.yml new file mode 100644 index 0000000..c30cf00 --- /dev/null +++ b/home_linux/remove_k3s.yml @@ -0,0 +1,26 @@ +--- +- name: Remove K3s from Raspberries + hosts: kubernetes + roles: + - common + become: yes + gather_facts: no + + tasks: + + - name: Remove K3s from master nodes + ansible.builtin.command: + cmd: sudo sh /usr/local/bin/k3s-uninstall.sh + when: ('kube_masters' in group_names) + + - name: Remove K3s from worker nodes + ansible.builtin.command: + cmd: sudo sh /usr/local/bin/k3s-agent-uninstall.sh + when: ('kube_workers' in group_names) + + - name: Unset installation environment variables + ansible.builtin.lineinfile: + path: /etc/environment + state: absent + regexp: 'K3S' +