ToziuhaNight:德古拉的复仇免安装绿色版
543M · 2025-09-28
PostgreSQL patroni高可用
PostgreSQL patroni 高可用 1:ectd 安装和配置
PostgreSQL patroni 高可用 2:patroni安装和配置
PostgreSQL patroni 高可用 3:patroni 运维
PostgreSQL patroni 高可用 1:etcd 安装
图片来源于:https://docs.percona.com/postgresql/12/solutions/high-availability.html#architecture-layout
以下在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
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"
三台服务器一样,/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
--添加环境变量--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/
检查集群节点状态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 | |+-----------------------------+--------+------------+-------+
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
管理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
543M · 2025-09-28
76.3M · 2025-09-28
241M · 2025-09-28