EcoX-PostgreSQL高可用集群最佳实践 原作者:何敏 创作时间:2019-06-20 20:36:11+08 |
wangliyun 发布于2019-06-21 08:36:11
![]() ![]() ![]() ![]() ![]() |
作者:何敏
作者简介
何敏,目前服务于成都文武信息技术有限公司,有丰富的开发和运维经验。参与开发公司的PostgreSQL高可用系统EcoX、RDS系统,对PostgreSQL高可用系统有非常深入的了解。服务过银行、保险、电力等行业客户。
EcoX简介
EcoX数据库高可用集群管理系统软件,该软件包含数据库高可用、读写分离、负载均衡、连接池等功能,能够保证业务7*24小时连续运行,保护业务最大可用性。其特点如下:
基于可靠的ZAB协议传递消息和控制指令,确保系统准确无误运行和执行指定的命令。
采用集群仲裁机制,防止单点故障和误操作。
单套ECOX集群能够管理成百上千个PostgreSQL集群,帮助企业和运维人员提高效率,简化运维难度。
主要模块
EcoX高可用方案由三个部分组成:分布式应用一致性服务软件仲裁集群、EcoX高可用集群管理软件、PostgreSQL数据库/集群:
仲裁集群:主要作用是观察者,判断PG集群是否有故障;采用集群是为了避免观察者本身的单点故障。
PostgreSQL数据库集群:PG本身的概念,不多做介绍。
ECOX集群:主要保护PG集群的高可用和节点角色切换,异步模式尽可能保证无数据丢失(同步复制不存在此情况)。
安装过程
1.1安装仲裁集群(略)
仲裁集群安装过程比较简单,需要注意myid的配置。如果启动失败,查看是否不是防火墙的问题。
仲裁集群最好是独立于数据库集群安装,推荐5节点集群,最低要求3个节点。
1.2安装PostgreSQL(略)
节点仅安装软件,不需要初始化数据库。
1.3安装EcoX高可用管理系统
在所有的数据库节点都需要安装EcoX高可用关系软件,本示例中采用三台服务器作为PostgreSQL数据库节点,分别是:10.9.5.21,10.9.5.22,10.9.5.24,因此在这几个服务器上需要安装EcoX。
1.3.1通过rpm包安装
从成都文武信息技术有限公司官网下载EcoX安装包:http://w3.ww-it.cn/Fileud/lists/cate_id/30
将安装包拷贝到每台数据库服务器,使用rpm执行安装,默认安装到/usr/EcoX:
rpm -ivh EcoX-5.12.2-1.el6.x86_64.rpm
1.3.2环境设置
设置环境变量:
export PATH=/usr/EcoX/bin:$PATH
export LD_LIBRARY_PATH=/usr/EcoX/lib:$LD_LIBRARY_PATH
添加postgres到sudoer中:
[root@cent634 ~]# sudo echo " postgres ALL=(ALL) NOPASSWD: ALL " >> /etc/sudoers
1.3.3修改配置文件
修改每台数据库服务器上的EcoX配置文件:/usr/EcoX/etc/EcoX.conf,配置文件的内容如下:
[root@pg22 etc]# cat ecox.conf
cluster=cluster_name
#init_role=comaster
pgcomaster_num=2
#auto_promote_slave=false
#auto_promote_slave=true
#async_replication=yes
zkhost=10.9.5.20:4119,10.9.5.21:4119,10.9.5.22:4119,10.9.5.24:4119,10.9.5.35:4119,10.9.5.36:4119
zktimeout=20000
pghost=10.9.5.20
pgbin=/usr/pgsql-10/bin
pgport=5433
pgdata=/var/lib/pgsql/10/data
pgclient_net=10.9.0.0/16
pgcluster_net=10.9.0.0/16
#do_not_use_vip=true
eth_port=eth0
virtual_ip=10.9.5.200
netmask=255.255.255.0
do_not_use_comaster_vip=true
comaster_eth_port=eth0
comaster_virtual_ip=10.9.5.201
comaster_netmask=255.255.255.0
基本操作
1.4 EcoX基本操作
EcoX is PostgreSQL cluster management software
Usage:
EcoX [OPTION]...
Options:
--help/-h show help message
--version show current EcoX version
--start start current EcoX postgreSQL node
--stop stop current node
--show node show current node's status
--show cluster show current node's status
--clean/-c clear all the zookeeper data of the cluster which this node in
--free free the current lock on cluster
--rewind execute pg_rewind, resync old master from the current master
--restart database restart postgreSQL without change node role
--status check the master node is ready, if so the cluster can be use
Copyright reserved, Chengdu WenWu Information Technology Co.,Ltd.
support: db_support@ww-it.cn | web: w3.ww-it.cn
1.5初始化集群
直接使用ecox start即可初始化集群,在ecox启动过程中,如果发现集群未初始化,就会先进行初始化,并对部分数据库参数进行优化,最后再启动数据库、启动EcoX。
(1)在主节点10.9.5.21上执行ecox start,会先initdb:
-bash-4.1$ ecox start
Initial log system success, priority is 600
PostgreSQL 10.1 is compatible
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default text search configuration will be set to "english".
Data page checksums are enabled.
fixing permissions on existing directory /var/lib/pgsql/10/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Waiting for database starting ....
done
database start success
2)在从节点10.9.5.22、10.9.5.24上分别执行ecox start,第一次会等待主节点上线,并从主节点上拉取基础备份过来,自动配置流复制参数,再启动PostgreSQL数据库:
-bash-4.1$ ecox start
Initial log system success, priority is 600
PostgreSQL 10.1 is compatible
-bash-4.1$ Begin wait for master online ...
Begin wait for node0000000000 online ...
Waiting for database starting ....
done
database start success
1.6查看节点状态
使用ecox show node查看本节点EcoX状态。
1)查看主节点10.9.5.21 EcoX状态:
-bash-4.1$ ecox show node
EcoX status: running
Node name : node0000000000
Node role : master
2)查看从节点10.9.5.22 EcoX状态:
-bash-4.1$ ecox show node
EcoX status: running
Node name : node0000000001
Node role : comaster
3)查看节点10.9.5.24 EcoX状态:
-bash-4.1$ ecox show node
EcoX status: running
Node name : node0000000002
Node role : slave
1.7查看集群状态
在任意节点上使用ecox show cluster查看集群状态:
-bash-4.1$ ecox show cluster
init cluster instance success
PostgreSQL 10.1 is compatible
node name : ip address | port | online
---------------------------------------------------------
node0000000000: 10.9.5.21 | 5410 | TRUE
node0000000001: 10.9.5.22 | 5410 | TRUE
node0000000002: 10.9.5.24 | 5410 | TRUE
master : node0000000000
comaster 1: node0000000001
last master: node0000000000
sync replication :node0000000001
故障切换
1.8模拟主节点掉线
1.8.1主节点掉线
当前集群中node0000000000:10.9.5.21是主节点,手动停止EcoX,模拟数据库掉线:
1)切换前集群状态,node0000000000为主节点:
-bash-4.1$ ecox show cluster
init cluster instance success
PostgreSQL 10.1 is compatible
node name : ip address | port | online
---------------------------------------------------------
node0000000000: 10.9.5.21 | 5410 | TRUE
node0000000001: 10.9.5.22 | 5410 | TRUE
node0000000002: 10.9.5.24 | 5410 | TRUE
master : node0000000000
comaster 1: node0000000001
last master: node0000000000
sync replication :node0000000001
-bash-4.1$
-bash-4.1$
2)找到EcoX进程PID并杀掉进程:
-bash-4.1$ ps -ef|grep EcoX
postgres 8188 1 0 14:21 pts/1 00:00:00 /usr/EcoX/bin/EcoX --start
postgres 8794 3411 0 14:24 pts/1 00:00:00 grep EcoX
-bash-4.1$ kill 8188
waiting for server to shut down.... done
server stopped
stop database success
system shutdown success
pg_ctl: PID file "/var/lib/pgsql/10/data/postmaster.pid" does not exist
Is server running?
-bash-4.1$ ps -ef|grep EcoX
postgres 8894 3411 0 14:29 pts/1 00:00:00 grep EcoX
-bash-4.1$ ps -ef|grep postgres
postgres 6963 16794 0 11:49 pts/0 00:00:00 ps -ef
postgres 6964 16794 0 11:49 pts/0 00:00:00 grep postgres
root 16793 16575 0 Apr28 pts/0 00:00:00 su - postgres
postgres 16794 16793 0 Apr28 pts/0 00:00:00 -bash
可以看到通过杀掉EcoX进程,不但杀掉了EcoX,同时,EcoX也会停止该节点的PostgreSQL数据库。
1.8.2集群自动切换
当10.9.5.21的EcoX掉线后,10.9.5.22、10.9.5.24节点上的EcoX会收到该掉线消息,并选举出新的主节点,从下面cluster信息可以看到node0000000001,即10.9.5.22选举为新的主节点:
-bash-4.1$ ecox show cluster
init cluster instance success
PostgreSQL 10.1 is compatible
node name : ip address | port | online
---------------------------------------------------------
node0000000000: 10.9.5.21 | 5410 | FALSE
node0000000001: 10.9.5.22 | 5410 | TRUE
node0000000002: 10.9.5.24 | 5410 | TRUE
master : node0000000001
comaster 1: node0000000002
last master: node0000000001
sync replication :node0000000002
1.8.3掉线主节点重新加集群
掉线的主节点,通过ecox start可以重新加入集群。但集群中已经有了主节点和从节点(实验环境配置文件中从节点个数配置为1),因此重新加入集群时,只能充当slave节点。且EcoX会自动探测该掉线主节点是否发生日志分支,并进行修复:
-bash-4.1$ ecox start
Initial log system success, priority is 600
PostgreSQL 10.1 is compatible
-bash-4.1$ Begin wait for node0000000001 online ...
begin to sync date from 10.9.5.22...
execute cmd:/usr/pgsql-10/bin/pg_rewind --target-pgdata='/var/lib/pgsql/10/data' --source-server='host=10.9.5.22 port=5410
connected to server
control file size is: 8192
servers diverged at WAL location 0/3000258 on timeline 1
no rewind required
Waiting for database starting ....
done
database start success
-bash-4.1$ ecox show node
EcoX status: running
Node name : node0000000000
Node role : slave
-bash-4.1$
-bash-4.1$ ecox show cluster
init cluster instance success
PostgreSQL 10.1 is compatible
node name : ip address | port | online
---------------------------------------------------------
node0000000000: 10.9.5.21 | 5410 | TRUE
node0000000001: 10.9.5.22 | 5410 | TRUE
node0000000002: 10.9.5.24 | 5410 | TRUE
master : node0000000001
comaster 1: node0000000002
last master: node0000000001
sync replication :node0000000002
1.9模拟从节点掉线
1.9.1杀掉从节点数据库主进程
手动停止从节点的PostgreSQL数据库,看slave节点是否被提升为新的从节点。
从上面可以看到10.9.5.24是从节点,我们到从节点上杀掉数据库进程,可以看到数据库停止了,EcoX也停止了:
-bash-4.1$ ecox show node
EcoX status: running
Node name : node0000000002
Node role : comaster
-bash-4.1$ ps -ef|grep postgres
postgres 16080 1 0 14:21 pts/0 00:00:04 /usr/EcoX/bin/EcoX --start
root 16793 16575 0 Apr28 pts/0 00:00:00 su - postgres
postgres 16794 16793 0 Apr28 pts/0 00:00:00 -bash
postgres 16920 16080 0 14:25 pts/0 00:00:01 /usr/pgsql-10/bin/postgres -D /var/lib/pgsql/10/data
postgres 16921 16920 0 14:25 ? 00:00:00 postgres: logger process
postgres 16922 16920 0 14:25 ? 00:00:00 postgres: startup process recovering 000000020000000000000003
postgres 16923 16920 0 14:25 ? 00:00:00 postgres: checkpointer process
postgres 16924 16920 0 14:25 ? 00:00:00 postgres: writer process
postgres 16925 16920 0 14:25 ? 00:00:00 postgres: stats collector process
postgres 16926 16920 0 14:25 ? 00:00:01 postgres: wal receiver process
postgres 19230 16920 0 14:34 ? 00:00:00 postgres: wal sender process postgres 10.9.5.21(22818) streaming 0/3000368
postgres 31556 16794 0 15:25 pts/0 00:00:00 ps -ef
postgres 31557 16794 0 15:25 pts/0 00:00:00 grep postgres
-bash-4.1$ kill 16920
-bash-4.1$ ps -ef|grep postgres
root 16793 16575 0 Apr28 pts/0 00:00:00 su - postgres
postgres 16794 16793 0 Apr28 pts/0 00:00:00 -bash
postgres 31593 16794 0 15:25 pts/0 00:00:00 ps -ef
postgres 31594 16794 0 15:25 pts/0 00:00:00 grep postgres
-bash-4.1$ ecox show node
EcoX status: stop
Node name : node0000000002
Node role : comaster
1.9.2集群自动切换
可以看到刚刚上线的10.9.5.21节点从slave角色变为了comaster,且和主节点保持同步复制模式:
-bash-4.1$ ecox show cluster
init cluster instance success
PostgreSQL 10.1 is compatible
node name : ip address | port | online
---------------------------------------------------------
node0000000000: 10.9.5.21 | 5410 | TRUE
node0000000001: 10.9.5.22 | 5410 | TRUE
node0000000002: 10.9.5.24 | 5410 | FALSE
master : node0000000001
comaster 1: node0000000000
last master: node0000000001
sync replication :node0000000000
1.9.3掉线节点重新加入集群
在刚刚掉线的节点10.9.5.24上执行ecox start,可以看到该节点加入到集群,角色变为了slave:
-bash-4.1$ ecox start
Initial log system success, priority is 600
PostgreSQL 10.1 is compatible
-bash-4.1$ Waiting for database starting ....
done
database start success
-bash-4.1$ ecox show cluster
init cluster instance success
PostgreSQL 10.1 is compatible
node name : ip address | port | online
---------------------------------------------------------
node0000000000: 10.9.5.21 | 5410 | TRUE
node0000000001: 10.9.5.22 | 5410 | TRUE
node0000000002: 10.9.5.24 | 5410 | TRUE
master : node0000000001
comaster 1: node0000000000
last master: node0000000001
sync replication :node0000000000
-bash-4.1$ ecox show node
EcoX status: running
Node name : node0000000002
Node role : slave
使用总结
EcoX高可用集群软件安装部署和使用都很方便,且不用用户手动配置流复制过程,EcoX自己会拉取备份、修改配置文件、启动集群。
EcoX能够立即发现PostgreSQL集群中故障节点,并迅速选举出新的节点顶替故障节点的角色。主节点掉线,从节点中sync节点会被竞选为新的主节点,主节点的VIP等资源会迁移到新的主节点上。从节点掉线,会从slave节点中选取出WAL LSN最大的节点为新的从节点,从节点的VIP会迁移到新的从节点上。
EcoX还有很多高级特色功能,例如:可以设置节点角色、节点VIP是否启用、可设置从节点个数、重启数据库、自动优化数据库配置文件等等,这里就不一一体验了,感兴趣的朋友可以到成都文武信息技术有限公司的官网下载EcoX全套资料进行了解和使用,
链接:http://w3.ww-it.cn/Fileud/lists/cate_id/30
请在登录后发表评论,否则无法保存。
1# __
xcvxcvsdf 回答于 2024-11-22 09:52:58+08
http://ouyu.hftcbmw.cn/zhangjiakou/
http://jinqiang.ahtcbmw.cn/jinshansh/
https://shfxcf.tiancebbs.cn/
http://huaguang.jxtcbmw.cn/ahhz/
http://fs.shtcxxw.cn/jixi/
https://wychengdong.tiancebbs.cn/
https://danzaozhen.tiancebbs.cn/
http://ruanwen.xztcxxw.cn/shuozhou/
http://huaguang.jxtcbmw.cn/fzyzp/
http://huaguang.jxtcbmw.cn/tai-zhou/
http://yz.cqtcxxw.cn/xingtai/
http://yz.cqtcxxw.cn/zjcz/
http://tuiguang.hntcxxw.cn/jszp/
http://fs.shtcxxw.cn/guiyang/
https://ssqdatangzhen.tiancebbs.cn/
http://wutai.cqtcxxw.cn/fuzhou/
https://pljingning.tiancebbs.cn/
2# __
xiaowu 回答于 2024-04-23 14:54:39+08
适合女性阳光的微信名:https://www.nanss.com/mingcheng/5600.html qq标签大全:https://www.nanss.com/shenghuo/5496.html 面面相觑造句:https://www.nanss.com/xuexi/5763.html 放生祈福的说说:https://www.nanss.com/wenan/5810.html 忘记的网名:https://www.nanss.com/mingcheng/5522.html 晒娃的唯美短句十字以内:https://www.nanss.com/wenan/5780.html 送给死党的沙雕生日祝福:https://www.nanss.com/yulu/5860.html 心灵的深处写一段话:https://www.nanss.com/xuexi/5024.html 游戏宠物名字:https://www.nanss.com/mingcheng/4896.html 又菜又爱玩类似名字:https://www.nanss.com/mingcheng/5714.html 封面句子4个字霸气:https://www.nanss.com/shenghuo/5853.html 情怀经典句子:https://www.nanss.com/yulu/5702.html 最酷的网名:https://www.nanss.com/mingcheng/5497.html 班级格言:https://www.nanss.com/xuexi/5543.html 8个字口号霸气:https://www.nanss.com/xuexi/5726.html 展望未来的励志句子:https://www.nanss.com/yulu/5963.html 三头六臂造句:https://www.nanss.com/xuexi/5188.html 形容困境中崛起的成语:https://www.nanss.com/xuexi/5608.html 好听群名:https://www.nanss.com/mingcheng/5951.html 最酷的名字:https://www.nanss.com/mingcheng/5041.html 女人运动打卡心情说说:https://www.nanss.com/wenan/5684.html 暧昧的话:https://www.nanss.com/yulu/5514.html 写给爸爸的话暖心到哭:https://www.nanss.com/yulu/5176.html 形容感情越走越远的句子:https://www.nanss.com/yulu/5840.html 王者荣耀名字稀有漂亮符号:https://www.nanss.com/mingcheng/5277.html 安于现状的唯美句子:https://www.nanss.com/yulu/5861.html lol搞笑名字:https://www.nanss.com/mingcheng/4941.html 晒饺子的幽默说说:https://www.nanss.com/wenan/6001.html 简单干净的文案:https://www.nanss.com/wenan/5374.html 恶有恶报的经典句子:https://www.nanss.com/yulu/5821.html
发表评论:
扫码关注
© PostgreSQL中文社区 ... (自2010年起)