POC: Supporting k3s-ansible with external database (#339)

* POC: Supporting k3s-ansible with external database

Signed-off-by: Peter Klijn <pjmklijn@gmail.com>
This commit is contained in:
Peter Klijn 2024-07-02 22:34:34 +02:00 committed by GitHub
parent 91ee70ee17
commit 31b8b1edcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 3 deletions

View File

@ -63,6 +63,30 @@ Start provisioning of the cluster using the following command:
ansible-playbook playbooks/site.yml -i inventory.yml 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 ## 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: 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:

View File

@ -7,3 +7,4 @@ user_kubectl: true # noqa var-naming[no-role-prefix]
cluster_context: k3s-ansible # 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] server_group: server # noqa var-naming[no-role-prefix]
agent_group: agent # 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]

View File

@ -58,7 +58,7 @@
when: inventory_hostname == groups[server_group][0] when: inventory_hostname == groups[server_group][0]
block: block:
- name: Copy K3s service file [Single] - 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: ansible.builtin.template:
src: "k3s-single.service.j2" src: "k3s-single.service.j2"
dest: "{{ systemd_dir }}/k3s.service" dest: "{{ systemd_dir }}/k3s.service"
@ -67,7 +67,9 @@
mode: "0644" mode: "0644"
- name: Copy K3s service file [HA] - 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: ansible.builtin.template:
src: "k3s-cluster-init.service.j2" src: "k3s-cluster-init.service.j2"
dest: "{{ systemd_dir }}/k3s.service" dest: "{{ systemd_dir }}/k3s.service"
@ -148,7 +150,9 @@
- inventory_hostname != groups[server_group][0] - inventory_hostname != groups[server_group][0]
block: block:
- name: Copy K3s service file [HA] - 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: ansible.builtin.template:
src: "k3s-ha.service.j2" src: "k3s-ha.service.j2"
dest: "{{ systemd_dir }}/k3s.service" dest: "{{ systemd_dir }}/k3s.service"
@ -156,6 +160,17 @@
group: root group: root
mode: "0644" 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 - name: Enable and check K3s service
ansible.builtin.systemd: ansible.builtin.systemd:
name: k3s name: k3s