This commit is contained in:
2024-09-18 10:37:20 +01:00
parent 2af64310d1
commit 368a8a9208
11 changed files with 89 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
---
- name: Disable swap on Raspberries
hosts: all
roles:
- common
become: yes
gather_facts: no
tasks:
- name: Disable swap
ansible.builtin.shell: sudo swapoff -a
- name: Reboot
ansible.builtin.reboot:
test_command: uptime
+26
View File
@@ -0,0 +1,26 @@
all:
children:
kubernetes:
hosts:
192.168.0.51:
192.168.0.52:
192.168.0.53:
192.168.0.54:
kube_masters:
hosts:
192.168.0.51:
kube_workers:
hosts:
192.168.0.52:
192.168.0.53:
192.168.0.54:
docker:
hosts:
192.168.0.50:
zero:
hosts:
192.168.0.49:
192.168.0.55:
192.168.0.48:
vars:
ansible_ssh_private_key_file = /home/vraducanu/.ssh/id_rsa
+162
View File
@@ -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)
+26
View File
@@ -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'
@@ -0,0 +1,10 @@
$ANSIBLE_VAULT;1.1;AES256
34323466633533666237623830316138373237326134336236613536633664373564633163613230
3938393338663632613061646365613630373635613536360a613039306363616432633030306334
33353839346339393965616234323561306338626533363238306662383938626364303061633332
6230623361393438610a383230376431373034306331376536313831633366373665393866373338
65313334623131323931353436623265623033396266623364313934383865386532623630336164
32653432613065303363303730353864353739633530376637333662303134613339353062303932
33646466383561373264333734626364663763393766666664653365386230393562626563376265
63303939363932636266303566363062643166343064343762313339353839326361383062653036
6232
@@ -0,0 +1,14 @@
---
- name: Update hosts files on k3s nodes
hosts: kubernetes
roles:
- common
become: yes
gather_facts: no
tasks:
- name: Replace the local A records for local services in k3s
ansible.builtin.lineinfile:
path: /etc/hosts
line: 192.168.0.56 services.ze.internet.local