From 31b8b1edcf0d51a2bb1281ab64862196d04ea7af Mon Sep 17 00:00:00 2001 From: Peter Klijn Date: Tue, 2 Jul 2024 22:34:34 +0200 Subject: [PATCH] POC: Supporting k3s-ansible with external database (#339) * POC: Supporting k3s-ansible with external database Signed-off-by: Peter Klijn --- README.md | 24 ++++++++++++++++++++++++ roles/k3s_server/defaults/main.yml | 1 + roles/k3s_server/tasks/main.yml | 21 ++++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a88a9fa..c9dffbf 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,30 @@ Start provisioning of the cluster using the following command: ansible-playbook playbooks/site.yml -i inventory.yml ``` +### Using an external database + +If an external database is preferred, this can be achieved by passing the `--datastore-endpoint` as an extra server argument as well as setting the `use_external_database` flag to true. + +```bash +k3s_cluster: + children: + server: + hosts: + 192.16.35.11: + 192.16.35.12: + agent: + hosts: + 192.16.35.13: + + vars: + use_external_database: true + extra_server_args: "--datastore-endpoint=postgres://username:password@hostname:port/database-name" +``` + +The `use_external_database` flag is required when more than one server is defined, as otherwise an embedded etcd cluster will be created instead. + +The format of the datastore-endpoint parameter is dependent upon the datastore backend, please visit the [K3s datastore endpoint format](https://docs.k3s.io/datastore#datastore-endpoint-format-and-functionality) for details on the format and supported datastores. + ## Upgrading A playbook is provided to upgrade K3s on all nodes in the cluster. To use it, update `k3s_version` with the desired version in `inventory.yml` and run: diff --git a/roles/k3s_server/defaults/main.yml b/roles/k3s_server/defaults/main.yml index ed9245d..b19d692 100644 --- a/roles/k3s_server/defaults/main.yml +++ b/roles/k3s_server/defaults/main.yml @@ -7,3 +7,4 @@ user_kubectl: true # noqa var-naming[no-role-prefix] cluster_context: k3s-ansible # noqa var-naming[no-role-prefix] server_group: server # noqa var-naming[no-role-prefix] agent_group: agent # noqa var-naming[no-role-prefix] +use_external_database: false # noqa var-naming[no-role-prefix] diff --git a/roles/k3s_server/tasks/main.yml b/roles/k3s_server/tasks/main.yml index 0718a81..baaa9ab 100644 --- a/roles/k3s_server/tasks/main.yml +++ b/roles/k3s_server/tasks/main.yml @@ -58,7 +58,7 @@ when: inventory_hostname == groups[server_group][0] block: - name: Copy K3s service file [Single] - when: groups[server_group] | length == 1 + when: groups[server_group] | length == 1 or use_external_database ansible.builtin.template: src: "k3s-single.service.j2" dest: "{{ systemd_dir }}/k3s.service" @@ -67,7 +67,9 @@ mode: "0644" - name: Copy K3s service file [HA] - when: groups[server_group] | length > 1 + when: + - groups[server_group] | length > 1 + - not use_external_database ansible.builtin.template: src: "k3s-cluster-init.service.j2" dest: "{{ systemd_dir }}/k3s.service" @@ -148,7 +150,9 @@ - inventory_hostname != groups[server_group][0] block: - name: Copy K3s service file [HA] - when: groups[server_group] | length > 1 + when: + - groups[server_group] | length > 1 + - not use_external_database ansible.builtin.template: src: "k3s-ha.service.j2" dest: "{{ systemd_dir }}/k3s.service" @@ -156,6 +160,17 @@ group: root mode: "0644" + - name: Copy K3s service file [External DB] + when: + - groups[server_group] | length > 1 + - use_external_database + ansible.builtin.template: + src: "k3s-single.service.j2" + dest: "{{ systemd_dir }}/k3s.service" + owner: root + group: root + mode: "0644" + - name: Enable and check K3s service ansible.builtin.systemd: name: k3s