mirror of
https://github.com/khuedoan/homelab.git
synced 2025-01-03 21:40:34 +07:00
Add k3s role
This commit is contained in:
parent
7ffb711f03
commit
92387435c8
@ -8,3 +8,8 @@
|
||||
gather_facts: no
|
||||
roles:
|
||||
- wake
|
||||
|
||||
- name: Create Kubernetes cluster
|
||||
hosts: metal
|
||||
roles:
|
||||
- k3s
|
||||
|
4
metal/roles/k3s/defaults/main.yml
Normal file
4
metal/roles/k3s/defaults/main.yml
Normal file
@ -0,0 +1,4 @@
|
||||
k3s_version: v1.21.4+k3s1
|
||||
k3s_config_file: /etc/rancher/k3s/config.yaml
|
||||
k3s_token_file: /etc/rancher/node/password
|
||||
k3s_service_file: /etc/systemd/system/k3s.service
|
68
metal/roles/k3s/tasks/main.yml
Normal file
68
metal/roles/k3s/tasks/main.yml
Normal file
@ -0,0 +1,68 @@
|
||||
- name: Download k3s binary
|
||||
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
|
||||
|
||||
- name: Ensure config directories exist
|
||||
file:
|
||||
path: "{{ item }}"
|
||||
state: directory
|
||||
loop:
|
||||
- /etc/rancher/k3s
|
||||
- /etc/rancher/node
|
||||
|
||||
- name: Check if k3s token file exists on the first node
|
||||
run_once: yes
|
||||
stat:
|
||||
path: "{{ k3s_token_file }}"
|
||||
register: k3s_token_file_stat
|
||||
|
||||
- name: Generate k3s token file on the first node if not exist yet
|
||||
run_once: yes
|
||||
when: not k3s_token_file_stat.stat.exists
|
||||
copy:
|
||||
content: lookup('community.general.random_string', length=32)
|
||||
dest: "{{ k3s_token_file }}"
|
||||
|
||||
- name: Get k3s token from the first node
|
||||
run_once: yes
|
||||
slurp:
|
||||
src: "{{ k3s_token_file }}"
|
||||
register: k3s_token_base64
|
||||
|
||||
- name: Ensure all nodes has the same token
|
||||
copy:
|
||||
content: "{{ k3s_token_base64.content | b64decode }}"
|
||||
dest: "{{ k3s_token_file }}"
|
||||
|
||||
- name: Copy k3s config files to master nodes
|
||||
when: "'masters' in group_names"
|
||||
template:
|
||||
src: "{{ item.src }}"
|
||||
dest: "{{ item.dest }}"
|
||||
vars:
|
||||
node_role: server
|
||||
loop:
|
||||
- src: config.yaml.j2
|
||||
dest: "{{ k3s_config_file }}"
|
||||
- src: k3s.service.j2
|
||||
dest: "{{ k3s_service_file }}"
|
||||
|
||||
- name: Copy k3s config files to worker nodes
|
||||
when: "'workers' in group_names"
|
||||
template:
|
||||
src: "{{ item.src }}"
|
||||
dest: "{{ item.dest }}"
|
||||
vars:
|
||||
node_role: agent
|
||||
loop:
|
||||
- src: config.yaml.j2
|
||||
dest: "{{ k3s_config_file }}"
|
||||
- src: k3s.service.j2
|
||||
dest: "{{ k3s_service_file }}"
|
||||
|
||||
# Get kubeconfig /etc/rancher/k3s/k3s.yaml
|
1
metal/roles/k3s/templates/config.yaml.j2
Normal file
1
metal/roles/k3s/templates/config.yaml.j2
Normal file
@ -0,0 +1 @@
|
||||
{{ node_role }}
|
24
metal/roles/k3s/templates/k3s.service.j2
Normal file
24
metal/roles/k3s/templates/k3s.service.j2
Normal file
@ -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 {{ node_role }}
|
||||
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…
Reference in New Issue
Block a user