Skip to content

Commit

Permalink
Transition to k3s installation and uninstall scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
hdensity committed Apr 3, 2023
1 parent 0d6d43e commit f03d5b0
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 241 deletions.
36 changes: 0 additions & 36 deletions roles/download/tasks/main.yml

This file was deleted.

1 change: 0 additions & 1 deletion roles/k3s/master/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ server_init_args: >-
{% else %}
--server https://{{ hostvars[groups['master'][0]].k3s_node_ip | split(",") | first | ansible.utils.ipwrap }}:6443
{% endif %}
--token {{ k3s_token }}
{% endif %}
{{ extra_server_args | default('') }}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
# Download logs of k3s-init.service from the nodes to localhost.
# Download logs of k3s.service from the nodes to localhost.
# Note that log_destination must be set.

- name: Fetch k3s-init.service logs
- name: Fetch k3s.service logs
ansible.builtin.command:
cmd: journalctl --all --unit=k3s-init.service
cmd: journalctl --all --unit=k3s.service
changed_when: false
register: k3s_init_log
register: k3s_log

- name: Create {{ log_destination }}
delegate_to: localhost
Expand All @@ -22,7 +22,7 @@
become: false
ansible.builtin.template:
src: content.j2
dest: "{{ log_destination }}/k3s-init@{{ ansible_hostname }}.log"
dest: "{{ log_destination }}/k3s@{{ ansible_hostname }}.log"
mode: 0644
vars:
content: "{{ k3s_init_log.stdout }}"
content: "{{ k3s_log.stdout }}"
98 changes: 22 additions & 76 deletions roles/k3s/master/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
---

- name: Clean previous runs of k3s-init
- name: Stop previous runs of k3s
systemd:
name: k3s-init
name: k3s
state: stopped
failed_when: false

- name: Clean previous runs of k3s-init
command: systemctl reset-failed k3s-init
- name: Clean previous runs of k3s
command: systemctl reset-failed k3s
failed_when: false
changed_when: false
args:
warn: false # The ansible systemd module does not support reset-failed

- name: Download install script
ansible.builtin.get_url:
url: https://get.k3s.io
dest: "{{ ansible_user_dir }}/install_k3s.sh"
mode: 744

- name: Install k3s
command:
cmd: "./install_k3s.sh {{ server_init_args }}"
creates: "{{ systemd_dir }}/k3s.service"
environment:
INSTALL_K3S_VERSION: "{{ k3s_version }}"
K3S_TOKEN: "{{ k3s_token }}"
failed_when: false

- name: Deploy vip manifest
include_tasks: vip.yml

- name: Deploy metallb manifest
include_tasks: metallb.yml

- name: Init cluster inside the transient k3s-init service
command:
cmd: "systemd-run -p RestartSec=2 \
-p Restart=on-failure \
--unit=k3s-init \
k3s server {{ server_init_args }}"
creates: "{{ systemd_dir }}/k3s.service"

- name: Verification
block:
- name: Verify that all nodes actually joined (check k3s-init.service if this fails)
- name: Verify that all nodes actually joined (check k3s.service if this fails)
command:
cmd: k3s kubectl get nodes -l "node-role.kubernetes.io/master=true" -o=jsonpath="{.items[*].metadata.name}"
register: nodes
Expand All @@ -38,63 +45,14 @@
delay: 10
changed_when: false
always:
- name: Save logs of k3s-init.service
include_tasks: fetch_k3s_init_logs.yml
- name: Save logs of k3s.service
include_tasks: fetch_k3s_logs.yml
when: log_destination
vars:
log_destination: >-
{{ lookup('ansible.builtin.env', 'ANSIBLE_K3S_LOG_DIR', default=False) }}
- name: Kill the temporary service used for initialization
systemd:
name: k3s-init
state: stopped
failed_when: false
when: not ansible_check_mode

- name: Copy K3s service file
register: k3s_service
template:
src: "k3s.service.j2"
dest: "{{ systemd_dir }}/k3s.service"
owner: root
group: root
mode: 0644

- name: Enable and check K3s service
systemd:
name: k3s
daemon_reload: yes
state: restarted
enabled: yes

- name: Wait for node-token
wait_for:
path: /var/lib/rancher/k3s/server/node-token

- name: Register node-token file access mode
stat:
path: /var/lib/rancher/k3s/server
register: p

- name: Change file access node-token
file:
path: /var/lib/rancher/k3s/server
mode: "g+rx,o+rx"

- name: Read node-token from master
slurp:
src: /var/lib/rancher/k3s/server/node-token
register: node_token

- name: Store Master node-token
set_fact:
token: "{{ node_token.content | b64decode | regex_replace('\n', '') }}"

- name: Restore node-token file access
file:
path: /var/lib/rancher/k3s/server
mode: "{{ p.stat.mode }}"

- name: Create directory .kube
file:
path: "{{ ansible_user_dir }}/.kube"
Expand Down Expand Up @@ -124,18 +82,6 @@
# would be undefined. This will not be the case during playbook execution.
# noqa jinja[invalid]

- name: Create kubectl symlink
file:
src: /usr/local/bin/k3s
dest: /usr/local/bin/kubectl
state: link

- name: Create crictl symlink
file:
src: /usr/local/bin/k3s
dest: /usr/local/bin/crictl
state: link

- name: Get contents of manifests folder
find:
paths: /var/lib/rancher/k3s/server/manifests
Expand Down
24 changes: 0 additions & 24 deletions roles/k3s/master/templates/k3s.service.j2

This file was deleted.

35 changes: 22 additions & 13 deletions roles/k3s/node/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
---

- name: Copy K3s service file
template:
src: "k3s.service.j2"
dest: "{{ systemd_dir }}/k3s-node.service"
owner: root
group: root
mode: 0755
- name: Download install script
ansible.builtin.get_url:
url: https://get.k3s.io
dest: "{{ ansible_user_dir }}/install_k3s.sh"
mode: 744

- name: Enable and check K3s service
systemd:
name: k3s-node
daemon_reload: yes
state: restarted
enabled: yes
- name: Install k3s
command:
cmd: "./install_k3s.sh {{ extra_agent_args | default('') }}"
creates: "{{ systemd_dir }}/k3s.service"
environment:
INSTALL_K3S_VERSION: "{{ k3s_version }}"
K3S_TOKEN: "{{ k3s_token }}"
K3S_URL: https://{{ apiserver_endpoint | ansible.utils.ipwrap }}:6443

- name: Ensure k3s is in a running state
service:
name: k3s-agent
state: started
register: k3sAgentServiceDetails
until: k3sAgentServiceDetails.status.ActiveState == "active"
retries: 20
delay: 10
24 changes: 0 additions & 24 deletions roles/k3s/node/templates/k3s.service.j2

This file was deleted.

57 changes: 14 additions & 43 deletions roles/reset/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,50 +1,21 @@
---
- name: Disable services
systemd:
name: "{{ item }}"
state: stopped
enabled: no
failed_when: false
with_items:
- k3s
- k3s-node
- k3s-init
- name: Check if uninstall script exists
stat:
path: /usr/local/bin/k3s-uninstall.sh
register: uninstalscript

- name: RUN pkill -9 -f "k3s/data/[^/]+/bin/containerd-shim-runc"
register: pkill_containerd_shim_runc
command: pkill -9 -f "k3s/data/[^/]+/bin/containerd-shim-runc"
changed_when: "pkill_containerd_shim_runc.rc == 0"
failed_when: false
- name: Uninstall k3s (server)
command: /usr/local/bin/k3s-uninstall.sh
when: uninstalscript.stat.exists

- name: Umount k3s filesystems
include_tasks: umount_with_children.yml
with_items:
- /run/k3s
- /var/lib/kubelet
- /run/netns
- /var/lib/rancher/k3s
- /var/lib/kubelet/pods
- /var/lib/kubelet/plugins
- /run/netns/cni-
loop_control:
loop_var: mounted_fs
- name: Check if agent uninstall script exists
stat:
path: /usr/local/bin/k3s-agent-uninstall.sh
register: agentuninstalscript

- name: Remove service files, binaries and data
file:
name: "{{ item }}"
state: absent
with_items:
- /usr/local/bin/k3s
- "{{ systemd_dir }}/k3s.service"
- "{{ systemd_dir }}/k3s-node.service"
- /etc/rancher/k3s
- /run/k3s
- /run/flannel
- /etc/rancher/
- /var/lib/kubelet
- /var/lib/rancher/k3s
- /var/lib/rancher/
- /var/lib/cni/
- name: Uninstall k3s (agent)
command: /usr/local/bin/k3s-agent-uninstall.sh
when: agentuninstalscript.stat.exists

- name: Reload daemon_reload
systemd:
Expand Down
16 changes: 0 additions & 16 deletions roles/reset/tasks/umount_with_children.yml

This file was deleted.

2 changes: 0 additions & 2 deletions site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
when: proxmox_lxc_configure
- role: prereq
become: true
- role: download
become: true
- role: raspberrypi
become: true

Expand Down

0 comments on commit f03d5b0

Please sign in to comment.