Skip to content

Commit

Permalink
POC: Supporting k3s-ansible with external database (#339)
Browse files Browse the repository at this point in the history
* POC: Supporting k3s-ansible with external database

Signed-off-by: Peter Klijn <[email protected]>
  • Loading branch information
peterklijn authored Jul 2, 2024
1 parent 91ee70e commit 31b8b1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions roles/k3s_server/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
21 changes: 18 additions & 3 deletions roles/k3s_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -148,14 +150,27 @@
- 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"
owner: root
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
Expand Down

0 comments on commit 31b8b1e

Please sign in to comment.