Commit 3030e32c authored by Julien DOCHE's avatar Julien DOCHE
Browse files

Support HA mode with embedded DB

This enables initializing a cluster in HA mode with an embedded DB.
https://rancher.com/docs/k3s/latest/en/installation/ha-embedded/



When multiple masters are specified in the master group, k3s-ansible will add
the necessary flags during the initialization phase.
(i.e. --cluster-init and --server)

For the embedded HA mode to work the k3s version must be >= v1.19.1

Signed-off-by: default avatarJulien DOCHE <julien.doche@gmail.com>
parent f1163ffc
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ Master and nodes must have passwordless SSH access
First create a new directory based on the `sample` directory within the `inventory` directory:

```bash
pip install -r requirements.txt
cp -R inventory/sample inventory/my-cluster
```

@@ -43,6 +44,10 @@ master
node
```

If multiple hosts are in the master group, the playbook will automatically setup k3s in HA mode with etcd.
https://rancher.com/docs/k3s/latest/en/installation/ha-embedded/
This requires at least k3s version 1.19.1

If needed, you can also edit `inventory/my-cluster/group_vars/all.yml` to match your environment.

Start provisioning of the cluster using the following command:

requirements.txt

0 → 100644
+1 −0
Original line number Diff line number Diff line
jmespath
+11 −0
Original line number Diff line number Diff line
---
ansible_user: root
server_init_args: >-
  {% if groups['master'] | length > 1 %}
    {% if ansible_host == groups['master'][0] %}
      --cluster-init
    {% else %}
      --server https://{{ groups['master'][0] }}:6443
    {% endif %}
  {% endif %}
  {{ extra_server_args | default('') }}
+43 −0
Original line number Diff line number Diff line
---
- name: Clean previous runs of k3s-init
  systemd:
    name: k3s-init
    state: stopped
  failed_when: false

- name: Clean previous runs of k3s-init
  command: systemctl reset-failed k3s-init
  failed_when: false
  changed_when: false
  args:
    warn: false  # The ansible systemd module does not support reset-failed

- name: Init cluster inside the transient k3s-init service
  command:
    cmd: "systemd-run -p RestartSec=2i \
                      -p Restart=on-failure \
                      -E K3S_TOKEN={{ hostvars[groups['master'][0]]['token'] }} \
                      --unit=k3s-init \
                      k3s server {{ server_init_args }}"
    creates: "{{ systemd_dir }}/k3s.service"
  args:
    warn: false  # The ansible systemd module does not support transient units

- name: Verification
  block:
    - name: Verify that all nodes actually joined
      command:
        cmd: k3s kubectl get --raw /api/v1/nodes/
        creates: "{{ systemd_dir }}/k3s.service"
      register: nodes
      until: nodes.rc == 0 and
        ((nodes.stdout | from_json)['items'] |
        json_query('[*].metadata.labels."node-role.kubernetes.io/master"') | count) == (groups['master'] | length)
      retries: 20
      delay: 10
      changed_when: false
  always:
    - name: Kill the temporary service used for initialization
      systemd:
        name: k3s-init
        state: stopped
      failed_when: false

- name: Copy K3s service file
  register: k3s_service
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
  with_items:
    - k3s
    - k3s-node
    - k3s-init

- name: pkill -9 -f "k3s/data/[^/]+/bin/containerd-shim-runc"
  register: pkill_containerd_shim_runc