Skip to content

Commit

Permalink
Add custom configuration of the apiserver endpoint for HA mode
Browse files Browse the repository at this point in the history
This replaces the `master_ip` var by `apiserver_endpoint` for genericity. The
init service is deployed only when k3s.service is not present on the machine to
ensure idempotence.

Signed-off-by: Julien DOCHE <[email protected]>
  • Loading branch information
St0rmingBr4in committed Mar 1, 2021
1 parent 3030e32 commit bb0e3c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
10 changes: 9 additions & 1 deletion inventory/sample/group_vars/all.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
k3s_version: v1.17.5+k3s1
ansible_user: debian
systemd_dir: /etc/systemd/system
master_ip: "{{ hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) }}"

# If you define multiple masters you should be providing a loadbalanced
# apiserver endpoint to all masters here. This default value is only suitable
# for a non-HA setup, if used in a HA setup, it will not protect you if the
# first node fails.

apiserver_endpoint: "{{ hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) }}"

extra_server_args: ""
extra_agent_args: ""
k3s_token: ""
4 changes: 2 additions & 2 deletions roles/k3s/master/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
ansible_user: root
server_init_args: >-
{% if groups['master'] | length > 1 %}
{% if ansible_host == groups['master'][0] %}
{% if ansible_host == hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) %}
--cluster-init
{% else %}
--server https://{{ groups['master'][0] }}:6443
--server https://{{ hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) }}:6443
{% endif %}
{% endif %}
{{ extra_server_args | default('') }}
6 changes: 3 additions & 3 deletions roles/k3s/master/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

- name: Init cluster inside the transient k3s-init service
command:
cmd: "systemd-run -p RestartSec=2i \
cmd: "systemd-run -p RestartSec=2 \
-p Restart=on-failure \
-E K3S_TOKEN={{ hostvars[groups['master'][0]]['token'] }} \
-E K3S_TOKEN={{ k3s_token }} \
--unit=k3s-init \
k3s server {{ server_init_args }}"
creates: "{{ systemd_dir }}/k3s.service"
Expand Down Expand Up @@ -105,7 +105,7 @@
- name: Replace https://localhost:6443 by https://master-ip:6443
command: >-
k3s kubectl config set-cluster default
--server=https://{{ master_ip }}:6443
--server=https://{{ apiserver_endpoint }}:6443
--kubeconfig ~{{ ansible_user }}/.kube/config
changed_when: true

Expand Down
2 changes: 1 addition & 1 deletion roles/k3s/node/templates/k3s.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ After=network-online.target
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("") }}
ExecStart=/usr/local/bin/k3s agent --server https://{{ apiserver_endpoint }}:6443 --token {{ k3s_token }} {{ extra_agent_args | default("") }}
KillMode=process
Delegate=yes
# Having non-zero Limit*s causes performance problems due to accounting overhead
Expand Down

0 comments on commit bb0e3c9

Please sign in to comment.