基于IvorySQL+Patroni+vip-manager构建高可用集群
Patroni作为一款简单易用的高可用性工具,能够轻松地管理PostgreSQL集群,从而确保数据安全性和连续性。最新发布的Patroni 3.2版本提供了与PostgreSQL 16的兼容性,与IvorySQL 3.1版本可以完美适配,共同构建出强大而稳定的数据库高可用集群。同时,结合vip-manager的灵活虚拟IP的漂移功能,能够在故障转移时实现客户端的无感知访问,确保业务的平滑运行。
下面,我们就详细介绍一下IvorySQL 3.1+Patroni 3.2+vip-manager 2.0的高可用集群部署流程。我们以 x86_64 架构的 CentOS7 操作系统为例,并准备三台 CentOS7 服务器作为环境搭建的基础。每台服务器上都需要执行数据库及高可用组件的安装与配置工作,以确保整个集群的顺利运行和高度可用性。
01
安装配置IvorySQL数据库
1.1 下载IvorySQL安装包
wget https://github.com/IvorySQL/IvorySQL/releases/download/IvorySQL_3.1/ivorysql3-libs-3.1-1.rhel7.x86_64.rpm
wget
https://github.com/IvorySQL/IvorySQL/releases/download/IvorySQL_3.1/ivorysql3-3.1-1.rhel7.x86_64.rpm
wget https://github.com/IvorySQL/IvorySQL/releases/download/IvorySQL_3.1/ivorysql3-contrib-3.1-1.rhel7.x86_64.rpm
wget
https://github.com/IvorySQL/IvorySQL/releases/download/IvorySQL_3.1/ivorysql3-server-3.1-1.rhel7.x86_64.rpm
1.2 安装rpm包
yum install -y libicu libxslt python3
rpm -ivh ivorysql3-libs-3.1-1.rhel7.x86_64.rpm
rpm -ivh ivorysql3-3.1-1.rhel7.x86_64.rpm
rpm -ivh ivorysql3-contrib-3.1-1.rhel7.x86_64.rpm --nodeps
rpm -ivh ivorysql3-server-3.1-1.rhel7.x86_64.rpm
1.3 创建用户及环境变量
(1)创建操作系统用户
/usr/sbin/groupadd ivorysql
/usr/sbin/useradd -m -g ivorysql ivorysql -c "IvorySQL3.1 Server"
/usr/bin/passwd ivorysql
(2)修改文件夹权限
chown -R ivorysql.ivorysql /usr/local/ivorysql/
(3)配置环境变量
export LD_LIBRARY_PATH=/usr/local/ivorysql/ivorysql-3/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/ivorysql/ivorysql-3/bin:$PATH
export PGDATA=/usr/local/ivorysql/ivorysql-3/data
source .bash_profile
1.4 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --zone=public --add-port=1521/tcp --permanent
firewall-cmd --zone=public --add-port=5432/tcp --permanent
firewall-cmd --zone=public --add-port=8008/tcp --permanent
firewall-cmd --zone=public --add-port=2379/tcp --permanent
firewall-cmd --zone=public --add-port=2380/tcp --permanent
firewall-cmd --reload
02
安装配置启动etcd
yum install etcd
[root@sophia-pghost5 ~]# etcd --version
etcd Version: 3.3.11
Git SHA: 2cf9e51
Go Version: go1.10.3
Go OS/Arch: linux/amd64
name: 'etcd_01'
data-dir: /usr/local/hghac/etcd/etcd01
listen-peer-urls: http://192.168.31.105:2380
listen-client-urls: http://192.168.31.105:2379,http://127.0.0.1:2379
initial-advertise-peer-urls: http://192.168.31.105:2380
advertise-client-urls: http://192.168.31.105:2379
initial-cluster: etcd_01=http://192.168.31.105:2380,etcd_02=http://192.168.31.106:2380,etcd_03=http://192.168.31.107:2380
initial-cluster-token: 'etcd-cluster'
initial-cluster-state: 'new'
etcd --config-file etcd.yml
[ivorysql@sophia-pghost5 etcd]$ etcdctl member list
57eb343a6baf05a: name=etcd_03 peerURLs=http://192.168.31.107:2380 clientURLs=http://192.168.31.107:2379 isLeader=false
11020de439841796: name=etcd_02 peerURLs=http://192.168.31.106:2380 clientURLs=http://192.168.31.106:2379 isLeader=false
a26f27e0d5c5a30d: name=etcd_01 peerURLs=http://192.168.31.105:2380 clientURLs=http://192.168.31.105:2379 isLeader=true
03
安装配置启动Patroni
3.1安装依赖库及patroni
yum install gcc python3-devel
pip install psycopg2
pip install patroni
[root@sophia-pghost5 ~]# patroni --version
patroni 3.2.2
3.2配置patroni.yml
scope: ivorysql-cluster
namespace: /service/
name: ivorysql_01
restapi:
listen: 192.168.31.105:8008
connect_address: 192.168.31.105:8008
etcd:
hosts: 192.168.31.105:2379,192.168.31.106:2379,192.168.31.107:2379
log:
level: INFO
traceback_level: INFO
dir: /usr/local/ivorysql/patroni
bootstrap:
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
parameters:
wal_level: replica
hot_standby: "on"
wal_keep_size: 100
max_wal_senders: 10
max_replication_slots: 10
wal_log_hints: "on"
archive_mode: "off"
archive_timeout: 1800s
logging_collector: on
postgresql:
database: postgres
listen: 0.0.0.0:5432
connect_address: 192.168.31.105:5432
bin_dir: /usr/local/ivorysql/ivorysql-3/bin
data_dir: /usr/local/ivorysql/ivorysql-3/data
config_dir: /usr/local/ivorysql/ivorysql-3/data
authentication:
replication:
username: ivorysql
password: ivory123
superuser:
username: ivorysql
password: ivory123
rewind:
username: ivorysql
password: ivory123
pg_hba:
- local all all trust
- host all all 0.0.0.0/0 trust
- host all all ::1/128 trust
- local replication all trust
- host replication all 0.0.0.0/0 trust
- host replication all ::1/128 trust
tags:
noloadbalance: false
clonefrom: false
nosync: false
3.3启动patroni
patroni patroni.yml
[ivorysql@sophia-pghost5 patroni]$ patronictl -c patroni.yml list
+ Cluster: ivorysql-cluster (7346459572541124634) +----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-------------+----------------+--------+---------+----+-----------+
| ivorysql_01 | 192.168.31.105 | Leader | running | 1 | |
+-------------+----------------+--------+---------+----+-----------+
[ivorysql@sophia-pghost5 patroni]$ patronictl -c patroni.yml list
+ Cluster: ivorysql-cluster (7346459572541124634) ---+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-------------+----------------+---------+-----------+----+-----------+
| ivorysql_01 | 192.168.31.105 | Leader | running | 1 | |
| ivorysql_02 | 192.168.31.106 | Replica | streaming | 1 | 0 |
| ivorysql_03 | 192.168.31.107 | Replica | streaming | 1 | 0 |
+-------------+----------------+---------+-----------+----+-----------+
04
安装配置启动vip-manager
4.1下载安装包
wget https://github.com/cybertec-postgresql/vip-manager/releases/download/v2.3.0/vip-manager_2.3.0_Linux_x86_64.tar.gz
4.2解压安装包
[root@sophia-pghost5 local]# tar -zxvf vip-manager_2.3.0_Linux_x86_64.tar.gz
vip-manager_2.3.0_Linux_x86_64/LICENSE
vip-manager_2.3.0_Linux_x86_64/README.md
vip-manager_2.3.0_Linux_x86_64/vip-manager.yml
vip-manager_2.3.0_Linux_x86_64/vip-manager
4.3配置vip-manager.yml
interval: 1000
trigger-key: "/service/ivorysql-cluster/leader"
trigger-value: "ivorysql_01"
ip: 192.168.0.108
netmask: 24
interface: enp0s3
hosting-type: basic
dcs-type: etcd
dcs-endpoints:
http://127.0.0.1:2379,http://192.168.31.101:2379,http://192.168.31.102:2379,http://192.168.31.103:2379
retry-num: 2
retry-after: 250
verbose: false
4.4启动vip-manager
./vip-manager --config vip-manager.yml
[root@sophia-pghost5 vip-manager_2.3.0_Linux_x86_64]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 08:00:27:75:18:bd brd ff:ff:ff:ff:ff:ff
inet 192.168.31.105/24 brd 192.168.31.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet 192.168.31.108/24 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fe75:18bd/64 scope link
valid_lft forever preferred_lft forever
[root@sophia-pghost6 ~]# psql -h 192.168.31.108 -U ivorysql -d postgres
psql (16.1)
Type "help" for help.
postgres=# select * from pg_is_in_recovery();
pg_is_in_recovery
-------------------
f
(1 row)
05
高可用故障场景模拟
[ivorysql@sophia-pghost7 patroni]$ patronictl -c patroni.yml list
+ Cluster: ivorysql-cluster (7346459572541124634) ---+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-------------+----------------+---------+-----------+----+-----------+
| ivorysql_02 | 192.168.31.106 | Leader | running | 2 | |
| ivorysql_03 | 192.168.31.107 | Replica | streaming | 2 | 0 |
+-------------+----------------+---------+-----------+----+-----------+
[ivorysql@sophia-pghost7 patroni]$ patronictl -c patroni.yml list
+ Cluster: ivorysql-cluster (7346459572541124634) ---+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-------------+----------------+---------+-----------+----+-----------+
| ivorysql_02 | 192.168.31.106 | Leader | running | 2 | |
| ivor[ivorysql@sophia-pghost7 patroni]$ patronictl -c patroni.yml list
+ Cluster: ivorysql-cluster (7346459572541124634) ---+----+-----------+
| Member | Host | Role | State | TL | Lag in MB |
+-------------+----------------+---------+-----------+----+-----------+
| ivorysql_01 | 192.168.31.105 | Replica | streaming | 2 | 0 |
| ivorysql_02 | 192.168.31.106 | Leader | running | 2 | |
| ivorysql_03 | 192.168.31.107 | Replica | streaming | 2 | 0 |
+-------------+----------------+---------+-----------+----+-----------+
关注公众号,了解更多社区动态
- 我们是谁 -
lvorySQL是由浪潮软件集团瀚高股份主导研发的,一款深度兼容Oracle的PostgreSQL开源数据库系统。IvorySQL从底层代码层面深入把握开源技术的发展趋势,基于PostgreSQL 16.1的最新内核进行构建,同时提供了更加全面灵活的Oracle兼容功能,具备高度的SQL和PL/SQL兼容性,能够满足企业对于数据库系统多样化和高兼容性的需求。
IvorySQL内核的Serverless数据库——HGNeon适配国内云对象存储
7个工具!让Oracle 迁移至 PostgreSQL成为可能!
4步上手Meson:让PostgreSQL 16 构建更现代化!
IvorySQL