From 8fe7c11617e86f1c89e34cae096d6b290ea06ce3 Mon Sep 17 00:00:00 2001 From: Ricard Illa Date: Sun, 30 Oct 2022 15:53:04 +0100 Subject: [PATCH] k3s role --- roles/k3s/files/init.d/k3s | 28 +++++++++++++++ roles/k3s/tasks/main.yml | 70 ++++++++++++++++++++++++++++++++++++++ roles/k3s/vars/main.yml | 29 ++++++++++++++++ 3 files changed, 127 insertions(+) create mode 100755 roles/k3s/files/init.d/k3s create mode 100644 roles/k3s/tasks/main.yml create mode 100644 roles/k3s/vars/main.yml diff --git a/roles/k3s/files/init.d/k3s b/roles/k3s/files/init.d/k3s new file mode 100755 index 0000000..fc7f120 --- /dev/null +++ b/roles/k3s/files/init.d/k3s @@ -0,0 +1,28 @@ +#!/sbin/openrc-run + +depend() { + after network-online + want cgroups +} + +start_pre() { + rm -f /tmp/k3s.* +} + +supervisor=supervise-daemon +name=k3s +command="/usr/local/bin/k3s" +command_args="server \ + >>/var/log/k3s.log 2>&1" + +output_log=/var/log/k3s.log +error_log=/var/log/k3s.log + +pidfile="/var/run/k3s.pid" +respawn_delay=5 +respawn_max=0 + +set -o allexport +if [ -f /etc/environment ]; then source /etc/environment; fi +if [ -f /etc/rancher/k3s/k3s.env ]; then source /etc/rancher/k3s/k3s.env; fi +set +o allexport diff --git a/roles/k3s/tasks/main.yml b/roles/k3s/tasks/main.yml new file mode 100644 index 0000000..368deb8 --- /dev/null +++ b/roles/k3s/tasks/main.yml @@ -0,0 +1,70 @@ +--- + +- name: set k3s download suffix + set_fact: + k3s_suffix: "{{ k3s_suffix_lookup[ansible_architecture] }}" + +- name: download k3s binary + get_url: + url: "{{ k3s_download_base_url }}/k3s{{ k3s_suffix }}" + dest: /usr/local/bin/k3s + checksum: "sha256:{{ k3s_download_base_url }}/sha256sum{{ k3s_suffix }}.txt" + mode: '0755' + +- name: create k3s symlinks + file: + state: link + src: /usr/local/bin/k3s + dest: "/usr/local/bin/{{ item }}" + loop: + - kubectl + - crictl + - ctr + +- name: create /etc/rancher/k3s directory + file: + path: /etc/rancher/k3s + state: directory + +- name: create k3s.env + file: + state: touch + path: /etc/rancher/k3s/k3s.env + mode: '0600' + modification_time: preserve + access_time: preserve + +- name: copy service file + copy: + src: init.d/k3s + dest: /etc/init.d + mode: '0755' + when: ansible_distribution == "Alpine" + +- name: enable and start k3s + service: + name: k3s + state: started + enabled: true + when: ansible_distribution == "Alpine" + +- name: add cgroup options cmdline.txt + replace: + path: /media/mmcblk0p1/cmdline.txt + regexp: '^([\w](?!.*\b{{ item }}\b).*)$' + replace: '\1 {{ item }}' + loop: + - "cgroup_memory=1" + - "cgroup_enable=memory" + - "cgroup_enable=cpuset" + +- name: add k3s files to lbu + lbu: + include: + - /usr/local/bin/k3s + - /usr/local/bin/kubectl + - /usr/local/bin/crictl + - /usr/local/bin/ctr + - /etc/rancher/k3s/k3s.env + - /etc/init.d/k3s + when: ansible_distribution == "Alpine" and alpine_mode in ["diskless", "data"] diff --git a/roles/k3s/vars/main.yml b/roles/k3s/vars/main.yml new file mode 100644 index 0000000..d314f3a --- /dev/null +++ b/roles/k3s/vars/main.yml @@ -0,0 +1,29 @@ +--- + +# The version for a given channel can be found with +# +# ```sh +# CHANNEL_URL=${CHANNEL_URL:-'https://update.k3s.io/v1-release/channels'} +# CHANNEL=${CHANNEL:-'stable'} +# curl \ +# --write-out '%{url_effective}' \ +# --location \ +# --silent \ +# --show-error \ +# "${CHANNEL_URL}/${CHANNEL}" \ +# -o /dev/null | +# sed -e 's|.*/||' +# ``` +k3s_version: v1.24.6+k3s1 + +k3s_suffix_lookup: + amd64: "" + x86_64: "" + arm64: "-arm64" + aarch64: "-arm64" + armv7l: "-armhf" # any other arm* should be `-armhf` too + s390x: "-s390x" + +k3s_releases_url: "https://github.com/k3s-io/k3s/releases/download" + +k3s_download_base_url: "{{ k3s_releases_url }}/{{ k3s_version }}"