So, we use PostgreSQL quite a bit. We actually use a fairly traditional replication setup - the whole of the data is pushed using zfs send and receive to a second system. Problem at the source? We just turn on the DR site, and we're done.
One of the reasons for that fairly traditional approach is that PostgreSQL has, historically, not had much built in support for replication. Or, at least, not in a simple and straightforward manner. But it's getting a lot better.
Many of the guides you'll find are rather dated, and show old, rather clunky, and quite laborious ways to set up replication. With current versions of PostgreSQL it's actually pretty trivial to get streaming replication running, so here's how to demo it if you're using Tribblix.
First set up a couple of zones. The idea is that pg1 is the master, pg2 the replica. Run, as root:
zap create-zone -z pg1 -t whole -o base -x 192.168.0.221 -U ptribble
zap create-zone -z pg2 -t whole -o base -x 192.168.0.222 -U ptribble
This simply creates a fairly basic zone, without much in the way of extraneous software installed. Adjust the IP addresses to suit, of course. And I've set them up so that I can use zlogin from my own account.
Then login to each zone, install postgres, and create a user.
zlogin pg1
zap install TRIBblix-postgres11 TRIBtext-locale
useradd -u 11799 -g staff -s /bin/bash -d /export/home/pguser pguser
passwd -N pguser
mkdir /export/home/pguser
chown -hR pguser /export/home/pguser
And the same for pg2.
Then log in to the master as pguser.
zlogin -l pguser pg1
Now initialise a database, and start it up:
env LANG=en_GB.UTF-8 /opt/tribblix/postgres11/bin/initdb -E UTF8 -D ~/db
env LANG=en_GB.UTF-8 /opt/tribblix/postgres11/bin/postgres -D ~/db
The next thing to do is create a PostgreSQL user that will run the streaming replication.
/opt/tribblix/postgres11/bin/psql -d postgres
CREATE ROLE replicate WITH REPLICATION LOGIN ;
set password_encryption = 'scram-sha-256';
SET
\password replicate
Enter new password: my_secret_password
Then you need to edit postgresql.conf (in the db directory) with the following settings:
listen_addresses = '*'And set up authentication so that the user you just created can actually access the database remotely, by adding the following line to pg_hba.conf
wal_level = replica
max_wal_senders = 3 # or whatever
wal_keep_segments = 64 # or whatever
hot_standby = on
host replication replicate 192.168.0.0/24 scram-sha-256
ideally we would use hostssl so the connection is encrypted, but that's out of scope for this example.
Then restart the master.
Now log in to the slave.
zlogin -l pguser pg2
And all you have to do to replicate the data is run pg_basebackup:
/opt/tribblix/postgres11/bin/pg_basebackup \
-h 192.168.0.221 -c fast -D ~/db -R -P \
-U replicate --wal-method=stream
It will prompt you for the super secret passord you entered earlier. Once that's completed you can start the slave:
env LANG=en_GB.UTF-8 /opt/tribblix/postgres11/bin/postgres -D ~/db
And that's it. Data you insert on the master will be written to the slave. In this mode, you can connect to the replica and issue queries to read the data, but you can't change anything.
Note that this doesn't do anything like manage failover or send client connections to the right place. It just makes sure the data is available. You need an extra layer of management to manage the master and slave(s) to actually get HA.
There are a whole variety of ways to do that, but the simplest way to test it is to stop the database on the master, which will make the slave unhappy. But if you know the master isn't coming back, you can promote the replica:
/opt/tribblix/postgres11/bin/pg_ctl promote -D ~/db
and it will spring into action, and you can point all your clients to it.. The old master is now useless to you; the only course of action you have at this point is to wipe it, and then create a new replica based on the new master.
No comments:
Post a Comment