Patroni

PG高可用

PostgreSQL的开源HA工具有很多种,下面几种算是比较常用的

  • PAF(PostgreSQL Automatic Failomianver)
  • repmgr
  • Patroni

它们的比较可以参考: https://scalegrid.io/blog/managing-high-availability-in-postgresql-part-1/
在这里插入图片描述

Patroni

Patroni 提供了用于配置高可用性 PostgreSQL 集群的模板。

Introduction

参数顺序

Patroni configuration
在这里插入图片描述

patronictl

patronictl --help

patronictl -c postgres-ha-bootstrap.yaml --help
Usage: patronictl [OPTIONS] COMMAND [ARGS]...

Options:
  -c, --config-file TEXT  Configuration file
  -d, --dcs TEXT          Use this DCS
  -k, --insecure          Allow connections to SSL sites without certs
  --help                  Show this message and exit.

Commands:
  configure    Create configuration file
  dsn          Generate a dsn for the provided member, defaults to a dsn...
  edit-config  Edit cluster configuration
  failover     Failover to a replica
  flush        Discard scheduled events
  history      Show the history of failovers/switchovers
  list         List the Patroni members for a given Patroni
  pause        Disable auto failover
  query        Query a Patroni PostgreSQL member
  reinit       Reinitialize cluster member
  reload       Reload cluster member configuration
  remove       Remove cluster from DCS
  restart      Restart cluster member
  resume       Resume auto failover
  scaffold     Create a structure for the cluster in DCS
  show-config  Show cluster configuration
  switchover   Switchover to a replica
  topology     Prints ASCII topology for given cluster
  version      Output version of patronictl command or a running Patroni...

patronictl edit-config & show-config

patronictl edit-config --force -s/-p 参数

patronictl show-config

在这里插入图片描述
在这里插入图片描述

patronictl list

patronictl -c postgres-ha-bootstrap.yaml list
+ Cluster: postgres-11c0db98 (7198370692495388831) -+---------+----+-----------+
| Member                | Host       | Role         | State   | TL | Lag in MB |
+-----------------------+------------+--------------+---------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Leader       | running |  2 |           |
| postgres-11c0db98-1-0 | 245.0.0.37 | Sync Standby | running |  2 |         0 |
+-----------------------+------------+--------------+---------+----+-----------+

patronictl reinit

Leader 不能 reinit

patronictl -c postgres-ha-bootstrap.yaml reinit postgres-11c0db98
+ Cluster: postgres-11c0db98 (7198370692495388831) -+---------+----+-----------+
| Member                | Host       | Role         | State   | TL | Lag in MB |
+-----------------------+------------+--------------+---------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Leader       | running |  2 |           |
| postgres-11c0db98-1-0 | 245.0.0.37 | Sync Standby | running |  2 |         0 |
+-----------------------+------------+--------------+---------+----+-----------+
Which member do you want to reinitialize [postgres-11c0db98-0-0, postgres-11c0db98-1-0]? []: postgres-11c0db98-0-0 
Are you sure you want to reinitialize members postgres-11c0db98-0-0? [y/N]: y
Failed: reinitialize for member postgres-11c0db98-0-0, status code=503, (I am the leader, can not reinitialize)
patronictl -c postgres-ha-bootstrap.yaml reinit postgres-11c0db98 postgres-11c0db98-1-0
+ Cluster: postgres-11c0db98 (7198370692495388831) -+---------+----+-----------+
| Member                | Host       | Role         | State   | TL | Lag in MB |
+-----------------------+------------+--------------+---------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Leader       | running |  2 |           |
| postgres-11c0db98-1-0 | 245.0.0.37 | Sync Standby | running |  2 |         0 |
+-----------------------+------------+--------------+---------+----+-----------+
Are you sure you want to reinitialize members postgres-11c0db98-1-0? [y/N]: y
Success: reinitialize for member postgres-11c0db98-1-0

patronictl -c switchover

patronictl -c postgres-ha-bootstrap.yaml switchover 
Master [postgres-11c0db98-0-0]: 
Candidate ['postgres-11c0db98-1-0'] []: 
When should the switchover take place (e.g. 2023-02-25T22:37 )  [now]: 
Current cluster topology
+ Cluster: postgres-11c0db98 (7198370692495388831) -+---------+----+-----------+
| Member                | Host       | Role         | State   | TL | Lag in MB |
+-----------------------+------------+--------------+---------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Leader       | running |  2 |           |
| postgres-11c0db98-1-0 | 245.0.0.37 | Sync Standby | running |  2 |         0 |
+-----------------------+------------+--------------+---------+----+-----------+
Are you sure you want to switchover cluster postgres-11c0db98, demoting current master postgres-11c0db98-0-0? [y/N]: y
2023-02-25 21:37:44.30145 Successfully switched over to "postgres-11c0db98-1-0"
+ Cluster: postgres-11c0db98 (7198370692495388831) -------+----+-----------+
| Member                | Host       | Role    | State    | TL | Lag in MB |
+-----------------------+------------+---------+----------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Replica | stopping |    |   unknown |
| postgres-11c0db98-1-0 | 245.0.0.37 | Leader  | running  |  2 |           |
+-----------------------+------------+---------+----------+----+-----------+

patronictl -c postgres-ha-bootstrap.yaml list
+ Cluster: postgres-11c0db98 (7198370692495388831) -------+----+-----------+
| Member                | Host       | Role    | State    | TL | Lag in MB |
+-----------------------+------------+---------+----------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Replica | stopping |    |   unknown |
| postgres-11c0db98-1-0 | 245.0.0.37 | Leader  | running  |  3 |           |
+-----------------------+------------+---------+----------+----+-----------+

patronictl -c postgres-ha-bootstrap.yaml list
+ Cluster: postgres-11c0db98 (7198370692495388831) -+---------+----+-----------+
| Member                | Host       | Role         | State   | TL | Lag in MB |
+-----------------------+------------+--------------+---------+----+-----------+
| postgres-11c0db98-0-0 | 245.0.0.26 | Sync Standby | running |  3 |         0 |
| postgres-11c0db98-1-0 | 245.0.0.37 | Leader       | running |  3 |           |
+-----------------------+------------+--------------+---------+----+-----------+

patronictl failover

patronictl -c postgres-ha-bootstrap.yaml failover
Candidate ['postgres-11c0db98-0-0'] []: 
Error: Failover could be performed only to a specific candidat
patronictl failover <clusterName> --candidate <memberName> --force

在这里插入图片描述

部署

基于Patroni的PostgreSQL高可用环境部署
在这里插入图片描述

failsafe可以解决此问题
在这里插入图片描述
etcd性能差的情况下可以适当调整该参数。
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值