Merge pull request #4 from khuedoan/k3s

Switch to k3s
This commit is contained in:
Khue Doan 2021-09-01 09:26:59 +00:00 committed by GitHub
commit 4752762dcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 115 additions and 5 deletions

View File

@ -125,3 +125,4 @@ Distributed under the GPLv3 License. See `LICENSE` for more information.
- [README template](https://github.com/othneildrew/Best-README-Template)
- [Run the same Cloudflare Tunnel across many `cloudflared` processes](https://developers.cloudflare.com/cloudflare-one/tutorials/many-cfd-one-tunnel)
- [MAC address environment variable in GRUB config](https://askubuntu.com/questions/1272400/how-do-i-automate-network-installation-of-many-ubuntu-18-04-systems-with-efi-and)
- [Official k3s systemd service file](https://github.com/k3s-io/k3s/blob/master/k3s.service)

View File

@ -1,9 +1,13 @@
metal:
hosts:
# metal0: {ansible_host: 192.168.1.110, mac: '00:23:24:d1:f3:f0'}
metal1: {ansible_host: 192.168.1.111, mac: '00:23:24:d1:f4:d6'}
metal2: {ansible_host: 192.168.1.112, mac: '00:23:24:e7:04:60'}
metal3: {ansible_host: 192.168.1.113, mac: '00:23:24:d1:f5:69'}
children:
masters:
hosts:
# metal0: {ansible_host: 192.168.1.110, mac: '00:23:24:d1:f3:f0'}
metal1: {ansible_host: 192.168.1.111, mac: '00:23:24:d1:f4:d6'}
metal2: {ansible_host: 192.168.1.112, mac: '00:23:24:e7:04:60'}
workers:
hosts:
metal3: {ansible_host: 192.168.1.113, mac: '00:23:24:d1:f5:69'}
vars:
ansible_user: root
ansible_ssh_private_key_file: ~/.ssh/id_ed25519

View File

@ -8,3 +8,8 @@
gather_facts: no
roles:
- wake
- name: Create Kubernetes cluster
hosts: metal
roles:
- k3s

View 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

View File

@ -0,0 +1,66 @@
- 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
template:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
loop:
- src: config.yaml.j2
dest: "{{ k3s_config_file }}"
- src: k3s.service.j2
dest: "{{ k3s_service_file }}"
- name: Enable k3s service
systemd:
name: k3s
enabled: yes
state: started
register: k3s_service
until: k3s_service is succeeded
retries: 5
- name: Get Kubernetes config file
run_once: yes
fetch:
src: /etc/rancher/k3s/k3s.yaml
dest: "{{ playbook_dir }}/kubeconfig.yaml"
flat: yes

View File

@ -0,0 +1,6 @@
{% if inventory_hostname == groups['masters'][0] %}
cluster-init: true
{% else %}
server: https://{{ hostvars[groups['masters'][0]].ansible_host }}:6443
{% endif %}
token-file: {{ k3s_token_file }}

View 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 {{ 'server' if 'masters' in group_names else 'agent' }}
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