Si repasamos las entradas anteriores Parte1 y Parte2, vemos que tenemos configurado el repmgr pero no hemos activado el demonio para que haga el chequeo continuo.

Para ello necesitamos activar el servicio a nivel de SO y cargar su librería en postgresql.

En el fichero de postgresql.conf añadimos la siguiente entrada, en todos los nodos, y lo reiniciamos:

shared_preload_libraries = 'repmgr'

[postgres@postgres1 ~]$ pg_ctl -D /var/lib/pgsql/13/data reload
server signaled

 

Además, en este ejemplo, vamos a configurar un testigo ajeno a los propios servidores que dirigirá las operaciones.

Activamos el demonio del repmgr en el SO de todos los nodos:

[root@postgres1 ~]# systemctl enable repmgr13
Created symlink /etc/systemd/system/multi-user.target.wants/repmgr13.service → /usr/lib/systemd/system/repmgr13.service.

[root@postgres1 ~]# systemctl start repmgr13

[root@postgres1 ~]# systemctl status repmgr13
● repmgr13.service - A replication manager, and failover management tool for PostgreSQL
Loaded: loaded (/usr/lib/systemd/system/repmgr13.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-01-08 13:21:28 CET; 4s ago
Process: 1541 ExecStart=/usr/pgsql-13/bin/repmgrd -f ${REPMGRDCONF} -p ${PIDFILE} -d --verbose (code=exited, status=0/SUCCESS)
Main PID: 1547 (repmgrd)
Tasks: 1 (limit: 5967)
Memory: 1.5M
CGroup: /system.slice/repmgr13.service
└─1547 /usr/pgsql-13/bin/repmgrd -f /etc/repmgr/13/repmgr.conf -p /var/run/repmgr/repmgrd-13.pid -d --verbose

ene 08 13:21:28 postgres1 systemd[1]: Starting A replication manager, and failover management tool for PostgreSQL...
ene 08 13:21:28 postgres1 repmgrd[1541]: [2021-01-08 13:21:28] [NOTICE] using provided configuration file "/etc/repmgr/13/repmgr.conf"
ene 08 13:21:28 postgres1 repmgrd[1541]: [2021-01-08 13:21:28] [NOTICE] redirecting logging output to "/var/lib/pgsql/13/repmgr.log"
ene 08 13:21:28 postgres1 systemd[1]: repmgr13.service: Can't open PID file /var/run/repmgr/repmgrd-13.pid (yet?) after start: No such file or directory
ene 08 13:21:28 postgres1 systemd[1]: Started A replication manager, and failover management tool for PostgreSQL.

[postgres@postgres1 ~]$ repmgr daemon status
ID | Name | Role | Status | Upstream | repmgrd | PID | Paused? | Upstream last seen
----+-----------+---------+-----------+-----------+-------------+------+---------+--------------------
1 | postgres1 | primary | * running | | running | 1547 | no | n/a
2 | postgres2 | standby | running | postgres1 | not running | n/a | n/a | n/a
3 | postgres3 | standby | running | postgres1 | not running | n/a | n/a | n/a

 

Se podría cambiar la prioridad de cada servidor en el fichero /etc/repmgr/13/repmgr.conf y así prevalecer unos servidores ante otros, en caso de failover (server1–>priority=80, server2–>priority=60, server3–>priority=40).

Añadimos las siguientes entradas al fichero /etc/repmgr/13/repmgr.conf:

failover='automatic'
repmgrd_service_start_command='sudo /bin/systemctl start repmgr13'
repmgrd_service_stop_command='sudo /bin/systemctl stop repmgr13'
standby_disconnect_on_failover=true
monitoring_history=yes
log_status_interval=60

 

Reiniciamos el demonio del repmgr en todos los nodos:

[postgres@postgres1 ~]$ repmgr daemon stop
NOTICE: executing: "sudo /bin/systemctl stop repmgr13"
NOTICE: repmgrd was successfully stopped
[postgres@postgres1 ~]$ repmgr daemon start
NOTICE: executing: "sudo /bin/systemctl start repmgr13"
NOTICE: repmgrd was successfully started

 

Y comprobamos su estado:

[postgres@postgres1 ~]$ repmgr service status
ID | Name | Role | Status | Upstream | repmgrd | PID | Paused? | Upstream last seen
----+-----------+---------+-----------+-----------+---------+------+---------+--------------------
1 | postgres1 | primary | * running | | running | 3321 | no | n/a
2 | postgres2 | standby | running | postgres1 | running | 2241 | no | 0 second(s) ago
3 | postgres3 | standby | running | postgres1 | running | 2050 | no | 1 second(s) ago

 

En el nodo «testigo» (postgres4), inicializamos postgres y creamos su base de datos para repmgr:

[root@postgres4 ~]# /usr/pgsql-13/bin/postgresql-13-setup initdb
Initializing database ... OK

[root@postgres4 ~]# systemctl start postgresql-13

[postgres@postgres4 ~]$ psql -c 'CREATE USER repmgr WITH REPLICATION SUPERUSER LOGIN';
CREATE ROLE
[postgres@postgres4 ~]$ psql -c 'CREATE DATABASE repmgr OWNER repmgr';
CREATE DATABASE
[postgres@postgres4 ~]$ psql -c 'ALTER USER repmgr SET search_path TO repmgr, "$user", public';
ALTER ROLE

 

Registramos el nodo «testigo» en el cluster contra el primary (postgres1):

[postgres@postgres4 ~]$ repmgr witness register -h 192.168.0.101
INFO: connecting to witness node "postgres4-witness" (ID: 4)
INFO: connecting to primary node
NOTICE: attempting to install extension "repmgr"
NOTICE: "repmgr" extension successfully installed
INFO: witness registration complete
NOTICE: witness node "postgres4-witness" (ID: 4) successfully registered

 

Y el cluster queda de la siguiente manera:

[postgres@postgres1 ~]$ repmgr cluster show
ID | Name | Role | Status | Upstream | Location | Priority | Timeline | Connection string
----+-------------------+---------+-----------+-----------+----------+----------+----------+----------------------------------------------
1 | postgres1 | primary | * running | | default | 100 | 3 | host=192.168.0.101 user=repmgr dbname=repmgr
2 | postgres2 | standby | running | postgres1 | default | 100 | 3 | host=192.168.0.102 user=repmgr dbname=repmgr
3 | postgres3 | standby | running | postgres1 | default | 100 | 3 | host=192.168.0.103 user=repmgr dbname=repmgr
4 | postgres4-witness | witness | * running | postgres1 | default | 0 | n/a | host=192.168.0.104 user=repmgr dbname=repmgr

 

Ahora vamos a la parte 2…