Skip to content

Commit

Permalink
Transition to k3s installation and uninstall scripts (techno-tim#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
hdensity committed Apr 10, 2023
1 parent 0d6d43e commit e6e97ea
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 253 deletions.
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ Hardware:
```yml
k3s_version: ""
ansible_user: NA
systemd_dir: ""

flannel_iface: ""

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env/
.vagrant
*.log
ansible.cfg
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Build a Kubernetes cluster using Ansible with k3s. The goal is easily install a
- [x] Debian (tested on version 11)
- [x] Ubuntu (tested on version 22.04)
- [x] Rocky (tested on version 9)
- [x] Alpine (tested on version 3.17)

on processor architecture:

Expand Down
1 change: 0 additions & 1 deletion inventory/sample/group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
k3s_version: v1.24.12+k3s1
# this is the user that has ssh access to these machines
ansible_user: ansibleuser
systemd_dir: /etc/systemd/system

# Set your timezone
system_timezone: "Your/Timezone"
Expand Down
1 change: 1 addition & 0 deletions inventory/sample/hosts.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[node]
192.168.30.41
192.168.30.42
192.168.30.43

# only required if proxmox_lxc_configure: true
# must contain all proxmox instances that have a master or worker node
Expand Down
14 changes: 14 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dependency:
name: galaxy
driver:
name: vagrant
provision: true
platforms:

- name: control1
Expand Down Expand Up @@ -70,6 +71,19 @@ platforms:
- network_name: private_network
ip: 192.168.30.42

- name: node3
box: generic/alpine317
instance_raw_config_args:
- 'vm.provision :shell, inline: "apk add --update --no-cache python3"'
memory: 2048
cpus: 2
groups:
- k3s_cluster
- node
interfaces:
- network_name: private_network
ip: 192.168.30.43

provisioner:
name: ansible
playbooks:
Expand Down
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 }}"
96 changes: 14 additions & 82 deletions roles/k3s/master/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
---

- name: Clean previous runs of k3s-init
systemd:
name: k3s-init
state: stopped
failed_when: false
- name: Download install script
ansible.builtin.get_url:
url: https://get.k3s.io
dest: "{{ ansible_user_dir }}/install_k3s.sh"
mode: 744

- name: Clean previous runs of k3s-init
command: systemctl reset-failed k3s-init
- name: Install k3s
command:
cmd: "./install_k3s.sh {{ server_init_args }}"
environment:
INSTALL_K3S_VERSION: "{{ k3s_version }}"
K3S_TOKEN: "{{ k3s_token }}"
failed_when: false
changed_when: false
args:
warn: false # The ansible systemd module does not support reset-failed

- 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 +31,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 +68,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.

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

- 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('') }}"
environment:
INSTALL_K3S_VERSION: "{{ k3s_version }}"
K3S_TOKEN: "{{ k3s_token }}"
K3S_URL: https://{{ apiserver_endpoint | ansible.utils.ipwrap }}:6443
24 changes: 0 additions & 24 deletions roles/k3s/node/templates/k3s.service.j2

This file was deleted.

Loading

0 comments on commit e6e97ea

Please sign in to comment.