Skip to content

Commit

Permalink
add ansible k3s setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ztnel committed Apr 23, 2023
1 parent febc18e commit e133e8e
Show file tree
Hide file tree
Showing 28 changed files with 454 additions and 157 deletions.
1 change: 0 additions & 1 deletion ansible/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ become = True
host_key_checking = False
deprecation_warnings = False
callback_whitelist = profile_tasks
ansible_stdout_callback=debug
4 changes: 3 additions & 1 deletion ansible/inventory/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
inventory.yaml
*
!.gitignore
!sample
40 changes: 0 additions & 40 deletions ansible/inventory/example.inventory.yaml

This file was deleted.

19 changes: 19 additions & 0 deletions ansible/playbooks/k3s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---

- hosts: k3s_cluster
gather_facts: yes
become: yes
roles:
- role: prereq
- role: download
- role: raspberrypi

- hosts: master
become: yes
roles:
- role: k3s/master

- hosts: node
become: yes
roles:
- role: k3s/node
7 changes: 7 additions & 0 deletions ansible/playbooks/reset-k3s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---

- hosts: k3s_cluster
gather_facts: yes
become: yes
roles:
- role: reset
36 changes: 36 additions & 0 deletions ansible/roles/download/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---

- name: Download k3s binary x64
get_url:
url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s
checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-amd64.txt
dest: /usr/local/bin/k3s
owner: root
group: root
mode: 0755
when: ansible_facts.architecture == "x86_64"

- name: Download k3s binary arm64
get_url:
url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s-arm64
checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-arm64.txt
dest: /usr/local/bin/k3s
owner: root
group: root
mode: 0755
when:
- ( ansible_facts.architecture is search("arm") and
ansible_facts.userspace_bits == "64" ) or
ansible_facts.architecture is search("aarch64")

- name: Download k3s binary armhf
get_url:
url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s-armhf
checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-arm.txt
dest: /usr/local/bin/k3s
owner: root
group: root
mode: 0755
when:
- ansible_facts.architecture is search("arm")
- ansible_facts.userspace_bits == "32"
38 changes: 0 additions & 38 deletions ansible/roles/hostname/README.md

This file was deleted.

3 changes: 0 additions & 3 deletions ansible/roles/hostname/defaults/main.yml

This file was deleted.

38 changes: 0 additions & 38 deletions ansible/roles/hostname/meta/main.yml

This file was deleted.

27 changes: 0 additions & 27 deletions ansible/roles/hostname/tasks/main.yml

This file was deleted.

2 changes: 0 additions & 2 deletions ansible/roles/hostname/tests/inventory

This file was deleted.

5 changes: 0 additions & 5 deletions ansible/roles/hostname/tests/test.yml

This file was deleted.

2 changes: 0 additions & 2 deletions ansible/roles/hostname/vars/main.yml

This file was deleted.

2 changes: 2 additions & 0 deletions ansible/roles/k3s/master/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
k3s_server_location: /var/lib/rancher/k3s
79 changes: 79 additions & 0 deletions ansible/roles/k3s/master/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---

- 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: "{{ k3s_server_location }}/server/node-token"

- name: Register node-token file access mode
stat:
path: "{{ k3s_server_location }}/server/node-token"
register: p

- name: Change file access node-token
file:
path: "{{ k3s_server_location }}/server/node-token"
mode: "g+rx,o+rx"

- name: Read node-token from master
slurp:
path: "{{ k3s_server_location }}/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: "{{ k3s_server_location }}/server/node-token"
mode: "{{ p.stat.mode }}"

- name: Create directory .kube
file:
path: ~{{ ansible_user }}/.kube
state: directory
owner: "{{ ansible_user }}"
mode: "u=rwx,g=rx,o="

- name: Copy config file to user home directory
copy:
src: /etc/rancher/k3s/k3s.yaml
dest: ~{{ ansible_user }}/.kube/config
remote_src: yes
owner: "{{ ansible_user }}"
mode: "u=rw,g=,o="

- name: Replace https://localhost:6443 by https://master-ip:6443
command: >-
k3s kubectl config set-cluster default
--server=https://{{ master_ip }}:6443
--kubeconfig ~{{ ansible_user }}/.kube/config
changed_when: true

- 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
24 changes: 24 additions & 0 deletions ansible/roles/k3s/master/templates/k3s.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
After=network-online.target

[Service]
Type=notify
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ extra_server_args | default("") }}
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
16 changes: 16 additions & 0 deletions ansible/roles/k3s/node/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---

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

- name: Enable and check K3s service
systemd:
name: k3s-node
daemon_reload: yes
state: restarted
enabled: yes
24 changes: 24 additions & 0 deletions ansible/roles/k3s/node/templates/k3s.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[Unit]
Description=Lightweight Kubernetes
Documentation=https://k3s.io
After=network-online.target

[Service]
Type=notify
ExecStartPre=-/sbin/modprobe br_netfilter
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/bin/k3s agent --server https://{{ master_ip }}:6443 --token {{ hostvars[groups['master'][0]]['token'] }} {{ extra_agent_args | default("") }}
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=1048576
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
TimeoutStartSec=0
Restart=always
RestartSec=5s

[Install]
WantedBy=multi-user.target
Loading

0 comments on commit e133e8e

Please sign in to comment.