一、节点规划
节点规划 | IP地址 | OS版本 | DB版本 |
PostgreSQL | 192.168.122.2 | CentOS 7.6 | PostgreSQL 12.11 |
PostgreSQL | 192.168.122.3 | CentOS 7.6 | PostgreSQL 12.11 |
| etcd | 192.168.122.4 | CentOS 7.6 | PostgreSQL 12.11 |
二、环境准备
2.1 配置yum
root:
wget http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache2.2 关闭防火墙和selinux
setenforce 0
sed -i.bak "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
systemctl disable firewalld.service
systemctl stop firewalld.service2.3 安装python3
mkdir -p /usr/local/python3
tar -zxvf Python-3.6.5.tgz
cd Python-3.6.5
./configure --prefix=/usr/local/python3 --enable-shared CFLAGS=-fPIC && make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
cp libpython3.6m.so.1.0 /usr/lib64三、安装etcd
3.1 安装依赖
yum install -y gcc python3-devel epel-release3.2 安装etcd
yum install -y etcd3.3 编辑etcd配置文件/etc/etcd/etcd.conf, 参考配置如下
节点一:
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.122.2:2380"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://192.168.122.2:2379"
ETCD_NAME="etcd01"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.122.2:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.122.2:2379"
ETCD_INITIAL_CLUSTER="etcd01=http://192.168.122.2:2380,etcd02=http://192.168.122.3:2380,etcd03=http://192.168.122.4:2380"
ETCD_INITIAL_CLUSTER_TOKEN="cluster1"
ETCD_INITIAL_CLUSTER_STATE="new"
节点二:
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.122.3:2380"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://192.168.122.3:2379"
ETCD_NAME="etcd02"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.122.3:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.122.3:2379"
ETCD_INITIAL_CLUSTER="etcd01=http://192.168.122.2:2380,etcd02=http://192.168.122.3:2380,etcd03=http://192.168.122.4:2380"
ETCD_INITIAL_CLUSTER_TOKEN="cluster1"
ETCD_INITIAL_CLUSTER_STATE="new"
节点三:
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.122.4:2380"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://192.168.122.4:2379"
ETCD_NAME="etcd03"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.122.4:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.122.4:2379"
ETCD_INITIAL_CLUSTER="etcd01=http://192.168.122.2:2380,etcd02=http://192.168.122.3:2380,etcd03=http://192.168.122.4:2380"
ETCD_INITIAL_CLUSTER_TOKEN="cluster1"
ETCD_INITIAL_CLUSTER_STATE="new"3.4 启动etcd
systemctl start etcd四、安装postgresql
useradd postgresmkdir -p /pgsql/datachown postgres:postgres -R /pgsqlchmod -R 700 /pgsql/datayum install -y libicu-devel libxslt glibc-devel libmpcrpm -ivh postgresql12-libs-12.11-1PGDG.rhel7.x86_64.rpmrpm -ivh postgresql12-12.11-1PGDG.rhel7.x86_64.rpmrpm -ivh postgresql12-server-12.11-1PGDG.rhel7.x86_64.rpmrpm -ivh postgresql12-contrib-12.11-1PGDG.rhel7.x86_64.rpmrpm -ivh postgresql12-devel-12.11-1PGDG.rhel7.x86_64.rpm五、安装patroni
5.1 安装
[root@pg01 ~]# python3 -m pip install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
100% |████████████████████████████████| 1.7MB 14kB/s
Installing collected packages: pip
Successfully installed pip-21.3.1[root@pg01 ~]# pip3 install psycopg2-binary -i https://mirrors.aliyun.com/pypi/simple/
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting psycopg2-binary
Downloading https://mirrors.aliyun.com/pypi/packages/df/68/a2e0aaf1f2bcd61cef9593f6e331848e839866cdd04e340e115d7e71d756/psycopg2_binary-2.9.3-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB)
|████████████████████████████████| 3.0 MB 147 kB/s
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.9.3[root@pg01 ~]# pip3 install patroni[etcd] -i https://mirrors.aliyun.com/pypi/simple/
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting patroni[etcd]
Downloading https://mirrors.aliyun.com/pypi/packages/a6/5e/e976bfb5dc4c0f99d8f4c9d80420ff1a7ef2741d761d0de6114050bbf463/patroni-2.1.4-py3-none-any.whl (225 kB)
|████████████████████████████████| 225 kB 103 kB/s
Collecting click>=4.1
Downloading https://mirrors.aliyun.com/pypi/packages/4a/a8/0b2ced25639fb20cc1c9784de90a8c25f9504a7f18cd8b5397bd61696d7d/click-8.0.4-py3-none-any.whl (97 kB)
|████████████████████████████████| 97 kB 127 kB/s
Collecting prettytable>=0.7
Downloading https://mirrors.aliyun.com/pypi/packages/9e/6d/40a24eaa03ea4418129708fd3f0f17eda73d568f16d4d4fd412566168b4c/prettytable-2.5.0-py3-none-any.whl (24 kB)
Collecting PyYAML
Downloading https://mirrors.aliyun.com/pypi/packages/b3/85/79b9e5b4e8d3c0ac657f4e8617713cca8408f6cdc65d2ee6554217cedff1/PyYAML-6.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (603 kB)
|████████████████████████████████| 603 kB 148 kB/s
Collecting psutil>=2.0.0
Downloading https://mirrors.aliyun.com/pypi/packages/6b/76/a8cb69ed3566877dcbccf408f5f9d6055227ad4fed694e88809fa8506b0b/psutil-5.9.1-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (280 kB)
|████████████████████████████████| 280 kB 144 kB/s Collecting python-dateutil
Downloading https://mirrors.aliyun.com/pypi/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
|████████████████████████████████| 247 kB 148 kB/s
Collecting urllib3!=1.21,>=1.19.1
Downloading https://mirrors.aliyun.com/pypi/packages/6f/de/5be2e3eed8426f871b170663333a0f627fc2924cc386cd41be065e7ea870/urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
|████████████████████████████████| 140 kB 151 kB/s
Collecting ydiff>=1.2.0
Downloading https://mirrors.aliyun.com/pypi/packages/1e/ed/e25e1f4fffbdfd0446f1c45504759e54676da0cde5a844d201181583fce4/ydiff-1.2.tar.gz (42 kB)
|████████████████████████████████| 42 kB 154 kB/s
Preparing metadata (setup.py) ... done
Collecting six>=1.7
Downloading https://mirrors.aliyun.com/pypi/packages/d9/5a/e7c31adbe875f2abbb91bd84cf2dc52d792b5a01506781dbcf25c91daf11/six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting python-etcd<0.5,>=0.4.3
Downloading https://mirrors.aliyun.com/pypi/packages/a1/da/616a4d073642da5dd432e5289b7c1cb0963cc5dde23d1ecb8d726821ab41/python-etcd-0.4.5.tar.gz (37 kB)
Preparing metadata (setup.py) ... done
Collecting importlib-metadata
Downloading https://mirrors.aliyun.com/pypi/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting wcwidth
Downloading https://mirrors.aliyun.com/pypi/packages/59/7c/e39aca596badaf1b78e8f547c807b04dae603a433d3e7a7e04d67f2ef3e5/wcwidth-0.2.5-py2.py3-none-any.whl (30 kB)
Collecting dnspython>=1.13.0
Downloading https://mirrors.aliyun.com/pypi/packages/9b/ed/28fb14146c7033ba0e89decd92a4fa16b0b69b84471e2deab3cc4337cc35/dnspython-2.2.1-py3-none-any.whl (269 kB)
|████████████████████████████████| 269 kB 151 kB/s
Collecting zipp>=0.5
Downloading https://mirrors.aliyun.com/pypi/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
Collecting typing-extensions>=3.6.4
Downloading https://mirrors.aliyun.com/pypi/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Using legacy 'setup.py install' for python-etcd, since package 'wheel' is not installed.
Using legacy 'setup.py install' for ydiff, since package 'wheel' is not installed.
Installing collected packages: zipp, typing-extensions, wcwidth, six, importlib-metadata, ydiff, urllib3, PyYAML, python-dateutil, psutil, prettytable, dnspython, click, python-etcd, patroni
Running setup.py install for ydiff ... done
Running setup.py install for python-etcd ... done
Successfully installed PyYAML-6.0 click-8.0.4 dnspython-2.2.1 importlib-metadata-4.8.3 patroni-2.1.4 prettytable-2.5.0 psutil-5.9.1 python-dateutil-2.8.2 python-etcd-0.4.5 six-1.16.0 typing-extensions-4.1.1 urllib3-1.26.12 wcwidth-0.2.5 ydiff-1.2 zipp-3.6.0
5.2 创建Partoni service配置文件
[Unit]
Description=Runners to orchestrate a high-availability PostgreSQL
After=syslog.target network.target
[Service]
Type=simple
User=postgres
Group=postgres
#StandardOutput=syslog
ExecStart=/usr/local/bin/patroni /opt/patroni/patroni.yml
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
TimeoutSec=30
Restart=no
[Install]
WantedBy=multi-user.target
5.3 配置/opt/patroni/patroni.yml
scope: pgsql
namespace: /service/
name: pg1
restapi:
listen: 0.0.0.0:8008
connect_address: 192.168.122.2:8008
etcd:
host: 192.168.122.2:2379bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
master_start_timeout: 300
synchronous_mode: false
postgresql:
use_pg_rewind: true
use_slots: true
parameters:
listen_addresses: "0.0.0.0"
port: 5432
wal_level: logical
hot_standby: "on"
wal_keep_segments: 100
max_wal_senders: 10
max_replication_slots: 10
wal_log_hints: "on"
pg_hba:
- host replication repl 0.0.0.0/0 trust
- host all repl 0.0.0.0/0 trust
- host all all 0.0.0.0/0 md5
postgresql:
listen: 192.168.122.2:5432
connect_address: 192.168.122.2:5432
data_dir: /pgsql/data
bin_dir: /usr/pgsql-12/bin
authentication:
replication:
username: repl
password: "123456"
superuser:
username: postgres
password: "123456"
basebackup:
max-rate: 100M
checkpoint: fast
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false
其他PG节点的patroni.yml需要相应修改下面3个参数
name:分别设置pg1~pg3
restapi.connect_address
根据各自节点IP设置
postgresql.connect_address
根据各自节点IP设置
启动Patroni
先在pg01上启动Patroni,在启动其他节点的patroni
systemctl start patroni初次启动Patroni时,Patroni会初始创建PostgreSQL实例和用户。
[root@pg01 ~]# patronictl -c /opt/patroni/patroni.yml list
+--------+---------------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+ Cluster: pgsql (7139790375193835241) ------+----+-----------+
| pg1 | 192.168.122.2 | Leader | running | 6 | |
| pg2 | 192.168.122.3 | Replica | running | 6 | 0 |
| pg3 | 192.168.122.4 | Replica | running | 6 | 0 |
+--------+---------------+---------+---------+----+-----------+
节点一patroni状态:
[root@pg01 ~]# systemctl status patroni
● patroni.service - Runners to orchestrate a high-availability PostgreSQL
Loaded: loaded (/etc/systemd/system/patroni.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-09-06 09:39:17 CST; 24min ago
Main PID: 14322 (patroni)
Tasks: 16
CGroup: /system.slice/patroni.service
├─14322 /usr/bin/python3 /usr/local/bin/patroni /opt/patroni/patroni.yml
├─14343 /usr/pgsql-12/bin/postgres -D /pgsql/data --config-file=/pgsql/data/postgresql.conf --listen_addresses=192.168.122.2 --port=5432 --cluster_name=pgsql --wal_level=logical --hot_standby=on --...
├─14345 postgres: pgsql: logger
├─14349 postgres: pgsql: checkpointer
├─14350 postgres: pgsql: background writer
├─14351 postgres: pgsql: stats collector
├─14353 postgres: pgsql: postgres postgres 192.168.122.2(40904) idle
├─14358 postgres: pgsql: walwriter
├─14359 postgres: pgsql: autovacuum launcher
├─14360 postgres: pgsql: logical replication launcher
├─14369 postgres: pgsql: walsender repl 192.168.122.3(58422) streaming 0/7001038
└─14375 postgres: pgsql: walsender repl 192.168.122.4(40686) streaming 0/7001038
Sep 06 10:02:39 pg01 patroni[14322]: 2022-09-06 10:02:39,290 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:02:49 pg01 patroni[14322]: 2022-09-06 10:02:49,288 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:02:59 pg01 patroni[14322]: 2022-09-06 10:02:59,289 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:03:09 pg01 patroni[14322]: 2022-09-06 10:03:09,289 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:03:19 pg01 patroni[14322]: 2022-09-06 10:03:19,289 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:03:29 pg01 patroni[14322]: 2022-09-06 10:03:29,291 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:03:39 pg01 patroni[14322]: 2022-09-06 10:03:39,298 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:03:49 pg01 patroni[14322]: 2022-09-06 10:03:49,289 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:03:59 pg01 patroni[14322]: 2022-09-06 10:03:59,290 INFO: no action. I am (pg1), the leader with the lock
Sep 06 10:04:09 pg01 patroni[14322]: 2022-09-06 10:04:09,289 INFO: no action. I am (pg1), the leader with the lock[root@pg02 ~]# systemctl status patroni.service
● patroni.service - Runners to orchestrate a high-availability PostgreSQL
Loaded: loaded (/etc/systemd/system/patroni.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-09-06 10:05:30 CST; 1min 10s ago
Main PID: 36015 (patroni)
Tasks: 13
CGroup: /system.slice/patroni.service
├─36015 /usr/bin/python3 /usr/local/bin/patroni /opt/patroni/patroni.yml
├─36037 /usr/pgsql-12/bin/postgres -D /pgsql/data --config-file=/pgsql/data/postgresql.conf --listen_addresses=192.168.122.3 --port=5432 --cluster_name=pgsql --wal_level=logical --hot_standby=on --...
├─36039 postgres: pgsql: logger
├─36040 postgres: pgsql: startup recovering 000000060000000000000007
├─36041 postgres: pgsql: checkpointer
├─36042 postgres: pgsql: background writer
├─36043 postgres: pgsql: stats collector
├─36044 postgres: pgsql: walreceiver
└─36049 postgres: pgsql: postgres postgres 192.168.122.3(58862) idle
Sep 06 10:05:31 pg02 patroni[36015]: 2022-09-06 10:05:31,772 INFO: Lock owner: pg1; I am pg2
Sep 06 10:05:31 pg02 patroni[36015]: 2022-09-06 10:05:31,772 INFO: establishing a new patroni connection to the postgres cluster
Sep 06 10:05:31 pg02 patroni[36015]: 2022-09-06 10:05:31,790 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:05:39 pg02 patroni[36015]: 2022-09-06 10:05:39,303 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:05:49 pg02 patroni[36015]: 2022-09-06 10:05:49,307 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:05:59 pg02 patroni[36015]: 2022-09-06 10:05:59,306 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:06:09 pg02 patroni[36015]: 2022-09-06 10:06:09,309 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:06:19 pg02 patroni[36015]: 2022-09-06 10:06:19,307 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:06:29 pg02 patroni[36015]: 2022-09-06 10:06:29,307 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)
Sep 06 10:06:39 pg02 patroni[36015]: 2022-09-06 10:06:39,305 INFO: no action. I am (pg2), a secondary, and following a leader (pg1)[root@pg03 ~]# systemctl status patroni.service
● patroni.service - Runners to orchestrate a high-availability PostgreSQL
Loaded: loaded (/etc/systemd/system/patroni.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2022-09-06 09:39:49 CST; 26min ago
Main PID: 10776 (patroni)
Tasks: 13
CGroup: /system.slice/patroni.service
├─10776 /usr/bin/python3 /usr/local/bin/patroni /opt/patroni/patroni.yml
├─10794 /usr/pgsql-12/bin/postgres -D /pgsql/data --config-file=/pgsql/data/postgresql.conf --listen_addresses=192.168.122.4 --port=5432 --cluster_name=pgsql --wal_level=logical --hot_standby=on --...
├─10796 postgres: pgsql: logger
├─10797 postgres: pgsql: startup recovering 000000060000000000000007
├─10798 postgres: pgsql: checkpointer
├─10799 postgres: pgsql: background writer
├─10800 postgres: pgsql: stats collector
├─10808 postgres: pgsql: postgres postgres 192.168.122.4(47384) idle
└─10812 postgres: pgsql: walreceiver streaming 0/7001038
Sep 06 10:04:59 pg03 patroni[10776]: 2022-09-06 10:04:59,307 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:05:09 pg03 patroni[10776]: 2022-09-06 10:05:09,309 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:05:19 pg03 patroni[10776]: 2022-09-06 10:05:19,305 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:05:29 pg03 patroni[10776]: 2022-09-06 10:05:29,306 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:05:39 pg03 patroni[10776]: 2022-09-06 10:05:39,303 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:05:49 pg03 patroni[10776]: 2022-09-06 10:05:49,309 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:05:59 pg03 patroni[10776]: 2022-09-06 10:05:59,306 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:06:09 pg03 patroni[10776]: 2022-09-06 10:06:09,309 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:06:19 pg03 patroni[10776]: 2022-09-06 10:06:19,307 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)
Sep 06 10:06:29 pg03 patroni[10776]: 2022-09-06 10:06:29,307 INFO: no action. I am (pg3), a secondary, and following a leader (pg1)



