PostgreSQL 高可用管理工具之xxx(pgsql高可用方案)
liuian 2025-07-06 14:04 40 浏览
Patroni & Etcd
环境说明
项目 | 说明 | 备注 |
操作系统 | RockyLinux8 | |
系统内核 | 5.14.0-362.8.1.el9_3.x86_64 | |
Patroni | 4.0.0 | /opt/patroni/patroni |
Etcd | 3.5.15 | /opt/etcd-3.5.15 |
PostgreSQL | 14.9 | |
主机IP | 10.16.18.160~10.16.18.162 |
安装配置Etcd
Etcd 是一个可靠的分布式 key-value 存储系统,主要用于配置共享和服务注册和发现。Patroni主要使用其存储PostgreSQL集群的信息。
以下操作需要在三台机器上都要执行。
下载安装包
cd /opt
wget https://github.com/etcd-io/etcd/releases/download/v3.5.15/etcd-v3.5.15-linux-amd64.tar.gz解压
tar xzf etcd-v3.5.15-linux-amd64.tar.gz修改下目录名称
mv etcd-v3.5.15-linux-amd64 etcd-3.5.15创建一个日志目录
mkdir -p /opt/etcd-3.5.15/logsPG01上添加配置文件
/opt/etcd-3.5.15/etcd.conf
name: patroni01
data-dir: /opt/etcd-3.5.15
listen-peer-urls: http://10.16.18.160:2380
listen-client-urls: http://10.16.18.160:2379,http://127.0.0.1:2379
initial-cluster-state: new
initial-cluster-token: etcd-cluster
advertise-client-urls: http://10.16.18.160:2379
initial-advertise-peer-urls: http://10.16.18.160:2380
initial-cluster: patroni01=http://10.16.18.160:2380,patroni02=http://10.16.18.161:2380,patroni03=http://10.16.18.162:2380如果添加enable-v2: true,那么对应的Patroni配置文件中应当是etcd,而不是etcd3。
PG02上添加配置文件
/opt/etcd-3.5.15/etcd.conf
name: patroni02
data-dir: /opt/etcd-3.5.15
listen-peer-urls: http://10.16.18.161:2380
listen-client-urls: http://10.16.18.161:2379,http://127.0.0.1:2379
initial-cluster-state: new
initial-cluster-token: etcd-cluster
advertise-client-urls: http://10.16.18.161:2379
initial-advertise-peer-urls: http://10.16.18.161:2380
initial-cluster: patroni01=http://10.16.18.160:2380,patroni02=http://10.16.18.161:2380,patroni03=http://10.16.18.162:2380PG03上添加配置文件
/opt/etcd-3.5.15/etcd.conf
name: patroni03
data-dir: /opt/etcd-3.5.15
listen-peer-urls: http://10.16.18.162:2380
listen-client-urls: http://10.16.18.162:2379,http://127.0.0.1:2379
initial-cluster-state: new
initial-cluster-token: etcd-cluster
advertise-client-urls: http://10.16.18.162:2379
initial-advertise-peer-urls: http://10.16.18.162:2380
initial-cluster: patroni01=http://10.16.18.160:2380,patroni02=http://10.16.18.161:2380,patroni03=http://10.16.18.162:2380在三台机器上配置系统服务
/usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target
WorkingDirectory=/opt/etcd-3.5.15/etcd
[Service]
User=root
Type=notify
ExecStart=/opt/etcd-3.5.15/etcd --config-file /opt/etcd-3.5.15/etcd.conf --log-level error
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000
StandardOutput=file:/opt/etcd-3.5.15/logs/etcd.log
StandardError=file:/opt/etcd-3.5.15/logs/etcd-error.log
[Install]
WantedBy=multi-user.target在三台机器上启动etcd服务,初始化etcd数据库
systemct daemon-reload && systemctl start etcd如果想设置为开机自启,加上systemctl enable etcd
查看节点的状态
/opt/etcd-3.5.15/etcdctl endpoint health --cluster -w table[root@PG01 opt]# /opt/etcd-3.5.15/etcdctl endpoint health --cluster -w table
+--------------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+--------------------------+--------+------------+-------+
| http://10.16.18.160:2379 | true | 1.018523ms | |
| http://10.16.18.162:2379 | true | 1.247401ms | |
| http://10.16.18.161:2379 | true | 1.384808ms | |
+--------------------------+--------+------------+-------+看到输出结果如上说明成功了,否则查看日志文件
/opt/etcd-3.5.15/logs/etcd-error.log查找原因。
网上有些配置文件示例是下面这样的,这种配置是环境变量的方式,不能通过etcd --config-file的方式执行。
#[Member]
ETCD_NAME="patroni01"
ETCD_DATA_DIR="/opt/etcd-3.5.15/data"
ETCD_LISTEN_PEER_URLS="http://10.16.18.160:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.16.18.160:2379,http://127.0.0.1:2379"
#[Clustering]
ETCD_ENABLE_V2=true
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://10.16.18.160:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.16.18.160:2380"
ETCD_INITIAL_CLUSTER="patroni01=http://10.16.18.160:2380,patroni02=http://10.16.18.161:2380,patroni03=http://10.16.18.162:2380"与其对应的是在
/usr/lib/systemd/system/etcd.service中增加EnvironmentFile
[Service]
EnvironmentFile=-/opt/etcd-v3.5.0/config/etcd.conf安装配置Patroni
以下操作三台机器上都要执行。
安装Python3
需要先安装Python3,网上类似的文章很多,这里不再赘述,或者直接使用中启乘数提供的编译好的Python包(是给他们CLup软件用的),我之前装过,所以这里不再安装Python环境。
这里给一个安装示例,只有el7和el8的版本
cd /opt
wget https://gitee.com/csudata/csupy3.9.16/releases/download/1.0/csupy3.9.16.el8.tar.xz
tar xf csupy3.9.16.el8.tar.xzel7的话替换下el8即可。
如果是其他的操作系统,又不想折腾编译Python3,也可以装一个CLup的开源版本(另一款PostgreSQL高可用软件,带有Web界面,建议有时间的话可以装一套用用),软件安装时会自动安装一个Python3的环境。
其安装命令如下,参考文档:
https://www.csudata.com/clup/manual/5.x/10147
wget -qO /tmp/clup.sh --no-check-certificate https://get.csudata.com/csuinst/clup.sh && bash /tmp/clup.sh openclup install在其中一套安装即可,如果想要试用CLup的话在其他机器上安装其Agent端,要不就是直接tar一下Python3环境,然后scp过去再解压。
上面的tar包解压或是安装CLup后的Python环境如下
.
├── csupy3.9.16
└── csu_pyenv- csupy3.9.16:Python3的软件目录
- csu_pyenv:安装了工具包的虚拟环境
需要先添加下环境变量~/.bashrc
export PATH=/opt/csupy3.9.16/bin:$PATH
export LD_LIBRARY_PATH=/opt/csupy3.9.16/lib:$LD_LIBRARY_PATH注意替换路径为自己的实际路径。
安装Patroni
Patroni安装比较简单,直接pip安装,下面还需安装psycopg2-binary(Python 连接PostgreSQL的库)
pip3 install psycopg2-binary -i https://mirrors.aliyun.com/pypi/simple/
pip3 install patroni[etcd] -i https://mirrors.aliyun.com/pypi/simple/配置Patroni
创建目录
mkdir -p /opt/patroni/conf
mkdir -p /opt/patroni/logsPG01上添加配置文件
/opt/patroni/conf/patroni_pg.yml
scope: pg14-cluster
name: patroni01
# log
log:
level: INFO
traceback_level: ERROR
dir: /opt/patroni/logs
file_num: 10
# file_size: 26214400
restapi:
listen: 10.16.18.160:8008
connect_address: 10.16.18.160:8008
etcd3:
hosts: 10.16.18.160:2379,10.16.18.161:2379,10.16.18.162:2379
bootstrap:
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
# and all other cluster members will use it as a `global configuration`
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: "*"
port: 5414
wal_level: logical
hot_standby: "on"
max_wal_senders: 10
max_replication_slots: 10
wal_log_hints: "on"
postgresql:
listen: 10.16.18.160:5414
connect_address: 10.16.18.160:5414
database: template1
data_dir: /data/pgdata14
bin_dir: /usr/csupg-14.9/bin
pgpass: /home/pg14/.pgpass
#callbacks:
#on_start:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
#on_stop:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
#on_role_change:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
authentication:
replication:
username: postgres
password: postgres
superuser:
username: postgres
password: postgres
rewind:
username: postgres
password: postgres
watchdog:
mode: off # Allowed values: off, automatic, required
device: /dev/watchdog
safety_margin: 5
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: falsePG02上添加配置文件
/opt/patroni/conf/patroni_pg.yml
scope: pg14-cluster
name: patroni02
# log
log:
level: INFO
traceback_level: ERROR
dir: /opt/patroni/logs
file_num: 10
# file_size: 26214400
restapi:
listen: 10.16.18.161:8008
connect_address: 10.16.18.161:8008
etcd3:
hosts: 10.16.18.160:2379,10.16.18.161:2379,10.16.18.162:2379
bootstrap:
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
# and all other cluster members will use it as a `global configuration`
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: "*"
port: 5414
wal_level: logical
hot_standby: "on"
max_wal_senders: 10
max_replication_slots: 10
wal_log_hints: "on"
postgresql:
listen: 10.16.18.161:5414
connect_address: 10.16.18.161:5414
database: template1
data_dir: /data/pgdata14
bin_dir: /usr/csupg-14.9/bin
pgpass: /home/pg14/.pgpass
#callbacks:
#on_start:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
#on_stop:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
#on_role_change:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
authentication:
replication:
username: postgres
password: postgres
superuser:
username: postgres
password: postgres
rewind:
username: postgres
password: postgres
watchdog:
mode: off # Allowed values: off, automatic, required
device: /dev/watchdog
safety_margin: 5
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: falsePG03上添加配置文件
/opt/patroni/conf/patroni_pg.yml`
scope: pg14-cluster
name: patroni03
# log
log:
level: INFO
traceback_level: ERROR
dir: /opt/patroni/logs
file_num: 10
# file_size: 26214400
restapi:
listen: 10.16.18.162:8008
connect_address: 10.16.18.162:8008
etcd3:
hosts: 10.16.18.160:2379,10.16.18.161:2379,10.16.18.162:2379
bootstrap:
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
# and all other cluster members will use it as a `global configuration`
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: "*"
port: 5414
wal_level: logical
hot_standby: "on"
max_wal_senders: 10
max_replication_slots: 10
wal_log_hints: "on"
postgresql:
listen: 10.16.18.162:5414
connect_address: 10.16.18.162:5414
database: template1
data_dir: /data/pgdata14
bin_dir: /usr/csupg-14.9/bin
pgpass: /home/pg14/.pgpass
#callbacks:
#on_start:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
#on_stop:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
#on_role_change:/u01/app/halo/product/shield/patroni/scripts/patroni_callback.sh
authentication:
replication:
username: postgres
password: postgres
superuser:
username: postgres
password: postgres
rewind:
username: postgres
password: postgres
watchdog:
mode: off # Allowed values: off, automatic, required
device: /dev/watchdog
safety_margin: 5
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false系统服务
/usr/lib/systemd/system/patroni.service
[Unit]
Description=Patroni Cluster
After=syslog.target network.target
[Service]
Type=simple
User=pg14
Group=pg14
# Set PATH LD_LIBRARY_PATH, they will be passed to OS env variables when call pg_ctl by patroni
Environment="LD_LIBRARY_PATH=/usr/csupg-14.9/lib:/opt/csupy3.9.16/lib"
# Start the patroni process
ExecStart=/opt/csupy3.9.16/bin/patroni /opt/patroni/conf/patroni_pg.yml
# Send HUP to reload from patroni.yml
ExecReload=/bin/kill -s HUP $MAINPID
# only kill the patroni process, not it's children, so it will gracefully stop Halo
KillMode=process
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=30
# Do not restart the service if it crashes, we want to manually inspect database on failure
Restart=no
[Install]
WantedBy=multi-user.target注意替换Environment中的值为自己的实际路径。
修改文件属主
chown -R pg14:pg14 /opt/patroni启动服务
systemctl daemon-reload && systemctl start patroni查看集群的状态
patronictl -c /opt/patroni/conf/patroni_pg.yml list这里Leader节点显示有些参数会在重启后生效,实际上是patroni在启动时会根据patroni的配置文件修改数据的参数。
剩下的两个Replica节点没有显示未生效的参数的原因是中间启动备节点的patroni有问题,后面重搭了备库,需要注意的是执行pg_basebackup后要修改下参数
# postgresql.conf
listen_addresses = 10.16.18.161
# postgresql.auto.conf
primary_conninfo="application_name=patroni02 ..."注意:这里的地址换成自己的当前备库的实际地址,primary_conninfo中是增加application_name=,而不是配置成上面那样。
问题解决
etcd启动时报错
[root@PG01 logs]# /opt/etcd-3.5.15/etcd --config-file /opt/etcd-3.5.15/etcd.conf --log-level debug
{"level":"info","ts":"2024-08-30T10:00:16.034302+0800","caller":"etcdmain/etcd.go:73","msg":"Running: ","args":["/opt/etcd-3.5.15/etcd","--config-file","/opt/etcd-3.5.15/etcd.conf","--log-level","debug"]}
{"level":"warn","ts":"2024-08-30T10:00:16.034328+0800","caller":"etcdmain/etcd.go:75","msg":"failed to verify flags","error":"error unmarshaling JSON: while decoding JSON: json: cannot unmarshal string into Go struct field configYAML.log-outputs of type []string"}检查配置文件,可能有些参数名称不对或者设置不对。
cluster id 不对
问题描述:
启动Patroni后日志中总是报错:
node patroni02 belongs to a different cluster: 7223643286119069904 !=7384703923858607267
原因:
可能是旧的集群数据没有清理掉
解决版本:清理旧集群数据,所有节点都要做
- 停止Patroni服务
- systemctl stop patroni
- 停止etcd服务
- systemctl stop etcd
- 清理或者重命名旧集群数据
- cd /etc/etcdxxx/
mv data data_old - 启动etcd
- systemctl start etcd
- 启动patroni
- systemctl start patroni
相关推荐
- bizhub15打印机驱动下载(bizhub打印机驱动安装)
-
1、请用USB数据线连接复印机和电脑。 2、打开电脑,然后到复印机的官网下载当前系统的驱动程序,然后点击安装。 3、安装完成后,点击打开打印机和传真,就可以到看扫描仪的图标。 4、找个要扫描的内...
- win7电脑截屏(windows7电脑截屏)
-
在Win7系统中,自带的截图快捷键是“PrtScn”键,即PrintScreen键。按下这个键后,系统会将当前屏幕的内容复制到剪贴板中,然后用户可以将其粘贴到其他应用程序中进行编辑或保存。此外,Wi...
- win10电脑所有软件都打不开(win10任何软件都打不开)
-
具体步骤如下:萊垍頭條1、如果遇到这类情况,你先看下快捷键alt+tab键能否查看,并把鼠标放在任务栏的图标上,或者查看一下窗口的缩略图。萊垍頭條2、我们将鼠标放在任务栏上,选中打不开的软件,然后al...
- 如何创建电子邮件账号(如何创建电子邮件账号在outlook中)
-
用QQ号的一键激活邮箱几乎是最快,最简单的注册邮箱手段了,且QQ邮箱功能强大,安全方便,推荐你使用,具体注册方法如下:1、你可以点击QQ面板邮箱快捷按钮,直接激活邮箱。2、如果你没有QQ,直接申请QQ...
- 戴尔音频驱动下载(戴尔电脑声卡驱动下载)
-
1、如果是笔记本没有音频设备的话,并不是没有输出设备,而是我们没有启用或者没有安装音频驱动导致的。先打开控制面板。2、打开控制面板之后下面依次找到音频清晰管理器,并且打开。3、打开之后我们这里把主音量...
- toshiba硬盘(TOSHIBA硬盘tlc)
-
东芝移动硬盘a3好,性价比很高,传输速率高,稳定耐用,安全高效外壳是磨砂质感!USB3.0,即插即用采用NTFS格式,兼容Windwos10、Windwos8.1、Windwos7,格式化后可兼容M...
- 完整版xp系统下载(xp系统最新版本安装包)
-
2012年前的可以无压力安装XP系统,搜索:itellyou.cn这里有WINDOWS几乎所有的系统。windowsXP系统升级的具体操作步骤如下:1、首先我们将老毛桃装机工具下载到U盘,将老毛桃...
- ps下载电脑版官方下载(ps电脑版下载地址)
-
目前在电脑上免费下载PS是不太可能的。主要有以下几个原因。1.AdobePhotoshop(简称PS)是一款商业软件,它需要用户购买和激活许可证才能合法使用。从正规渠道下载并且获得合法授权需要付费...
- 迅猛兔加速器(迅猛兔加速器官网)
-
要下载迅猛兔加速器,首先需要在官网或其他可信的下载平台上搜索并找到该软件。一般情况下,官网提供的下载链接是最稳定和安全的选择。在下载之前,确保您的电脑或手机系统能够支持使用此软件,并检查下载链接的文件...
- 台式电脑怎么重做系统(台式电脑怎么重装系统)
-
你好,电脑系统重装的步骤如下:1.备份数据:在重装系统之前,需要备份电脑中的重要数据,以免数据丢失。2.准备安装介质:需要准备一个安装介质,可以是光盘、U盘或者硬盘分区镜像等。3.设置启动顺序:将电脑...
-
- 电脑无法从u盘启动怎么办(电脑无法从u盘启动解决方法)
-
电脑的进入不了u盘启动的解决方法:一、我们第一步需要确定的是你的u盘在别的电脑上检查一下U盘是否可读,如果可读的话是否成功制作了u盘启动盘了,因为想要启动进入pe的话需要u盘具备启动的功能。 二、如果你检查好自己的u盘已经成功制作了启动盘...
-
2026-01-13 10:05 liuian
- cpu频率越高越好吗(cpu频率越高速度越快吗)
-
高好。CPU的频率是影响CPU的一个重要因素,直观上来说,频率的高低影响了CPU的性能。频率越高,CPU性能越好;不过需要注意的是,CPU的主频表示在CPU内数字脉冲信号震荡的速度,与CPU实际的运算...
- 注册表清理软件(注册表清理软件残留软件)
-
你好!关于注册表清理工具的推荐,以下是几个值得推荐的工具:1.CCleaner:这是一款功能强大的免费清理工具,可以有效地清理注册表、垃圾文件等,使用简单方便。2.WiseRegistryCl...
- 显卡驱动升级有好处吗(显卡驱动升级有什么坏处)
-
显卡的新版本驱动能修改一些游戏,图形显示的BUG,所以新版本的显卡驱动能有效的利用显卡的资源,提高游戏性能。不仅可以修正旧版本中的BUG,而且可以进一步挖掘显卡硬件的功能,使得部分硬件功能得以充分发挥...
- 一周热门
-
-
飞牛OS入门安装遇到问题,如何解决?
-
如何在 iPhone 和 Android 上恢复已删除的抖音消息
-
Boost高性能并发无锁队列指南:boost::lockfree::queue
-
大模型手册: 保姆级用CherryStudio知识库
-
用什么工具在Win中查看8G大的log文件?
-
如何在 Windows 10 或 11 上通过命令行安装 Node.js 和 NPM
-
威联通NAS安装阿里云盘WebDAV服务并添加到Infuse
-
Trae IDE 如何与 GitHub 无缝对接?
-
idea插件之maven search(工欲善其事,必先利其器)
-
如何修改图片拍摄日期?快速修改图片拍摄日期的6种方法
-
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)
