Postgre Cluster Replication
Postgre Cluster Replication
4.1. Setup
Before setting up the cluster, stop the database on both nodes and turn on the appropriate
firewalls:
Set the password for the 'hacluster’ user which is created when pacemaker is installed. Be sure
the hacluster password is the same on both systems.
#passwd hacluster
Run the following commands on each database system. Pick a cluster name that is unique.
#NODE1=NODE1_HOSTNAME
#NODE2=NODE2_HOSTNAME
#CLUSTER_NAME=CLUSTER_NAME
Run this command to setup authorization to pcsd on both nodes. It will prompt for the password:
#pcs status
Next, configure fencing to allow a surviving cluster node to forcibly remove a non-responsive
node from the cluster.
Note
For the reference environment VMware components are used as the virtualization provider. If
using Red Hat technologies, the following specific VMware configuration is not used. Refer
to Red Hat Enterprise Linux 7: High Availability Add-on Administration.[5] for additional
information on fencing configuration and setup.
Check that both database systems can access the SOAP API of the vCenter they are running on.
The vCenter user needs to be able to power virtual machines on and off.
Add vCenter as a fence device to the cluster. This only needs to be run on the first database
system:
On both database systems, verify that fencing works. The following command
reboots NODE2 when run on NODE1 .
After each of the systems comes back, verify that the following command shows both cluster
nodes as online, and the fence_vmware_soap agent as started:
#pcs status
Add the virtual IP address that the CloudForms appliances will use to connect to the primary
database.
Next, create the resource for the cluster to run the PostgreSQL database. Due to using a newer
PostgreSQL version from the Red Hat Software Collections channel, create two custom scripts
on both database systems:
/usr/local/bin/pg_ctl:
#!/bin/bash
EOF
/usr/local/bin/psql:
#!/bin/bash
EOF
#chmod 755 /usr/local/bin/{pg_ctl,psql}
On the NODE1 , create the resource for the cluster to manage the PostgreSQL service.
Replace REPLICATOR_PASSWORD and REPLICATION_VIP with the actual values.
Set low timeout values for the monitor operations to speed up the master/slave negotiation when
the resource is started.
Configure the cluster to keep the replication and service IP address on the same cluster node as
the primary database.
Replace the postgresql resource agent file to support the pglogical failover. This new resource
agent will run the syncsubs method every 10 minutes as defined in the resource configuration.
Syncsubs reads from the pglogical tables for the subscription information and writes it to a target
location which is either a local file or remote-by-ssh location. When a failure occurs, the
subscription information is already available to the new master and it will read in and recreate
the subscription.
#pcs status
This should show that the database is running as master on one node, and as slave on the other
node. If it shows that the database is stopped on both nodes and provides "failed actions" details,
run the following to help diagnose the issue:
To do this, create a script in /etc/cron.daily on both nodes. The find command at the end of the
script is run as the postgres user as in production, the wal-archive directory is an NFS mount and
not readable by the root user.
/etc/cron.daily/pgsql-replication:
Expand
#!/bin/bash
# Delete archived WAL files after N days. This assumes that we take a full
backup more often than that.
set -eu
# Exit if we are on the standby node. We only need to run the delete command
once, and the primary node is the one who # writes the files.
if [ -e /var/opt/rh/rh-postgresql94/lib/pgsql/data/recovery.conf ]; then
exit 0
fi
# The number of days after which archived WAL files will be deleted.
MAX_AGE=3
EOF
chmod +x /etc/cron.daily/pgsql-replication
4.1.1. Operations
If that node is the primary database, the remaining node should automatically switch from
standby to primary.
To re-enable the node to host cluster resources:
The pgsql cluster resource agent uses a lock file to track clean shutdowns of the primary/standby
combo and will refuse to make a node the primary if the lock file is there.
This lock file is only deleted when the standby is stopped first, and then the primary is stopped.
If the primary is stopped first, or is fenced, and the standby takes over, the lock file is not
deleted.
In reference environment, this precaution is not necessary because the Write Ahead Logs are
archived. If PostgreSQL remains in a “Stopped” state on a node and the "Failed actions" list
shows "unknown error", execute to help diagnose the issue: