01-patron安装
【摘要】 CentOS Linux release 7.8.2003 (Core)VERSION = PostgreSQL 13.4patroni 2.1.1etcd Version: 3.3.11#https://github.com/zalando/patroni依赖软件:yum install -y haproxyyum install -y watchdogyum install -y li...
CentOS Linux release 7.8.2003 (Core)
VERSION = PostgreSQL 13.4
patroni 2.1.1
etcd Version: 3.3.11
依赖软件:
yum install -y haproxy
yum install -y watchdog
yum install -y libnfnetlink
yum install -y postgresql-server.x86_64
yum install postgresql-devel* -y
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum install gcc.x86_64 -y
yum install -y libpqxx-devel.x86_64
# 安装软件,linux内置功能
yum install -y watchdog
# 初始化watchdog字符设备
modprobe softdog
# 修改/dev/watchdog设备权限
chmod 666 /dev/watchdog
# 启动watchdog服务
systemctl start watchdog
systemctl enable watchdog
#参考手册:
https://patroni.readthedocs.io/en/latest/README.html
YUM 安装python:
yum install python36 python36-devel -y
curl https://bootstrap.pypa.io/get-pip.py -o /tmp/get-pip.py
python3.6 /tmp/get-pip.py
#psycopg2 库是 python 用来操作 postgreSQL 数据库的第三方库。
#安装之前,先安装完成pg数据库软件。(Error: pg_config executable not found.)
#https://pypi.org/project/psycopg2/
pip install psycopg2-binary
#python-etcd module in order to use Etcd as DCS
pip3 install patroni[etcd]
01,python 3.6安装
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
mkdir /usr/local/python3
tar xvf Python-3.6.12.tgz
cd Python-3.6.12
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3.6 /usr/local/bin/
ln -s /usr/local/python3/bin/pip3.6 /usr/local/bin
02,patroni安装
#在线安装(需要连接互联网)
pip3.6 install psycopg2-binary
pip3.6 install patroni[etcd]
###离线下载和安装:
pip3.6 download -d s01/ psycopg2-binary
pip3.6 download -d s002/ patroni[etcd]
#下载制定版本
#https://pypi.org/project/psycopg2-binary/#history
#https://pypi.org/project/patroni/#history
pip3.6 download psycopg2-binary==2.8
==文件拷贝到离线服务器就可以:
pip3.6 install s01/*
pip3.6 install s002/*
#离线安装日志:
[root@db03 soft]# pip3.6 install s01/*
Processing ./s01/psycopg2-binary-2.9.1.tar.gz
Installing collected packages: psycopg2-binary
Running setup.py install for psycopg2-binary ... done
Successfully installed psycopg2-binary-2.9.1
[root@db03 soft]# pip3.6 install s002/*
Processing ./s002/psutil-5.8.0.tar.gz
Processing ./s002/python-etcd-0.4.5.tar.gz
Processing ./s002/ydiff-1.2.tar.gz
Processing ./s002/click-8.0.3-py3-none-any.whl
Processing ./s002/dnspython-2.1.0-py3-none-any.whl
Processing ./s002/importlib_metadata-4.8.1-py3-none-any.whl
Processing ./s002/patroni-2.1.1-py3-none-any.whl
Processing ./s002/prettytable-2.2.1-py3-none-any.whl
Processing ./s002/python_dateutil-2.8.1-py2.py3-none-any.whl
Processing ./s002/PyYAML-6.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Processing ./s002/six-1.16.0-py2.py3-none-any.whl
Processing ./s002/typing_extensions-3.10.0.2-py3-none-any.whl
Processing ./s002/urllib3-1.26.7-py2.py3-none-any.whl
Processing ./s002/wcwidth-0.2.5-py2.py3-none-any.whl
Processing ./s002/zipp-3.6.0-py3-none-any.whl
Installing collected packages: zipp, typing-extensions, importlib-metadata, click, dnspython, urllib3, wcwidth, prettytable, six, PyYAML, ydiff, psutil, python-dateutil, patroni, python-etcd
Running setup.py install for ydiff ... done
Running setup.py install for psutil ... done
Running setup.py install for python-etcd ... done
Successfully installed PyYAML-6.0 click-8.0.3 dnspython-2.1.0 importlib-metadata-4.8.1 patroni-2.1.1 prettytable-2.2.1 psutil-5.8.0 python-dateutil-2.8.1 python-etcd-0.4.5 six-1.16.0 typing-extensions-3.10.0.2 urllib3-1.26.7 wcwidth-0.2.5 ydiff-1.2 zipp-3.6.0
安装位置:
#/usr/local/python3/bin/patroni
ln -s /usr/local/python3/bin/patronictl /usr/local/bin/
ln -s /usr/local/python3/bin/patroni /usr/local/bin/
设置环境变量:
cd /etc/ld.so.conf.d/
echo "/usr/local/postgresql/lib" >>pg.conf
ldconfig
/usr/local/python3/bin/patroni --version
报错信息:
-bash-4.2# ./patroni --help
FATAL: Patroni requires psycopg2>=2.5.4 or psycopg2-binary
-bash-4.2# ./python3.6
Python 3.6.12 (default, Nov 1 2021, 01:19:57)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import psycopg2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.6/site-packages/psycopg2/__init__.py", line 51, in <module>
from psycopg2._psycopg import ( # noqa
ImportError: libpq.so.5: cannot open shared object file: No such file or directory
>>> exit
Use exit() or Ctrl-D (i.e. EOF) to exit
>>>
#报错原因:
ImportError: libpq.so.5: cannot open shared object
#解决办法:
cd /etc/ld.so.conf.d/
echo "/usr/local/postgresql/lib" >>pg.conf
ldconfig
02.配置文件(YAML Configuration)
mkdir /etc/patroni/
chown -R postgres:postgres /etc/patroni
vi /etc/patroni/patroni_postgresql.yml
# DCS (Distributed Configuration Store)
-bash-4.2# ll /etc/patroni/patroni_postgresql.yml
-rw-r--r-- 1 root root 1541 Nov 1 03:39 /etc/patroni/patroni_postgresql.yml
-bash-4.2# cat /etc/patroni/patroni_postgresql.yml
#集群名
scope: pgha
#Etcd中键值位置,数据存储路径
namespace: /pgsql/
#node 节点名称,每个节点不同
name: node1
restapi:
# 保持默认,监听所有的8008端口
listen: 192.168.5.201:8008
# 本地连接通信
connect_address: 192.168.5.201:8008
etcd:
hosts: 192.168.5.201:2379,192.168.5.202:2379,192.168.5.203:2379
#host: ip:port #single etd server
bootstrap:
# this section will be written into Etcd:/<namespace>/<scope>/config after initializing new cluster
#Patroni的引导程序,patroni集群初始化的时候,就会把信息写入到etcd中的/namespace/scope/config下面。
# 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: 20485760
master_start_timeout: 300
# synchronous_mode: false
postgresql:
use_pg_rewind: true
use_slots: false
#postgresql.conf参数文件
parameters:
listen_addresses: "*"
port: 5432
wal_level: logical
hot_standby: "on"
wal_keep_segments: 1000
max_wal_senders: 10
#synchronous_standby_names: '*'
max_connections: 150
max_replication_slots: 10
wal_log_hints: "on"
postgresql:
listen: 0.0.0.0:5432
connect_address: 192.168.5.201:5432 # local ip
data_dir: /data/db02
bin_dir: /usr/local/postgresql/bin
# config_dir:
authentication:
replication:
username: replicator
password: replicator
superuser:
username: postgres
password: postgres
#watchdog:
# 使用linux自带的软件watchdog监控patroni的服务持续性
# mode: automatic # Allowed values: off, automatic, required
# device: /dev/watchdog
# safety_margin: 5
##safety_margin指如果Patroni没有及时更新watchdog,watchdog会在Leader key过期前多久触发重启。在本例的配置下(ttl=30,loop_wait=10,safety_margin=5)下,patroni进程每隔10秒(loop_wait)都会更新Leader key和watchdog。
tags:
#标签的设置,如果集群包含异地的数据中心,可以根据需要配置该节点为不参与选主,不参与负载均衡,也不作为同步备库。
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false
#启动脚本
/usr/lib/systemd/system/patroni.service
[Unit]
Description=Patroni server
Documentation='https://github.com/zalando/patroni,https://www.opsdash.com/blog/postgres-getting-started-patroni.html'
After=syslog.target
After=network.target
[Service]
Type=simple
User=postgres
Group=postgres
# StandardOutput=syslog
WorkingDirectory=/etc/patroni/
ExecStart=/usr/local/bin/patroni /etc/patroni/patroni_postgresql.yml
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
# Do not set any timeout value, so that systemd will not kill postmaster
# during crash recovery.
TimeoutSec=0
[Install]
WantedBy=multi-user.target
#系统systemctl启动:
systemctl start patroni
systemctl enable patroni
systemctl status patroni
#日志输出位置/var/log/messages
#手动启动,使用postgres用户:(后台启动来保持patroni服务)
#patroni etc/patroni.yml
nohup /usr/local/bin/patroni /etc/patroni/patroni_postgresql.yml >/tmp/patroni.log 2>&1 &
#查看集群状态:
-bash-4.2# patronictl -c /etc/patroni/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+ Cluster: pgha (7025507499523621070) -------+----+-----------+
| node1 | 192.168.5.201 | Leader | running | 1 | |
| node2 | 192.168.5.202 | Replica | running | 1 | 0 |
| node3 | 192.168.5.203 | Replica | running | 1 | 0 |
+--------+---------------+---------+---------+----+-----------+
#查看配置文件:
-bash-4.2# patronictl -c /etc/patroni/patroni_postgresql.yml show-config
loop_wait: 10
master_start_timeout: 300
maximum_lag_on_failover: 20485760
postgresql:
parameters:
hot_standby: 'on'
listen_addresses: '*'
max_connections: 150
max_replication_slots: 10
max_wal_senders: 10
port: 5432
wal_keep_segments: 1000
wal_level: logical
wal_log_hints: 'on'
use_pg_rewind: true
use_slots: false
retry_timeout: 10
ttl: 30
03.switchover
patronictl -c /etc/patroni/patroni_postgresql.yml switchover
-bash-4.2# patronictl -c /etc/patroni/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+ Cluster: pgha (7025507499523621070) -------+----+-----------+
| node1 | 192.168.5.201 | Leader | running | 1 | |
| node2 | 192.168.5.202 | Replica | running | 1 | 0 |
| node3 | 192.168.5.203 | Replica | running | 1 | 0 |
+--------+---------------+---------+---------+----+-----------+
-bash-4.2# patronictl -c /etc/patroni/patroni_postgresql.yml switchover
Master [node1]:
Candidate ['node2', 'node3'] []:
When should the switchover take place (e.g. 2021-11-01T05:37 ) [now]:
Current cluster topology
+--------+---------------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+ Cluster: pgha (7025507499523621070) -------+----+-----------+
| node1 | 192.168.5.201 | Leader | running | 1 | |
| node2 | 192.168.5.202 | Replica | running | 1 | 0 |
| node3 | 192.168.5.203 | Replica | running | 1 | 0 |
+--------+---------------+---------+---------+----+-----------+
Are you sure you want to switchover cluster pgha, demoting current master node1? [y/N]: y
2021-11-01 04:37:12.321 EDT [4893] LOG: received fast shutdown request
2021-11-01 04:37:12.322 EDT [4893] LOG: aborting any active transactions
2021-11-01 04:37:12.322 EDT [5939] FATAL: terminating connection due to administrator command
2021-11-01 04:37:12.322 EDT [5652] FATAL: terminating connection due to administrator command
2021-11-01 04:37:12.324 EDT [4893] LOG: background worker "logical replication launcher" (PID 4901) exited with exit code 1
2021-11-01 04:37:12.324 EDT [4895] LOG: shutting down
2021-11-01 04:37:12.411 EDT [6540] FATAL: the database system is shutting down
2021-11-01 04:37:12.505 EDT [4893] LOG: database system is shut down
2021-11-01 04:37:13.20909 Successfully switched over to "node2"
+--------+---------------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+ Cluster: pgha (7025507499523621070) -------+----+-----------+
| node1 | 192.168.5.201 | Replica | stopped | | unknown |
| node2 | 192.168.5.202 | Leader | running | 1 | |
| node3 | 192.168.5.203 | Replica | running | 1 | 0 |
+--------+---------------+---------+---------+----+-----------+
-bash-4.2# patronictl -c /etc/patroni/patroni_postgresql.yml list
+--------+---------------+---------+---------+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+ Cluster: pgha (7025507499523621070) -------+----+-----------+
| node1 | 192.168.5.201 | Replica | running | 2 | 0 |
| node2 | 192.168.5.202 | Leader | running | 2 | |
| node3 | 192.168.5.203 | Replica | running | 2 | 0 |
+--------+---------------+---------+---------+----+-----------+
04,主节点宕机,备节点自动接管
Nov 1 21:36:40 db04 patroni: 2021-11-01 21:36:40,593 INFO: Lock owner: node2; I am node3
Nov 1 21:36:40 db04 patroni: 2021-11-01 21:36:40,609 INFO: Local timeline=3 lsn=0/D000028
Nov 1 21:36:40 db04 patroni: 2021-11-01 21:36:40,613 INFO: master_timeline=4
Nov 1 21:36:40 db04 patroni: 2021-11-01 21:36:40,614 INFO: master: history=1#0110/B0000A0#011no recovery target specified
Nov 1 21:36:40 db04 patroni: 2#0110/C0000A0#011no recovery target specified
Nov 1 21:36:40 db04 patroni: 3#0110/C0002A0#011no recovery target specified
Nov 1 21:36:40 db04 patroni: 2021-11-01 21:36:40,622 INFO: running pg_rewind from node2
Nov 1 21:36:40 db04 patroni: 2021-11-01 21:36:40,626 INFO: running pg_rewind from dbname=postgres user=postgres host=192.168.5.202 port=5432
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,431 INFO: pg_rewind exit code=0
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,432 INFO: stdout=
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,432 INFO: stderr=pg_rewind: servers diverged at WAL location 0/C0002A0 on timeline 3
Nov 1 21:36:41 db04 patroni: pg_rewind: rewinding from last common checkpoint at 0/C0001F0 on timeline 3
Nov 1 21:36:41 db04 patroni: pg_rewind: Done!
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,436 WARNING: Postgresql is not running.
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,436 INFO: Lock owner: node2; I am node3
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,439 INFO: pg_controldata:
Nov 1 21:36:41 db04 patroni: pg_control version number: 1300
Nov 1 21:36:41 db04 patroni: Catalog version number: 202007201
Nov 1 21:36:41 db04 patroni: Database system identifier: 7025507499523621070
Nov 1 21:36:41 db04 patroni: Database cluster state: in archive recovery
Nov 1 21:36:41 db04 patroni: pg_control last modified: Mon Nov 1 21:30:27 2021
Nov 1 21:36:41 db04 patroni: Latest checkpoint location: 0/C000428
Nov 1 21:36:41 db04 patroni: Latest checkpoint's REDO location: 0/C0003F0
Nov 1 21:36:41 db04 patroni: Latest checkpoint's REDO WAL file: 00000004000000000000000C
Nov 1 21:36:41 db04 patroni: Latest checkpoint's TimeLineID: 4
Nov 1 21:36:41 db04 patroni: Latest checkpoint's PrevTimeLineID: 4
Nov 1 21:36:41 db04 patroni: Latest checkpoint's full_page_writes: on
Nov 1 21:36:41 db04 patroni: Latest checkpoint's NextXID: 0:486
Nov 1 21:36:41 db04 patroni: Latest checkpoint's NextOID: 16385
Nov 1 21:36:41 db04 patroni: Latest checkpoint's NextMultiXactId: 1
Nov 1 21:36:41 db04 patroni: Latest checkpoint's NextMultiOffset: 0
Nov 1 21:36:41 db04 patroni: Latest checkpoint's oldestXID: 478
Nov 1 21:36:41 db04 patroni: Latest checkpoint's oldestXID's DB: 1
Nov 1 21:36:41 db04 patroni: Latest checkpoint's oldestActiveXID: 486
Nov 1 21:36:41 db04 patroni: Latest checkpoint's oldestMultiXid: 1
Nov 1 21:36:41 db04 patroni: Latest checkpoint's oldestMulti's DB: 1
Nov 1 21:36:41 db04 patroni: Latest checkpoint's oldestCommitTsXid: 0
Nov 1 21:36:41 db04 patroni: Latest checkpoint's newestCommitTsXid: 0
Nov 1 21:36:41 db04 patroni: Time of latest checkpoint: Mon Nov 1 21:30:27 2021
Nov 1 21:36:41 db04 patroni: Fake LSN counter for unlogged rels: 0/3E8
Nov 1 21:36:41 db04 patroni: Minimum recovery ending location: 0/C01B828
Nov 1 21:36:41 db04 patroni: Min recovery ending loc's timeline: 4
Nov 1 21:36:41 db04 patroni: Backup start location: 0/0
Nov 1 21:36:41 db04 patroni: Backup end location: 0/0
Nov 1 21:36:41 db04 patroni: End-of-backup record required: no
Nov 1 21:36:41 db04 patroni: wal_level setting: logical
Nov 1 21:36:41 db04 patroni: wal_log_hints setting: on
Nov 1 21:36:41 db04 patroni: max_connections setting: 150
Nov 1 21:36:41 db04 patroni: max_worker_processes setting: 8
Nov 1 21:36:41 db04 patroni: max_wal_senders setting: 10
Nov 1 21:36:41 db04 patroni: max_prepared_xacts setting: 0
Nov 1 21:36:41 db04 patroni: max_locks_per_xact setting: 64
Nov 1 21:36:41 db04 patroni: track_commit_timestamp setting: off
Nov 1 21:36:41 db04 patroni: Maximum data alignment: 8
Nov 1 21:36:41 db04 patroni: Database block size: 8192
Nov 1 21:36:41 db04 patroni: Blocks per segment of large relation: 131072
Nov 1 21:36:41 db04 patroni: WAL block size: 8192
Nov 1 21:36:41 db04 patroni: Bytes per WAL segment: 16777216
Nov 1 21:36:41 db04 patroni: Maximum length of identifiers: 64
Nov 1 21:36:41 db04 patroni: Maximum columns in an index: 32
Nov 1 21:36:41 db04 patroni: Maximum size of a TOAST chunk: 1996
Nov 1 21:36:41 db04 patroni: Size of a large-object chunk: 2048
Nov 1 21:36:41 db04 patroni: Date/time type storage: 64-bit integers
Nov 1 21:36:41 db04 patroni: Float8 argument passing: by value
Nov 1 21:36:41 db04 patroni: Data page checksum version: 0
Nov 1 21:36:41 db04 patroni: Mock authentication nonce: dda11a8b9383f150bf98b689e3f970e5b19375bd1ed7a9cba1ceff1e786b9877
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,451 INFO: Lock owner: node2; I am node3
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,453 INFO: starting as a secondary
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41,668 INFO: postmaster pid=1656
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.677 EDT [1656] LOG: starting PostgreSQL 13.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.677 EDT [1656] LOG: listening on IPv4 address "0.0.0.0", port 5432
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.678 EDT [1656] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.680 EDT [1658] LOG: database system was interrupted while in recovery at log time 2021-11-01 21:30:27 EDT
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.680 EDT [1658] HINT: If this has occurred more than once some data might be corrupted and you might need to choose an earlier recovery target.
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.689 EDT [1658] LOG: entering standby mode
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.690 EDT [1658] LOG: redo starts at 0/C0001B8
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.692 EDT [1658] LOG: invalid record length at 0/C01B7F0: wanted 24, got 0
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.743 EDT [1662] FATAL: the database system is starting up
Nov 1 21:36:41 db04 patroni: localhost:5432 - rejecting connections
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.747 EDT [1664] FATAL: the database system is starting up
Nov 1 21:36:41 db04 patroni: localhost:5432 - rejecting connections
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.749 EDT [1661] LOG: started streaming WAL from primary at 0/C000000 on timeline 4
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.750 EDT [1658] LOG: consistent recovery state reached at 0/C01B828
Nov 1 21:36:41 db04 patroni: 2021-11-01 21:36:41.750 EDT [1656] LOG: database system is ready to accept read only connections
Nov 1 21:36:42 db04 patroni: localhost:5432 - accepting connections
Nov 1 21:36:42 db04 patroni: 2021-11-01 21:36:42,787 INFO: Lock owner: node2; I am node3
Nov 1 21:36:42 db04 patroni: 2021-11-01 21:36:42,787 INFO: establishing a new patroni connection to the postgres cluster
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)