From 174799ee17cfedf7a116f6d3b77c9510e34c5ed2 Mon Sep 17 00:00:00 2001 From: Emilien Mantel Date: Tue, 27 Aug 2024 11:24:51 +0200 Subject: [PATCH] :rocket: Manage Galera --- .github/workflows/molecule.yml | 1 + defaults/main.yml | 2 +- handlers/main.yml | 1 + .../_shared/inventory/group_vars/galera.yml | 1 + molecule/debian12_galera/converge.yml | 9 +++ molecule/debian12_galera/molecule.yml | 55 +++++++++++++++++++ molecule/debian12_galera/verify.yml | 31 +++++++++++ tasks/galera/bootstrap.yml | 1 + tasks/galera/main.yml | 23 ++------ tasks/galera/nodes.yml | 35 ------------ tasks/main.yml | 11 +--- .../etc/mysql/mariadb.conf.d/60-galera.cnf.j2 | 3 +- 12 files changed, 111 insertions(+), 62 deletions(-) create mode 100644 molecule/debian12_galera/converge.yml create mode 100644 molecule/debian12_galera/molecule.yml create mode 100644 molecule/debian12_galera/verify.yml delete mode 100644 tasks/galera/nodes.yml diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml index e5cd7b3..bd927b5 100644 --- a/.github/workflows/molecule.yml +++ b/.github/workflows/molecule.yml @@ -14,6 +14,7 @@ jobs: fail-fast: false matrix: scenario: + - debian12_galera - debian12_master_slave - debian12_upstream diff --git a/defaults/main.yml b/defaults/main.yml index efddbea..68ece29 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -121,7 +121,7 @@ mariadb_upstream_version: '11.4' # Galera # ------------------------------------- mariadb_galera_resetup: false -mariadb_wsrep_node_address: false # Set primary node IP +mariadb_wsrep_node_address: '{{ ansible_default_ipv4.address if ansible_default_ipv4 is defined else "127.0.0.1" }}' mariadb_galera_members: [] mariadb_galera_primary_node: 'change_me' # See: https://github.com/ansible/ansible/issues/17453 mariadb_wsrep_sst_method: 'rsync' diff --git a/handlers/main.yml b/handlers/main.yml index 424bf17..8b2737c 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -5,3 +5,4 @@ name: "{{ mariadb_service_name }}" state: restarted when: mariadb_notify_restart + throttle: 1 diff --git a/molecule/_shared/inventory/group_vars/galera.yml b/molecule/_shared/inventory/group_vars/galera.yml index 1b540db..df99016 100644 --- a/molecule/_shared/inventory/group_vars/galera.yml +++ b/molecule/_shared/inventory/group_vars/galera.yml @@ -2,3 +2,4 @@ mariadb_use_galera: true mariadb_galera_members: "{{ groups['galera'] }}" +mariadb_galera_primary_node: "{{ mariadb_galera_members | first }}" diff --git a/molecule/debian12_galera/converge.yml b/molecule/debian12_galera/converge.yml new file mode 100644 index 0000000..1d52e7f --- /dev/null +++ b/molecule/debian12_galera/converge.yml @@ -0,0 +1,9 @@ +--- + +- name: Converge + hosts: all + gather_facts: true + tasks: + - name: Include role + ansible.builtin.include_role: + name: "hanxhx.mysql" diff --git a/molecule/debian12_galera/molecule.yml b/molecule/debian12_galera/molecule.yml new file mode 100644 index 0000000..026fb09 --- /dev/null +++ b/molecule/debian12_galera/molecule.yml @@ -0,0 +1,55 @@ +--- + +platforms: + - name: debian12-galera1 + image: dokken/debian-12 + command: /lib/systemd/systemd + dockerfile: ../_shared/Dockerfile.j2 + capabilities: + - SYS_ADMIN + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + groups: + - galera + networks: + - name: "00-molecule-d12-galera" + ipv4_address: '172.16.51.1' + docker_networks: + - name: "00-molecule-d12-galera" + ipam_config: + - subnet: "172.16.51.0/24" + gateway: "172.16.51.254" + + - name: debian12-galera2 + image: dokken/debian-12 + command: /lib/systemd/systemd + dockerfile: ../_shared/Dockerfile.j2 + capabilities: + - SYS_ADMIN + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + groups: + - galera + networks: + - name: "00-molecule-d12-galera" + ipv4_address: '172.16.51.2' + + - name: debian12-galera3 + image: dokken/debian-12 + command: /lib/systemd/systemd + dockerfile: ../_shared/Dockerfile.j2 + capabilities: + - SYS_ADMIN + cgroupns_mode: host + volumes: + - /sys/fs/cgroup:/sys/fs/cgroup:rw + privileged: true + groups: + - galera + networks: + - name: "00-molecule-d12-galera" + ipv4_address: '172.16.51.3' diff --git a/molecule/debian12_galera/verify.yml b/molecule/debian12_galera/verify.yml new file mode 100644 index 0000000..31fc687 --- /dev/null +++ b/molecule/debian12_galera/verify.yml @@ -0,0 +1,31 @@ +--- + +- name: Verify Galera + hosts: galera + gather_facts: false + tasks: + - name: MYSQL_QUERY | Check if Galera is running + community.mysql.mysql_query: + query: "SHOW GLOBAL STATUS LIKE 'wsrep_ready'" + login_unix_socket: /run/mysqld/mysqld.sock + register: wsrep_ready + + - name: ASSERT | Fail if Galera is not running + ansible.builtin.assert: + that: + - wsrep_ready.query_result.0.0.Value == "ON" + fail_msg: "Galera is not running" + success_msg: "Galera is running" + + - name: MYSQL_QUERY | Check Galera status + community.mysql.mysql_query: + query: "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment'" + login_unix_socket: /run/mysqld/mysqld.sock + register: wsrep_local_state_comment + + - name: ASSERT | Fail if Galera is not on the right state + ansible.builtin.assert: + that: + - wsrep_local_state_comment.query_result.0.0.Value == "Synced" + fail_msg: "Galera is not expected state ({{ wsrep_local_state_comment.query_result.0.0.Value }})" + success_msg: "Galera is in expected state" diff --git a/tasks/galera/bootstrap.yml b/tasks/galera/bootstrap.yml index 011a892..28704c4 100644 --- a/tasks/galera/bootstrap.yml +++ b/tasks/galera/bootstrap.yml @@ -1,4 +1,5 @@ --- + - name: SET_FACT | Prepare mark var ansible.builtin.set_fact: __mark: "{{ mariadb_datadir }}/.ansible_galera_boostrap" diff --git a/tasks/galera/main.yml b/tasks/galera/main.yml index 6458516..02f0279 100644 --- a/tasks/galera/main.yml +++ b/tasks/galera/main.yml @@ -2,31 +2,20 @@ - name: APT | Install Galera ansible.builtin.apt: - pkg: "{{ mariadb_galera_package_name }}" + name: "{{ mariadb_galera_package_name }}" - name: TEMPLATE | Deploy Galera configuration ansible.builtin.template: - src: etc/mysql/mariadb.conf.d/20-galera.cnf.j2 - dest: /etc/mysql/mariadb.conf.d/20-galera.cnf + src: etc/mysql/mariadb.conf.d/60-galera.cnf.j2 + dest: /etc/mysql/mariadb.conf.d/60-galera.cnf mode: 0644 owner: root group: root - register: galeraconfig + notify: Restart mariadb - name: INCLUDE | Bootstrap first node ansible.builtin.import_tasks: 'bootstrap.yml' when: inventory_hostname == mariadb_galera_primary_node -- name: INCLUDE | Configure other nodes - ansible.builtin.import_tasks: 'nodes.yml' - when: inventory_hostname != mariadb_galera_primary_node - -- name: SERVICE | Restart MariaDB if needed - ansible.builtin.service: - name: "{{ mariadb_service_name }}" - state: restarted - when: - ((galeraconfig or (p is defined and p.changed)) and - (bootstrap_run is not defined)) or - ((inventory_hostname != mariadb_galera_primary_node) and - (mariadb_galera_resetup)) +- name: META | Flush handlers + ansible.builtin.meta: flush_handlers diff --git a/tasks/galera/nodes.yml b/tasks/galera/nodes.yml deleted file mode 100644 index 71f232b..0000000 --- a/tasks/galera/nodes.yml +++ /dev/null @@ -1,35 +0,0 @@ ---- - -- name: COMMAND | GET debian.cnf from primary node - ansible.builtin.command: cat /etc/mysql/debian.cnf - register: debiancnf - delegate_to: '{{ mariadb_galera_primary_node }}' - changed_when: false - -- name: COMMAND | Get current debian.cnf - ansible.builtin.command: cat /etc/mysql/debian.cnf - register: ondc - changed_when: false - -- name: BLOCK | Compare debian.cnf - when: debiancnf.stdout != ondc.stdout - block: - - - name: SERVICE | Stop MariaDB - ansible.builtin.service: - name: "{{ mariadb_service_name }}" - state: stopped - - - name: COPY | Paste primary node's debian.cnf - ansible.builtin.copy: - content: "{{ debiancnf.stdout }}" - dest: /etc/mysql/debian.cnf - mode: 0600 - owner: root - group: root - register: paste - - - name: SERVICE | Start MariaDB - ansible.builtin.service: - name: "{{ mariadb_service_name }}" - state: started diff --git a/tasks/main.yml b/tasks/main.yml index f1a0874..e376dc5 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -36,6 +36,7 @@ - etc/mysql/conf.d/mysqldump.cnf.j2 - etc/mysql/mariadb.conf.d/10-extra.cnf.j2 - etc/mysql/mariadb.conf.d/50-server.cnf.j2 + notify: Restart mariadb - name: FILE | Ensure log dir exists ansible.builtin.file: @@ -45,14 +46,8 @@ owner: mysql group: mysql -- name: SERVICE | Restart Mariadb now one at a time (prevent bugs) - throttle: 1 - ansible.builtin.service: - name: "{{ mariadb_service_name }}" - state: restarted - when: - config.changed and - not mariadb_galera_resetup +- name: META | Flush handlers + ansible.builtin.meta: flush_handlers - name: SERVICE | Ensure service is started ansible.builtin.service: diff --git a/templates/etc/mysql/mariadb.conf.d/60-galera.cnf.j2 b/templates/etc/mysql/mariadb.conf.d/60-galera.cnf.j2 index e351c24..f12df34 100644 --- a/templates/etc/mysql/mariadb.conf.d/60-galera.cnf.j2 +++ b/templates/etc/mysql/mariadb.conf.d/60-galera.cnf.j2 @@ -12,6 +12,7 @@ # Global wsrep_on = ON +wsrep_provider = /usr/lib/galera/libgalera_smm.so wsrep_cluster_address = gcomm://{{ mariadb_galera_members | join(",") }} wsrep_sst_method = "{{ mariadb_wsrep_sst_method }}" {% if mariadb_wsrep_cluster_name is defined %} @@ -19,7 +20,7 @@ wsrep_cluster_name = "{{ mariadb_wsrep_cluster_name }}" {% endif %} # Node Configuration -wsrep_node_address = "{{ mariadb_wsrep_node_address | default(ansible_default_ipv4.address if ansible_default_ipv4 is defined else '127.0.0.1') }}" +wsrep_node_address = "{{ mariadb_wsrep_node_address }}" wsrep_node_name = "{{ mariadb_wsrep_node_name | default(ansible_fqdn) }}" # Deps