Prepare replica, Patroni, Ubuntu, part 7

The indications of hosts:
# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 server

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

192.168.88.11 node1
192.168.88.12 node2
192.168.88.13 node3
192.168.88.10 etcd
Prerequsites:
apt update && apt-get -y install postgresql postgresql-contrib watchdog nmap jq nfs-common nfs-kernel-server python3-dev python3-pip python3-testresources
pip3 install --upgrade pip && pip3.8 install --upgrade setuptools && pip3.8 install psycopg2-binary python-etcd patroni
Make directory where patroni config will be kept:
mkdir -p /etc/patroni
In case this is replica then grab original from leader:
scp root@node1:/etc/patroni/config.yml /etc/patroni
Regards node2:
sed -i "s|node1|node2|g;s|192.168.88.11:8008|192.168.88.12:8008|g;s|192.168.88.11:5444|192.168.88.12:5444|g" /etc/patroni/config.yml
Regards node3:
sed -i "s|node1|node3|g;s|192.168.88.11:8008|192.168.88.13:8008|g;s|192.168.88.11:5444|192.168.88.13:5444|g" /etc/patroni/config.yml
Make sure owner is postgres:
chown -R postgres:postgres /etc/patroni
If this is the service has been launched before then remove data dir to have a clear replica:
rm -rf /postgresql/12/patroni/pg_data
Navigate to postgres user:
su - postgres
Test if config is accessible and in good condition:
patroni --validate-config /etc/patroni/config.yml
List existing members:
patronictl --config-file /etc/patroni/config.yml list
It will print:
+ Cluster: patronii_ha (6947682064307824555) ----+----+-----------+
| Member | Host               | Role   | State   | TL | Lag in MB |
+--------+--------------------+--------+---------+----+-----------+
| node1  | 192.168.88.11:5444 | Leader | running |  1 |           |
+--------+--------------------+--------+---------+----+-----------+
Test second node:
patroni /etc/patroni/config.yml
This will print:
2021-04-05 17:43:54,750 INFO: No PostgreSQL configuration items changed, nothing to reload.
2021-04-05 17:43:54,755 INFO: Lock owner: node1; I am node2
2021-04-05 17:43:54,758 INFO: trying to bootstrap from leader 'node1'
2021-04-05 17:43:56,110 INFO: replica has been created using basebackup
2021-04-05 17:43:56,111 INFO: bootstrapped from leader 'node1'
2021-04-05 17:43:56.296 EEST [15447] LOG:  starting PostgreSQL 12.6 (Ubuntu 12.6-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
2021-04-05 17:43:56.296 EEST [15447] LOG:  listening on IPv4 address "0.0.0.0", port 5444
2021-04-05 17:43:56,298 INFO: postmaster pid=15447
2021-04-05 17:43:56.303 EEST [15447] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5444"
2021-04-05 17:43:56.331 EEST [15450] LOG:  database system was interrupted; last known up at 2021-04-05 17:43:54 EEST
2021-04-05 17:43:56.337 EEST [15451] FATAL:  the database system is starting up
localhost:5444 - rejecting connections
2021-04-05 17:43:56.348 EEST [15453] FATAL:  the database system is starting up
localhost:5444 - rejecting connections
cp: cannot stat '/wal_archive/archive/00000002.history': No such file or directory
2021-04-05 17:43:57.050 EEST [15450] LOG:  entering standby mode
2021-04-05 17:43:57.122 EEST [15450] LOG:  restored log file "000000010000000000000004" from archive
2021-04-05 17:43:57.145 EEST [15450] LOG:  redo starts at 0/4000028
2021-04-05 17:43:57.151 EEST [15450] LOG:  consistent recovery state reached at 0/4000100
2021-04-05 17:43:57.154 EEST [15447] LOG:  database system is ready to accept read only connections
cp: cannot stat '/wal_archive/archive/000000010000000000000005': No such file or directory
2021-04-05 17:43:57.169 EEST [15463] FATAL:  could not start WAL streaming: ERROR:  replication slot "node2" does not exist
cp: cannot stat '/wal_archive/archive/00000002.history': No such file or directory
cp: cannot stat '/wal_archive/archive/000000010000000000000005': No such file or directory
2021-04-05 17:43:57.196 EEST [15468] FATAL:  could not start WAL streaming: ERROR:  replication slot "node2" does not exist
cp: cannot stat '/wal_archive/archive/00000002.history': No such file or directory
localhost:5444 - accepting connections
2021-04-05 17:43:57,382 INFO: Lock owner: node1; I am node2
2021-04-05 17:43:57,382 INFO: does not have lock
2021-04-05 17:43:57,382 INFO: establishing a new patroni connection to the postgres cluster
2021-04-05 17:43:57,400 INFO: no action.  i am a secondary and i am following a leader
2021-04-05 17:43:58,798 INFO: Lock owner: node1; I am node2
2021-04-05 17:43:58,798 INFO: does not have lock
2021-04-05 17:43:58,802 INFO: no action.  i am a secondary and i am following a leader
cp: cannot stat '/wal_archive/archive/000000010000000000000005': No such file or directory
2021-04-05 17:44:02.198 EEST [15478] LOG:  started streaming WAL from primary at 0/5000000 on timeline 1
Exit postgres user:
exit
Start normaly as service user:
systemctl start patroni
If at startup it says:
2021-04-05 16:46:54,032 INFO: No PostgreSQL configuration items changed, nothing to reload.
2021-04-05 16:46:54,048 CRITICAL: system ID mismatch, node node2 belongs to a different cluster: 6947658390375686177 != 6947651656822903731
Stop all patroni nodes and remove datadir:
rm -rf /postgresql/12/patroni/pg_data
rm -rf /wal_archive/archive
Then on etcd list all services:
etcdctl ls
etcdctl ls /service
Remove:
etcdctl rm --recursive /service/patronii_ha
Start back patroni nodes. See status:
watch -n1 'su - postgres -c "patronictl -c /etc/patroni/config.yml list"'

No comments: