🚀 Manage Galera

pull/46/head
Emilien Mantel 2024-08-27 11:24:51 +02:00
parent 6eeb28133e
commit 174799ee17
12 changed files with 111 additions and 62 deletions

View File

@ -14,6 +14,7 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
scenario: scenario:
- debian12_galera
- debian12_master_slave - debian12_master_slave
- debian12_upstream - debian12_upstream

View File

@ -121,7 +121,7 @@ mariadb_upstream_version: '11.4'
# Galera # Galera
# ------------------------------------- # -------------------------------------
mariadb_galera_resetup: false 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_members: []
mariadb_galera_primary_node: 'change_me' # See: https://github.com/ansible/ansible/issues/17453 mariadb_galera_primary_node: 'change_me' # See: https://github.com/ansible/ansible/issues/17453
mariadb_wsrep_sst_method: 'rsync' mariadb_wsrep_sst_method: 'rsync'

View File

@ -5,3 +5,4 @@
name: "{{ mariadb_service_name }}" name: "{{ mariadb_service_name }}"
state: restarted state: restarted
when: mariadb_notify_restart when: mariadb_notify_restart
throttle: 1

View File

@ -2,3 +2,4 @@
mariadb_use_galera: true mariadb_use_galera: true
mariadb_galera_members: "{{ groups['galera'] }}" mariadb_galera_members: "{{ groups['galera'] }}"
mariadb_galera_primary_node: "{{ mariadb_galera_members | first }}"

View File

@ -0,0 +1,9 @@
---
- name: Converge
hosts: all
gather_facts: true
tasks:
- name: Include role
ansible.builtin.include_role:
name: "hanxhx.mysql"

View File

@ -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'

View File

@ -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"

View File

@ -1,4 +1,5 @@
--- ---
- name: SET_FACT | Prepare mark var - name: SET_FACT | Prepare mark var
ansible.builtin.set_fact: ansible.builtin.set_fact:
__mark: "{{ mariadb_datadir }}/.ansible_galera_boostrap" __mark: "{{ mariadb_datadir }}/.ansible_galera_boostrap"

View File

@ -2,31 +2,20 @@
- name: APT | Install Galera - name: APT | Install Galera
ansible.builtin.apt: ansible.builtin.apt:
pkg: "{{ mariadb_galera_package_name }}" name: "{{ mariadb_galera_package_name }}"
- name: TEMPLATE | Deploy Galera configuration - name: TEMPLATE | Deploy Galera configuration
ansible.builtin.template: ansible.builtin.template:
src: etc/mysql/mariadb.conf.d/20-galera.cnf.j2 src: etc/mysql/mariadb.conf.d/60-galera.cnf.j2
dest: /etc/mysql/mariadb.conf.d/20-galera.cnf dest: /etc/mysql/mariadb.conf.d/60-galera.cnf
mode: 0644 mode: 0644
owner: root owner: root
group: root group: root
register: galeraconfig notify: Restart mariadb
- name: INCLUDE | Bootstrap first node - name: INCLUDE | Bootstrap first node
ansible.builtin.import_tasks: 'bootstrap.yml' ansible.builtin.import_tasks: 'bootstrap.yml'
when: inventory_hostname == mariadb_galera_primary_node when: inventory_hostname == mariadb_galera_primary_node
- name: INCLUDE | Configure other nodes - name: META | Flush handlers
ansible.builtin.import_tasks: 'nodes.yml' ansible.builtin.meta: flush_handlers
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))

View File

@ -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

View File

@ -36,6 +36,7 @@
- etc/mysql/conf.d/mysqldump.cnf.j2 - etc/mysql/conf.d/mysqldump.cnf.j2
- etc/mysql/mariadb.conf.d/10-extra.cnf.j2 - etc/mysql/mariadb.conf.d/10-extra.cnf.j2
- etc/mysql/mariadb.conf.d/50-server.cnf.j2 - etc/mysql/mariadb.conf.d/50-server.cnf.j2
notify: Restart mariadb
- name: FILE | Ensure log dir exists - name: FILE | Ensure log dir exists
ansible.builtin.file: ansible.builtin.file:
@ -45,14 +46,8 @@
owner: mysql owner: mysql
group: mysql group: mysql
- name: SERVICE | Restart Mariadb now one at a time (prevent bugs) - name: META | Flush handlers
throttle: 1 ansible.builtin.meta: flush_handlers
ansible.builtin.service:
name: "{{ mariadb_service_name }}"
state: restarted
when:
config.changed and
not mariadb_galera_resetup
- name: SERVICE | Ensure service is started - name: SERVICE | Ensure service is started
ansible.builtin.service: ansible.builtin.service:

View File

@ -12,6 +12,7 @@
# Global # Global
wsrep_on = ON wsrep_on = ON
wsrep_provider = /usr/lib/galera/libgalera_smm.so
wsrep_cluster_address = gcomm://{{ mariadb_galera_members | join(",") }} wsrep_cluster_address = gcomm://{{ mariadb_galera_members | join(",") }}
wsrep_sst_method = "{{ mariadb_wsrep_sst_method }}" wsrep_sst_method = "{{ mariadb_wsrep_sst_method }}"
{% if mariadb_wsrep_cluster_name is defined %} {% if mariadb_wsrep_cluster_name is defined %}
@ -19,7 +20,7 @@ wsrep_cluster_name = "{{ mariadb_wsrep_cluster_name }}"
{% endif %} {% endif %}
# Node Configuration # 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) }}" wsrep_node_name = "{{ mariadb_wsrep_node_name | default(ansible_fqdn) }}"
# Deps # Deps