patroni+etcd安装部署

环境:
Os:Centos 7
PG:13.8
etcd:3.3
patroni:2.1.4

pg主从:
角色 ip 部署中间件
主 192.168.1.102 pg+Patroni
从1 192.168.1.103 pg+Patroni
从2 192.168.1.104 pg+Patroni

etcd:
192.168.1.105
192.168.1.106
192.168.1.107

说明:pg和Patroni一对一的安装部署,安装pg的机器都相应安装Patroni,etcd可以独立机器安装

patroni文档官网地址:

https://patroni.readthedocs.io/en/latest/ 

 

1.关闭每个节点的防火墙
每个节点都要执行
# systemctl stop firewalld.service
# systemctl disable firewalld.service

2.安装python3
在Patroni所在节点(192.168.1.102,192.168.1.103,192.168.1.104)上执行
参考链接
https://www.cnblogs.com/hxlasky/p/14913936.html

 

安装完成python3,需要修改如下配置才可以使用yum
/usr/bin/yum: 将文件第一行改为/usr/bin/python2.7。(2.7.x也改为2.7)
/usr/libexec/urlgrabber-ext-down: 将文件第一行改为/usr/bin/python2.7

 

###########################部署pg主从########################

在192.168.1.102、192.168.1.103、192.168.1.104上进行部署,组成一个1主2从的集群
部署步骤可以参考
https://www.cnblogs.com/hxlasky/p/16846972.html
说明:这里维护都是使用systemctl维护的,部署了patroni会接管systemctl,直接使用patroni管理

只从部署的复制级别是:

wal_level = 'replica' 

 

###########################部署etcd##########################

可以参考自定义下载安装(etcd v3版本)

https://www.cnblogs.com/hxlasky/p/16892752.html

下面的安装步骤是etcd v2版本

在192.168.1.105、192.168.1.106、192.168.1.107上进行部署

1.安装etcd
每个etcd节点都要执行
[root@localhost ~]# yum -y install etcd

2.修改配置文件
192.168.1.105
vi /etc/etcd/etcd.conf
找到如下栏目进行修改

ETCD_NAME=etcd1
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.1.105:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.1.105:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.105:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.105:2380,etcd2=http://192.168.1.106:2380,etcd3=http://192.168.1.107:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.105:2379"

 

192.168.1.106
vi /etc/etcd/etcd.conf
找到如下栏目进行修改

ETCD_NAME=etcd2
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.1.106:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.1.106:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.106:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.105:2380,etcd2=http://192.168.1.106:2380,etcd3=http://192.168.1.107:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.106:2379"

 

192.168.1.107
vi /etc/etcd/etcd.conf
找到如下栏目进行修改

ETCD_NAME=etcd3
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_PEER_URLS="http://192.168.1.107:2380"
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.1.107:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.107:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.105:2380,etcd2=http://192.168.1.106:2380,etcd3=http://192.168.1.107:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.107:2379"

 

3.启动etcd
每个etcd节点都要执行
[root@localhost etcd]# systemctl start etcd
[root@localhost etcd]# systemctl enable etcd
[root@localhost etcd]# systemctl status etcd

发现开始启动失败的,多启动几次好像就可以了

 

4.验证
在任意节点上查看ETCD状态
[root@localhost etcd]# etcdctl cluster-health
member 48a8abb41634acbe is healthy: got healthy result from http://192.168.1.107:2379
member 73079f127bc11ea8 is healthy: got healthy result from http://192.168.1.106:2379
member cec9e0ee849de346 is healthy: got healthy result from http://192.168.1.105:2379
cluster is healthy

 

在任意节点上查看ETCD成员:
[root@localhost etcd]# etcdctl member list
48a8abb41634acbe: name=etcd3 peerURLs=http://192.168.1.107:2380 clientURLs=http://192.168.1.107:2379 isLeader=false
73079f127bc11ea8: name=etcd2 peerURLs=http://192.168.1.106:2380 clientURLs=http://192.168.1.106:2379 isLeader=false
cec9e0ee849de346: name=etcd1 peerURLs=http://192.168.1.105:2380 clientURLs=http://192.168.1.105:2379 isLeader=true

 

###########################部署Patroni##########################

1.在pg所在的机器上执行,我这里是192.168.1.102,192.168.1.103,192.168.1.104
[root@hmaster ~]#yum install postgresql-devel*
[root@hmaster ~]#pip3 install psycopg2-binary
[root@hmaster ~]#pip3 install patroni[etcd,consul]
[root@hmaster ~]# which patroni
/usr/local/bin/patroni

2.查看安装的版本
[root@hmaster ~]# patroni --version
patroni 2.1.4

 

3.pg创建复制账号
只需要在主库上执行,没有创建的需要创建,已经创建的可以忽略
postgres=# CREATE ROLE replica login replication encrypted password 'replica';

 

5.创建配置文件
192.168.1.102

[root@hmaster conf]#mkdir -p /usr/patroni/conf
[root@hmaster conf]#cd /usr/patroni/conf
[root@hmaster conf]#vi patroni_postgresql.yml
添加如下内容

  

[postgres@localhost conf]$ more patroni_postgresql.yml 
scope: pgsql13
namespace: /pgsql/
name: pg1
restapi:
  listen: 192.168.1.100:8008
  connect_address: 192.168.1.100:8008
etcd3:
  hosts: 192.168.1.105:2379,192.168.1.106:2379,192.168.1.107:2379
##etcd:
##  hosts: 192.168.1.104:2379,192.168.1.108:2379,192.168.1.109:2379
log:
  level: "INFO"
  dir: "/usr/patroni/log/"
  file_size: 1073741824

bootstrap:
  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: false ##不使用复制槽的话需要关闭,默认是开启的,直接编辑bootstrap段落好像不生效,使用edit-config进行设置生效
      parameters:
##      listen_addresses: "*"
##      max_connections: 200
wal_keep_size: 512MB ##该参数好像在postgres_base_conf中配置不起作用
## wal_level: logical ## hot_standby: "on" ## wal_keep_segments: 1000 ## max_wal_senders: 10 ## max_replication_slots: 10 ## wal_log_hints: "on"

##下面定义的是每个pg实例各自的特性配置,比如端口,数据目录等 postgresql: listen: '0.0.0.0:5432' connect_address: '192.168.1.100:5432' data_dir: /opt/pg13/data bin_dir: /usr/pgsql-13/bin authentication: replication: username: replica password: replica superuser: username: postgres password: postgres callbacks: on_start: /usr/patroni/patroni_callback.sh on_stop: /usr/patroni/patroni_callback.sh on_role_change: /usr/patroni/patroni_callback.sh tags: nofailover: false noloadbalance: false clonefrom: false nosync: false

 

 说明

a.若是使用etcd v3版本的需要如下配置

etcd3:
hosts: 192.168.1.105:2379,192.168.1.106:2379,192.168.1.107:2379

b.需要创建日志目录(postgres用户有读写权限)

mkdir -p   /usr/patroni/log/

c. /usr/patroni/patroni_callback.sh脚本后面有介绍;

d.若是修改端口,好像只能在 postgresql栏目下制定,在bootstrap设置不管用;

e.bootstrap的参数需要使用edit-config编辑修改才能生效,手工修改好像不生效的;

 

以下参数是patroni控制的,直接修改pg基础配置文件postgresql.base.conf或是使用alter system方式修改无效

max_connections: 100
max_locks_per_transaction: 64
max_worker_processes: 8
max_prepared_transactions: 0
wal_log_hints: on
track_commit_timestamp: off
max_wal_senders: 5
max_replication_slots: 5
wal_keep_segments: 8
wal_keep_size: 128MB
postgresql:
  listen: '0.0.0.0:5432' 

 

 6.修改目录属主

chown -R postgres:postgres /usr/patroni/

 

7.配置vip部分

每个节点上都需要执行,需要配置vip的,需要执行该步骤,查看后面章节<<配置vip部分>>

 

 7.创建复制槽(use_slots: true才需要配置)

只在主节点上执行 

好像系统启动后会自动创建,我们这里手工创建,名字需要与patroni_postgresql.yml中的name一致

postgres=# select * from pg_create_physical_replication_slot('pg1');
postgres=# select * from pg_create_physical_replication_slot('pg2');
postgres=# select * from pg_create_physical_replication_slot('pg3');

  查看

select * from pg_replication_slots;
select * from pg_stat_replication;

  

7.安装watchdog
每个patroni节点上执行
[root@hmaster ~]# yum install watchdog -y
[root@hmaster ~]# modprobe softdog
[root@hmaster ~]# systemctl enable watchdog

 

修改权限
chmod 666 /dev/watchdog
否则报如下的错误:
WARNING: Could not activate Linux watchdog device: "Can't open watchdog device: [Errno 13] Permission denied: '/dev/watchdog'"

 

8.做成系统服务的方式
下面步骤需要在root账号下执行

vi /usr/lib/systemd/system/patroni.service

 

[Unit]
Description=patroni - a high-availability PostgreSQL
Documentation=https://patroni.readthedocs.io/en/latest/index.html
After=syslog.target network.target etcd.target
Wants=network-online.target

[Service]
Type=simple
User=postgres
Group=postgres
PermissionsStartOnly=true
ExecStart=/usr/local/bin/patroni /usr/patroni/conf/patroni_postgresql.yml
ExecReload=/bin/kill -HUP $MAINPID
LimitNOFILE=65536
KillMode=process
KillSignal=SIGINT
Restart=on-abnormal
RestartSec=30s
TimeoutSec=0

[Install]
WantedBy=multi-user.target

 

将以上的配置文件和启动文件拷贝到另外的节点

 

scp  /usr/lib/systemd/system/patroni.service root@192.168.1.103:/usr/lib/systemd/system/
scp patroni_postgresql.yml root@192.168.1.103:/usr/patroni/conf/
scp patroni_callback.sh root@192.168.1.103:/usr/patroni/

 

注意要修改配置文件,修改相应的ip地址,相应的目录需要提前创建好

 

9.启动

 等所有的节点部署完成后,逐一启动每个patroni节点,在postgres用户下执行

su - postgres
/usr/local/bin/patroni /usr/patroni/conf/patroni_postgresql.yml

 该方式启动的话Crtl+c会退出中断,可以使用nohup的方式或是做成服务的方式启动

 使用服务的方式启动

systemctl start patroni
systemctl enable patroni
systemctl status patroni

启动日志会输出配置的日志目录下/usr/patroni/log

 

 9.查看状态

节点1 

[postgres@hmaster data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Replica | running |  5 |         0 | *               |
| pg2    | 192.168.1.103 | Leader  | running |  5 |           | *               |
| pg3    | 192.168.1.104 | Replica | running |  5 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

从库1

[postgres@hslavea data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Replica | running |  5 |         0 | *               |
| pg2    | 192.168.1.103 | Leader  | running |  5 |           | *               |
| pg3    | 192.168.1.104 | Replica | running |  5 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

从库2

[postgres@hslaveb ~]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Replica | running |  5 |         0 | *               |
| pg2    | 192.168.1.103 | Leader  | running |  5 |           | *               |
| pg3    | 192.168.1.104 | Replica | running |  5 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

10.修改参数

 

查看参数
在其中一个节点上操作,我这里是在节点1上操作,一个节点修改会自动同步到其他的节点

su - postgres
[postgres@localhost ~]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml show-config
loop_wait: 10
master_start_timeout: 300
maximum_lag_on_failover: 1048576
postgresql:
  parameters: null
  use_pg_rewind: true
  use_slots: false
retry_timeout: 10
synchronous_mode: false
ttl: 30

下面修改最大连接数和wal_keep_size

[postgres@localhost ~]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml edit-config

loop_wait: 10
master_start_timeout: 300
maximum_lag_on_failover: 1048576
postgresql:
  parameters: 
    max_connections: 200
    wal_keep_size: 512MB
  use_pg_rewind: true
  use_slots: false
retry_timeout: 10
synchronous_mode: false
ttl: 30

查看参数,max_connections需要重启动实例才能生效
postgres=# show max_connections;
max_connections
-----------------
100
(1 row)

postgres=# show wal_keep_size;
wal_keep_size
---------------
512MB
(1 row)

重启顺序:先从库-->主库,切换过程会发生主从切换
[root@localhost ~]# systemctl restart patroni

重启后再次查看连接数参数
[postgres@localhost ~]$ psql
psql (14.6)
Type "help" for help.

postgres=# show max_connections;
max_connections
-----------------
200
(1 row)

 

 

#############################################故障切换######################################

1.尝试停掉主库
我们这里停掉主库192.168.1.103

[root@hslavea ~]# systemctl status postgresql-13
● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://www.postgresql.org/docs/13/static/

Nov 11 08:57:34 hslavea systemd[1]: Starting PostgreSQL 13 database server...
Nov 11 08:57:34 hslavea postmaster[2045]: 2022-11-11 08:57:34.868 CST [2045] LOG:  redirecting log output to ...ocess
Nov 11 08:57:34 hslavea postmaster[2045]: 2022-11-11 08:57:34.868 CST [2045] HINT:  Future log output will ap...log".
Nov 11 08:57:35 hslavea systemd[1]: Started PostgreSQL 13 database server.
Nov 11 08:59:28 hslavea systemd[1]: Stopping PostgreSQL 13 database server...
Nov 11 08:59:28 hslavea systemd[1]: Stopped PostgreSQL 13 database server.
Nov 11 09:01:40 hslavea systemd[1]: Starting PostgreSQL 13 database server...
Nov 11 09:01:40 hslavea postmaster[2255]: 2022-11-11 09:01:40.266 CST [2255] LOG:  redirecting log output to ...ocess
Nov 11 09:01:40 hslavea postmaster[2255]: 2022-11-11 09:01:40.266 CST [2255] HINT:  Future log output will ap...log".
Nov 11 09:01:40 hslavea systemd[1]: Started PostgreSQL 13 database server.
Hint: Some lines were ellipsized, use -l to show in full.

发现patroni接管了原有的系统服务

直接关闭虚拟机
[root@hslavea ~]# shutdown -h now

在其他机器查看主从情况
[postgres@hslaveb data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host | Role | State | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1 | 192.168.1.102 | Leader | running | 6 | | * |
| pg3 | 192.168.1.104 | Replica | running | 6 | 0 | * |
+--------+---------------+---------+---------+----+-----------+-----------------+


虚拟机重新开启后,启动patroni程序,发现自动加入到集群
[postgres@hslaveb data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host | Role | State | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1 | 192.168.1.102 | Leader | running | 6 | | * |
| pg2 | 192.168.1.103 | Replica | running | 6 | 0 | * |
| pg3 | 192.168.1.104 | Replica | running | 6 | 0 | * |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

2.手工切换switchover

查看当前的集群状态,在任意节点上执行,我这里是在主节点上执行

[root@hmaster log]# su - postgres
[postgres@hmaster ~]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Leader  | running | 10 |           | *               |
| pg2    | 192.168.1.103 | Replica | running | 10 |         0 | *               |
| pg3    | 192.168.1.104 | Replica | running | 10 |         0 |                 |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

执行手工切换

[postgres@hmaster ~]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml switchover
Master [pg1]: 
Candidate ['pg2', 'pg3'] []:    ##这里有一多个候选的话 系统会自动现在一个,也可以具体指定那一个做为候选
When should the switchover take place (e.g. 2022-11-13T22:19 )  [now]: 
Current cluster topology
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Leader  | running | 10 |           | *               |
| pg2    | 192.168.1.103 | Replica | running | 10 |         0 | *               |
| pg3    | 192.168.1.104 | Replica | running | 10 |         0 |                 |
+--------+---------------+---------+---------+----+-----------+-----------------+
Are you sure you want to switchover cluster pgsql13, demoting current master pg1? [y/N]: y
2022-11-13 21:19:20.95016 Successfully switched over to "pg3"

 

再次查看集群

[postgres@hmaster ~]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Replica | running | 10 |         0 | *               |
| pg2    | 192.168.1.103 | Replica | running | 11 |         0 | *               |
| pg3    | 192.168.1.104 | Leader  | running | 11 |           |                 |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

 3.failover

查看当前的集群

[postgres@hslavea data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Leader  | running | 12 |           | *               |
| pg2    | 192.168.1.103 | Replica | running | 12 |         0 | *               |
| pg3    | 192.168.1.104 | Replica | running | 12 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

[postgres@hslavea data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml failover
Candidate ['pg2', 'pg3'] []: pg2 ##这里必须指定候选者
Current cluster topology
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Leader  | running | 12 |           | *               |
| pg2    | 192.168.1.103 | Replica | running | 12 |         0 | *               |
| pg3    | 192.168.1.104 | Replica | running | 12 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+
Are you sure you want to failover cluster pgsql13, demoting current master pg1? [y/N]: y
2022-11-14 10:52:05.76778 Successfully failed over to "pg2"
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Replica | stopped |    |   unknown | *               |
| pg2    | 192.168.1.103 | Leader  | running | 12 |           | *               |
| pg3    | 192.168.1.104 | Replica | running | 12 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

再次查看集群

[postgres@hslavea data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+-----------------+
| Member | Host          | Role    | State   | TL | Lag in MB | Pending restart |
+ Cluster: pgsql13 (7163923628183234578) ----+----+-----------+-----------------+
| pg1    | 192.168.1.102 | Replica | running | 13 |         0 | *               |
| pg2    | 192.168.1.103 | Leader  | running | 13 |           | *               |
| pg3    | 192.168.1.104 | Replica | running | 13 |         0 | *               |
+--------+---------------+---------+---------+----+-----------+-----------------+

 

  

11.验证从库是否可以写入
[postgres@hslavea ~]$ psql -h 192.168.1.103 -U hxl -d db_test
Password for user hxl:
psql (13.8)
Type "help" for help.
db_test=> insert into tb_test(name) values('name11');
ERROR: cannot execute INSERT in a read-only transaction


尝试主库登录
[postgres@hmaster data]$ psql -h 192.168.1.102 -U hxl -d db_test
Password for user hxl:
psql (13.8)
Type "help" for help.

db_test=> insert into tb_test(name) values('name11');
INSERT 0 1

 

12.查看配置
任意一个节点执行

[postgres@hmaster data]$ patronictl -c /usr/patroni/conf/patroni_postgresql.yml show-config
loop_wait: 10
master_start_timeout: 300
maximum_lag_on_failover: 1048576
postgresql:
  parameters: null
  use_pg_rewind: true
retry_timeout: 10
synchronous_mode: false
ttl: 30

这里的参数是patroni_postgresql.yml配置的参数

 

#######################配置vip部分#################

我们这里假定的VIP地址为:192.168.1.199
1.添加sudo用户
root用户下执行
每个patroni节点上执行
echo 'postgres ALL=(ALL) NOPASSWD: ALL'> /etc/sudoers.d/postgres

 

2.编写vip脚本
postgres账号下操作
[root@hmaster network-scripts]# su - postgres
[postgres@hmaster ~]$ cd /usr/patroni/

[postgres@hmaster conf]$ vi patroni_callback.sh

#!/bin/bash

readonly cb_name=$1
readonly role=$2
readonly scope=$3

function usage() {
    echo "Usage: $0 <on_start|on_stop|on_role_change> <role> <scope>";
    exit 1;
}

echo "this is patroni callback $cb_name $role $scope"

case $cb_name in
    on_stop)
        sudo ip addr del 192.168.1.199/24 dev enp0s3 label enp0s3:1   
        #sudo arping -q -A -c 1 -I enp0s3 192.168.1.199
        sudo iptables -F
        ;;
    on_start)
        ;;
    on_role_change)
        if [[ $role == 'master' ]]; then
            sudo ip addr add 192.168.1.199/24 brd 192.168.1.255 dev enp0s3 label enp0s3:1
            sudo arping -q -A -c 1 -I enp0s3 192.168.1.199
            sudo iptables -F
        elif [[ $role == 'slave' ]]||[[ $role == 'replica' ]]||[[ $role == 'logical' ]]; then
            sudo ip addr del 192.168.1.199/24 dev enp0s3 label enp0s3:1   
            #sudo arping -q -A -c 1 -I enp0s8 192.168.56.100
            sudo iptables -F
        fi
        ;;
    *)
        usage
        ;;
esac

这里的网卡名称需要与每台机器的名称保持一致 

 

 

3.添加可执行权限
[postgres@hmaster conf]$ chmod u+x patroni_callback.sh

 

说明:
1.patroni进程停掉了,那么pg也自动停掉了

2.原来使用systemctl停启动数据库的不能使用,需要使用patroni停启动

 3.问题:

2022-11-14 09:25:34.834 CST [1811] ERROR:  replication slot "pg3" does not exist
2022-11-14 09:25:34.834 CST [1811] STATEMENT:  START_REPLICATION SLOT "pg3" 0/D000000 TIMELINE 9
2022-11-14 09:25:34.861 CST [1813] ERROR:  replication slot "pg3" does not exist
2022-11-14 09:25:34.861 CST [1813] STATEMENT:  START_REPLICATION SLOT "pg3" 0/D000000 TIMELINE 9
2022-11-14 09:25:39.759 CST [1814] ERROR:  replication slot "pg3" does not exist
2022-11-14 09:25:39.759 CST [1814] STATEMENT:  START_REPLICATION SLOT "pg3" 0/D000000 TIMELINE 9

 

-- The End --

posted @ 2022-11-11 16:25  slnngk  阅读(982)  评论(0编辑  收藏  举报