PostgreSQL patroni高可用

PostgreSQL patroni 高可用 1:ectd 安装和配置
PostgreSQL patroni 高可用 2:patroni安装和配置
PostgreSQL patroni 高可用 3:patroni 运维

 

PostgreSQL patroni 高可用 1:etcd 安装

PostgreSQL ptroni的高可用架构图如下所示,本文完成如下架构图中红色标记内的ectd分布式存储的安装和配置。

图片来源于:https://docs.percona.com/postgresql/12/solutions/high-availability.html#architecture-layout

 

1,服务器环境

Ubuntu08:192.168.152.115
Ubuntu09:192.168.152.116
Ubuntu10:192.168.152.117
 

1.1 ectd下载与安装

以下在Ubuntu08,Ubuntu09,Ubuntu10分别安装

下载与解压安装wget https://github.com/etcd-io/etcd/releases/download/v3.6.5/etcd-v3.6.5-linux-amd64.tar.gztar xzvf etcd-v3.6.5-linux-amd64.tar.gz -C /usr/localln -s /usr/local/etcd-v3.6.5-linux-amd64/ /usr/local/etcd建立软连接ln -s /usr/local/etcd/etcd /usr/bin/etcdln -s /usr/local/etcd/etcdctl /usr/bin/etcdctletcd --version/*输出:etcd Version: 3.6.5Git SHA: a061450Go Version: go1.24.7Go OS/Arch: linux/amd64*/建立数据文件路径mkdir -p /var/lib/etcd mkdir /var/lib/etcd/default.etcd

 

1.2 etcd 配置文件

Ubuntu08:192.168.152.115节点,路径:/var/lib/etcd/etcd.conf

#etcd名称,自定义ETCD_NAME="etcd01"#存放etcd数据的目录,自定义ETCD_DATA_DIR="/var/lib/etcd/default"#监听URL,用户客户端和SERVER进行通信ETCD_LISTEN_CLIENT_URLS="http://192.168.152.115:2379,http://127.0.0.1:2379"#告知客户端自身的URL,TCP 2379端口用于监听客户端请求ETCD_ADVERTISE_CLIENT_URLS="http://192.168.152.115:2379"#监听URL,用于和其他节点通信ETCD_LISTEN_PEER_URLS="http://192.168.152.115:2380"#告知集群其他节点,端口2380用于集群通信ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.152.115:2380"#定义了集群内所有成员ETCD_INITIAL_CLUSTER="etcd01=http://192.168.152.115:2380,etcd02=http://192.168.152.116:2380,etcd03=http://192.168.152.117:2380"#集群ID,唯一标识ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"#集群状态,new为新创建集群,existing为已经存在的集群ETCD_INITIAL_CLUSTER_STATE="new"

Ubuntu09:192.168.152.116节点,路径:/var/lib/etcd/etcd.conf

#etcd名称,自定义ETCD_NAME="etcd02"#存放etcd数据的目录,自定义ETCD_DATA_DIR="/var/lib/etcd/default"#监听URL,用户客户端和SERVER进行通信ETCD_LISTEN_CLIENT_URLS="http://192.168.152.116:2379,http://127.0.0.1:2379"#告知客户端自身的URL,TCP 2379端口用于监听客户端请求ETCD_ADVERTISE_CLIENT_URLS="http://192.168.152.116:2379"#监听URL,用于和其他节点通信ETCD_LISTEN_PEER_URLS="http://192.168.152.116:2380"#告知集群其他节点,端口2380用于集群通信ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.152.116:2380"#定义了集群内所有成员ETCD_INITIAL_CLUSTER="etcd01=http://192.168.152.115:2380,etcd02=http://192.168.152.116:2380,etcd03=http://192.168.152.117:2380"#集群ID,唯一标识ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"#集群状态,new为新创建集群,existing为已经存在的集群ETCD_INITIAL_CLUSTER_STATE="new"

Ubuntu10:192.168.152.117节点,路径:/var/lib/etcd/etcd.conf

#etcd名称,自定义ETCD_NAME="etcd03"#存放etcd数据的目录,自定义ETCD_DATA_DIR="/var/lib/etcd/default"#监听URL,用户客户端和SERVER进行通信ETCD_LISTEN_CLIENT_URLS="http://192.168.152.117:2379,http://127.0.0.1:2379"#告知客户端自身的URL,TCP 2379端口用于监听客户端请求ETCD_ADVERTISE_CLIENT_URLS="http://192.168.152.117:2379"#监听URL,用于和其他节点通信ETCD_LISTEN_PEER_URLS="http://192.168.152.117:2380"#告知集群其他节点,端口2380用于集群通信ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.152.117:2380"#定义了集群内所有成员ETCD_INITIAL_CLUSTER="etcd01=http://192.168.152.115:2380,etcd02=http://192.168.152.116:2380,etcd03=http://192.168.152.117:2380"#集群ID,唯一标识ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"#集群状态,new为新创建集群,existing为已经存在的集群ETCD_INITIAL_CLUSTER_STATE="new"

 

1.3 etcd systemctl 服务脚本

三台服务器一样,/etc/systemd/system/ectd.service

[Unit]Description=Etcd ServerAfter=network.targetAfter=network-online.targetWants=network-online.target[Service]Type=notifyEnvironmentFile=/var/lib/etcd/etcd.confWorkingDirectory=/var/lib/etcd/ExecStart=/usr/bin/etcd Restart=on-failureRestartSec=5LimitNOFILE=65536[Install]WantedBy=multi-user.target

启动服务

systemctl daemon-reloadsystemctl enable etcdsystemctl start etcdsystemctl status etcd

 

1.4 etcd 环境变量

--添加环境变量--ETCDCTL_API是有v2和v3两个版本,默认是v2版本,执行命令可能会报错echo 'export ETCDCTL_API=3' >> /etc/profilesource /etc/profile

其他

--新增etcd节点,systemd启动服务出现Job for etcd.service failed because a timeout was exceeded错误--解决办法:出现该问题的原因可能时因为etcd的data-dir中的残留信息导致的,删除data-dir中的内容,重新启动etcd服务rm -rf /var/lib/etcd/default/

 

2,检查etcd集群管理

2.1 etcd状态检查

检查集群节点状态:etcdctl endpoint health --cluster -w table
列出集群成员:etcdctl member list -w table
检查集群节点的健康状态:etcdctl endpoint health --cluster -w table
检查集群节点状态root@ubuntu08:/usr/local# etcdctl endpoint status --cluster -w table{"level":"warn","ts":"2025-09-22T09:34:53.831953+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+|          ENDPOINT           |        ID        | VERSION | STORAGE VERSION | DB SIZE | IN USE | PERCENTAGE NOT IN USE | QUOTA | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | DOWNGRADE TARGET VERSION | DOWNGRADE ENABLED |+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+| http://192.168.152.116:2379 | 6eb726f58bd2aa70 |   3.6.5 |           3.6.0 |   20 kB |  16 kB |                   20% |   0 B |     false |      false |         2 |          9 |                  9 |        |                          |             false || http://192.168.152.115:2379 | 7158c1b754a1eeba |   3.6.5 |           3.6.0 |   25 kB |  16 kB |                   34% |   0 B |      true |      false |         2 |          9 |                  9 |        |                          |             false || http://192.168.152.117:2379 | af96143dba13346f |   3.6.5 |           3.6.0 |   20 kB |  16 kB |                   20% |   0 B |     false |      false |         2 |          9 |                  9 |        |                          |             false |+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+列出集群成员root@ubuntu08:/usr/local# etcdctl member list -w table{"level":"warn","ts":"2025-09-22T09:36:22.799237+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}+------------------+---------+--------+-----------------------------+-----------------------------+------------+|        ID        | STATUS  |  NAME  |         PEER ADDRS          |        CLIENT ADDRS         | IS LEARNER |+------------------+---------+--------+-----------------------------+-----------------------------+------------+| 6eb726f58bd2aa70 | started | etcd02 | http://192.168.152.116:2380 | http://192.168.152.116:2379 |      false || 7158c1b754a1eeba | started | etcd01 | http://192.168.152.115:2380 | http://192.168.152.115:2379 |      false || af96143dba13346f | started | etcd03 | http://192.168.152.117:2380 | http://192.168.152.117:2379 |      false |+------------------+---------+--------+-----------------------------+-----------------------------+------------+检查集群节点的健康状态root@ubuntu08:/usr/local#  etcdctl endpoint health --cluster -w table{"level":"warn","ts":"2025-09-22T09:36:49.159370+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}+-----------------------------+--------+------------+-------+|          ENDPOINT           | HEALTH |    TOOK    | ERROR |+-----------------------------+--------+------------+-------+| http://192.168.152.115:2379 |   true | 4.838419ms |       || http://192.168.152.117:2379 |   true | 7.374066ms |       || http://192.168.152.116:2379 |   true | 7.334373ms |       |+-----------------------------+--------+------------+-------+

 

2.2 etcd常用命令

etcd常用命令--截至目前位置ectd中没有用户自定义数据,如下查询是启用了patroni之后才能查到,这里仅做示例-- 查询所有的keyroot@ubuntu08:/usr/local# etcdctl get "" --prefix --keys-only{"level":"warn","ts":"2025-09-23T09:36:04.000716+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}/service/pg_cluster_wy_prod/config/service/pg_cluster_wy_prod/history/service/pg_cluster_wy_prod/initialize/service/pg_cluster_wy_prod/leader/service/pg_cluster_wy_prod/members/ubuntu09/service/pg_cluster_wy_prod/status--删除所有的keyroot@ubuntu08:/usr/local# etcdctl del /service/pg_cluster_wy_prod/ --prefix{"level":"warn","ts":"2025-09-23T09:38:52.248350+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}6

 

2.3 管理etcd

--查看集群节点:etcdctl endpoint status --cluster -w table
--转移领导权: etcdctl move-leader 6eb726f58bd2aa70
--备份数据库:etcdctl snapshot save etcd_bak.db,默认备份到当前目录
管理etcd--查看集群节点root@ubuntu08:/usr/local# etcdctl endpoint status --cluster -w table{"level":"warn","ts":"2025-09-22T09:52:07.064600+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+|          ENDPOINT           |        ID        | VERSION | STORAGE VERSION | DB SIZE | IN USE | PERCENTAGE NOT IN USE | QUOTA | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | DOWNGRADE TARGET VERSION | DOWNGRADE ENABLED |+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+| http://192.168.152.116:2379 | 6eb726f58bd2aa70 |   3.6.5 |           3.6.0 |   20 kB |  16 kB |                   20% |   0 B |     false |      false |         2 |         12 |                 12 |        |                          |             false || http://192.168.152.115:2379 | 7158c1b754a1eeba |   3.6.5 |           3.6.0 |   25 kB |  16 kB |                   34% |   0 B |      true |      false |         2 |         12 |                 12 |        |                          |             false || http://192.168.152.117:2379 | af96143dba13346f |   3.6.5 |           3.6.0 |   20 kB |  16 kB |                   20% |   0 B |     false |      false |         2 |         12 |                 12 |        |                          |             false |+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+--将领导权转移到192.168.152.116,需要在leader节点(192.168.152.115)上执行以下命令root@ubuntu08:/usr/local#  etcdctl move-leader 6eb726f58bd2aa70{"level":"warn","ts":"2025-09-22T09:52:52.764611+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}Leadership transferred from 7158c1b754a1eeba to 6eb726f58bd2aa70root@ubuntu08:/usr/local#--再次查看集群节点root@ubuntu08:/usr/local# etcdctl endpoint status --cluster -w table{"level":"warn","ts":"2025-09-22T09:53:25.957168+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+|          ENDPOINT           |        ID        | VERSION | STORAGE VERSION | DB SIZE | IN USE | PERCENTAGE NOT IN USE | QUOTA | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS | DOWNGRADE TARGET VERSION | DOWNGRADE ENABLED |+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+| http://192.168.152.116:2379 | 6eb726f58bd2aa70 |   3.6.5 |           3.6.0 |   20 kB |  16 kB |                   20% |   0 B |      true |      false |         3 |         13 |                 13 |        |                          |             false || http://192.168.152.115:2379 | 7158c1b754a1eeba |   3.6.5 |           3.6.0 |   25 kB |  16 kB |                   34% |   0 B |     false |      false |         3 |         13 |                 13 |        |                          |             false || http://192.168.152.117:2379 | af96143dba13346f |   3.6.5 |           3.6.0 |   20 kB |  16 kB |                   20% |   0 B |     false |      false |         3 |         13 |                 13 |        |                          |             false |+-----------------------------+------------------+---------+-----------------+---------+--------+-----------------------+-------+-----------+------------+-----------+------------+--------------------+--------+--------------------------+-------------------+备份数据库,执行etcdctl snapshot save etcd_bak.db,默认备份到当前目录root@ubuntu08:/usr/local#  etcdctl snapshot save etcd_bak.db{"level":"warn","ts":"2025-09-22T09:54:51.963842+0800","caller":"flags/flag.go:94","msg":"unrecognized environment variable","environment-variable":"ETCDCTL_API=3"}{"level":"info","ts":"2025-09-22T09:54:51.971594+0800","caller":"snapshot/v3_snapshot.go:83","msg":"created temporary db file","path":"etcd_bak.db.part"}{"level":"info","ts":"2025-09-22T09:54:51.973601+0800","logger":"client","caller":"[email protected]/maintenance.go:236","msg":"opened snapshot stream; downloading"}{"level":"info","ts":"2025-09-22T09:54:51.975135+0800","caller":"snapshot/v3_snapshot.go:96","msg":"fetching snapshot","endpoint":"127.0.0.1:2379"}{"level":"info","ts":"2025-09-22T09:54:51.975385+0800","logger":"client","caller":"[email protected]/maintenance.go:302","msg":"completed snapshot read; closing"}{"level":"info","ts":"2025-09-22T09:54:51.976335+0800","caller":"snapshot/v3_snapshot.go:111","msg":"fetched snapshot","endpoint":"127.0.0.1:2379","size":"25 kB","took":"4.643252ms","etcd-version":"3.6.0"}{"level":"info","ts":"2025-09-22T09:54:51.976404+0800","caller":"snapshot/v3_snapshot.go:121","msg":"saved","path":"etcd_bak.db"}Snapshot saved at etcd_bak.dbServer version 3.6.0
 
本站提供的所有下载资源均来自互联网,仅提供学习交流使用,版权归原作者所有。如需商业使用,请联系原作者获得授权。 如您发现有涉嫌侵权的内容,请联系我们 邮箱:[email protected]