如何轻松搭建postgresql 集群搭建高可用

VS/NAT原理图:
二、系统环境
实验拓扑:
系统平台:CentOS 6.3
Kernel:2.6.32-279.el6.i686
LVS版本:ipvsadm-1.26
keepalived版本:keepalived-1.2.4
0、安装LVS前系统需要安装popt-static,kernel-devel,make,gcc,openssl-devel,lftp,libnl*,popt*
1、在两台Director Server上分别配置LVS+Keepalived
LVS install -------------
[root@CentOS-LVS_MASTER ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz
[root@CentOS-LVS_MASTER ~]# ln -s /usr/src/kernels/2.6.32-279.el6.i686//usr/src/linux/
[root@CentOS-LVS_MASTER ~]# tar zxvf ipvsadm-1.26.tar.gz
[root@CentOS-LVS_MASTER ~]# cd ipvsadm-1.26
[root@CentOS-LVS_MASTER ipvsadm-1.26]# make && make install
Keepalived install -------------
[root@CentOS-LVS_MASTER ~]# wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
[root@CentOS-LVS_MASTER ~]# tar zxvf keepalived-1.2.4.tar.gz
[root@CentOS-LVS_MASTER ~]# cd keepalived-1.2.4
[root@CentOS-LVS_MASTER keepalived-1.2.4]# ./configure && make && make install
#########&将keepalived做成启动服务,方便管理##########
[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@CentOS-LVS_MASTER ~]# mkdir /etc/keepalived/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@CentOS-LVS_MASTER ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@CentOS-LVS_MASTER ~]# service keepalived start | stop
2、开启路由转发
[root@CentOS-LVS_MASTER ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@CentOS-LVS_MASTER ~]# sysctl -p
3、配置Keepalived
[root@CentOS-LVS_MASTER ~]# less /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
上修改为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
virtual_ipaddress {
10.0.0.227
vrrp_instance LAN_GATEWAY {
state MASTER
interface eth1
virtual_router_id 52
priority 100
上修改为80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
virtual_ipaddress {
192.168.10.10
virtual_server 10.0.0.227 80 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 5
protocol TCP
real_server 192.168.10.4 80 {
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
real_server 192.168.10.5 80 {
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
BACKUP服务器同上配置,先安装lvs再安装keepalived,然后配置/etc/keepalived/keepalived.conf,只需将批注部分改一下即可。
4、分别在2台Real Server上面设置网关
把网关都设置成:192.168.10.10
5、在2台RealServer中分别配置HTTP
[root@WEB1 ~]# yum -y install httpd
[root@WEB1 ~]# cd /var/www/html/
[root@WEB1 html]# cat index.html
&h1&WEB1/192.168.10.4&/h1&
[root@WEB1 html]# /etc/init.d/httpd start
另一台机器配置一样,过程略。
6、分别在CentOS-LVS_MASTER、CentOS-LVS_BACKUP上执行service keepalived start启动keepalived就可实现负载均衡及高可用集群了;
[root@CentOS-LVS_MASTER keepalived]# service keepalived start
####高可用性测试####
模拟故障,将CentOS-LVS_MASTER上的keepalived服务停掉,然后观察CentOS-LVS_BACKUP上的日志,信息如下
从日志中可知,主机出现故障后,备机立刻检测到,此时备机变为MASTER角色,并且接管了主机的虚拟IP资源,最后将虚拟IP绑定在etho设备上。
将CentOS-LVS_MASTER&上的keepalived服务开启后,CentOS-LVS_BACKUP的日志状态。
从日志可知,备机在检测到主机重新恢复正常后,释放了虚拟IP资源重新成为BACKUP角色
####故障切换测试####
故障切换是测试当某个节点出现故障后,Keepalived监制模块是否能及时发现然后屏蔽故障节点,同时将服务器转移到正常节点来执行。
将WEB2节点服务停掉,假设这个节点出现故障,然后主、备机日志信息如下
从以上可以看出,Keepalived监控模块检测到192.168.10.5这台主机出现故障后,将WEB2从集群系统中剔除掉了。&此时访问http://10.0.0.227只能看到WEB1了)
重新启动WEB2节点的服务,日志信息如下:
Keepalived监控模块检测到192.168.10.5这台主机恢复正常后,又将此节点加入集群系统中,再次访问就可以访问到WEB2页面了)
RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡
阅读(...) 评论()1669人阅读
在阅读的过程中有任何问题,欢迎一起交流
&&& Keepalived通过对服务器池中的对象进行健康检查,同时实现了对失效机器和服务的故障隔离,并把其从服务池中删除;如果失效的机器或者服务恢复正常,Keepalived会自动将其加入服务池中。PostgreSQL数据库有时会发生宕机的情况,因此使用Keepalived对两台服务器进行配置,主服务器负责用户交互,从服务器负责实时备份,当发生故障时可以实现高可用。
二、工作原理
当 Master 与 Slave 均运作正常时, Master负责服务,Slave负责备份;
当 Master 挂掉Slave 正常时, Slave接管服务,同时关闭主从复制功能;
当 Master 恢复正常,则从Slave同步数据,同步数据之后关闭主从复制功能,恢复Master身份,于此同时Slave等待Master同步数据完成之后,恢复Slave身份。
三、配置过程
环境:Master: 10.6.1.143
&&&&&&&&&&& Slave: 10.6.1.144
&&&&&&&&&&&&Virtural IP Address (VIP): 10.6.1.200
1、在Master和Slave上安装Keepalived
$ sudo apt-get install keepalived
2、修改Master和Slave的/etc/hosts文件
$ sudo vim /etc/hosts
3、默认安装完成keepalived之后是没有配置文件的,因此我们需要手动创建:
首先,在Master上创建如下配置文件:
$ sudo vim /etc/keepalived/keepalived.conf
该文件主要配置监控脚本、监控时间、监控网络、虚机IP等信息
! Configuration File for keepalived
global_defs {
&&& router_id PostgreSQL-HA2
vrrp_script monitor_pgsql_status {
&& script &/etc/keepalived/scripts/monitor_pgsql_status.sh&
&& interval 30
vrrp_instance V_PostgreSQL-HA {
state BACKUP
&& interface eth0
&& notify_master /etc/keepalived/scripts/to_master.sh
&& notify_backup /etc/keepalived/scripts/to_backup.sh
&& notify_stop /etc/keepalived/scripts/to_backup.sh
&& virtual_router_id 51
&& priority 80
&& advert_int 1
&& garp_master_delay 10
&& authentication {
&&&&&&&& auth_type PASS
&&&&&&&& auth_pass&password
&& track_script {
&&&&&& monitor_pgsql_status
&& virtual_ipaddress {
&&&&&&&虚拟IP/端口 dev eth1
然后,在Slave上创建如下配置文件:
$ sudo vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
&&& router_id PostgreSQL-HA2
vrrp_script monitor_pgsql_status {
&& script &/etc/keepalived/scripts/monitor_pgsql_status.sh&
&& interval 30
vrrp_instance V_PostgreSQL-HA {
state BACKUP
&& interface eth0
&& notify_master /etc/keepalived/scripts/to_master.sh
&& notify_backup /etc/keepalived/scripts/to_backup.sh
&& notify_stop /etc/keepalived/scripts/to_backup.sh
&& virtual_router_id 51
&& priority 80
&& advert_int 1
&& garp_master_delay 10
&& authentication {
&&&&&&&& auth_type PASS
&&&&&&&& auth_pass&password
&& track_script {
&&&&&& monitor_pgsql_status
&& virtual_ipaddress {
&&&&&&&虚拟IP/端口 dev eth1
4、在Master和Slave上创建监控Redis的脚本
1. 需要在PostgreSQL中创建postgres用户,并赋予SELECT执行权限(可其权限限定至最小范围);
2. 根据实际情况修改变量psql和VIP。
$ mkdir /etc/keepalived/scripts
$ vim /etc/keepalived/scripts/monitor_pgsql_status.sh
5、编写以下负责运作的关键脚本:
notify_master /etc/keepalived/scripts/to_master.sh
notify_backup /etc/keepalived/scripts/to_backup.sh
因为Keepalived在转换状态时会依照状态来呼叫:
当进入Master状态时会呼叫notify_master,在to_master.sh中执行相应的操作
当进入Backup状态时会呼叫notify_backup,在to_backup.sh中执行相应的操作
当Keepalived程序终止时则呼叫notify_backup
6、给脚本都加上可执行权限:
$ sudo chmod +x /etc/keepalived/scripts/*.sh
可以使用如下命令进行主从模式的切换:
/etc/init.d/keepalived restart
附:agent部署过程:
解压安装DBCheck包
为start.sh何stop.sh脚本赋予权限 并测试
进入/etc/keepalived/scripts目录
在to_master.sh脚本最后添加:cd /home/test/DBCheck && ./start.sh conf
在to_backup.sh脚本开头添加:cd /home/test/DBCheck && ./stop.sh kill
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:51094次
积分:1157
积分:1157
排名:千里之外
原创:66篇
转载:11篇
评论:12条
(1)(3)(6)(4)(6)(10)(15)(3)(16)(1)(12)君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
PostgreSQL高可用新方案-朱贤文
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer--144.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口利用pgpool实现PostgreSQL的高可用_数据库技术_Linux公社-Linux系统门户网站
你好,游客
利用pgpool实现PostgreSQL的高可用
来源:oschina.net&
作者:Suregogo
这里利用pgpool-ii实现PG的高可用。基于流复制的方式,两节点自动切换:
1、单pgpool
pgpool:192.168.238.129data1:192.168.238.130data2:192.168.238.131
c.配置互信
ssh-copy-id ha@node1ssh-copy-id ha@node2&d.数据库节点配置,请参照《
e.pgpool配置:
listen_addresses = '*'backend_hostname0 = 'node1'backend_port0 = 5432backend_weight0 = 1backend_data_directory0 = '/home/ha/pgdb/data'backend_flag0 = 'ALLOW_TO_FAILOVER'&backend_hostname1 = 'node2'backend_port1 = 5432backend_weight1 = 1backend_data_directory1 = '/home/ha/pgdb/data'backend_flag1 = 'ALLOW_TO_FAILOVER'&enable_pool_hba = onpool_passwd = 'pool_passwd'&pid_file_name = '/home/ha/pgpool/pgpool.pid'logdir = '/home/ha/pgpool/log'&health_check_period = 1health_check_user = 'ha'health_check_password = 'ha'&failover_command = '/home/ha/pgdb/fail.sh %H'&recovery_user = 'ha'recovery_password = 'ha'&f.fail.sh
# Failover command for streaming replication.# This script assumes that DB node 0 is primary, and 1 is standby.## If standby goes down, do nothing. If primary goes down, create a# trigger file so that standby takes over primary node.## Arguments: $1: failed node id. $2: new master hostname. $3: path to# trigger file.&new_master=$1trigger_command="/home/ha/pgdb/bin/pg_ctl -D /home/ha/pgdb/data promote -m fast"&# Do nothing if standby goes down.if [ $failed_node = 1 ]; then& & & & exit 0;fi&# Create the trigger file./usr/bin/ssh -T $new_master $trigger_command&exit 0;&
g.建立pool_passwd
pg_md5 -m -p -u postgres pool_passwd&& & & & PS:在9.1之前一直用的是trigger_file,这里建议用promote -m fast的方式,因为 & pg_ctl promote -m fast will skip the checkpoint at end of recovery so that we can achieve very fast failover when the apply delay is low. Write new WAL record XLOG_END_OF_RECOVERY to allow us to switch timeline correctly for downstream log readers. If we skip synchronous end of recovery checkpoint we request a normal spread checkpoint so that the window of re-recovery is low. Simon Riggs and Kyotaro Horiguchi, with input from Fujii Masao. Review by Heikki Linnakangas &
h.测试&pgpool节点
[ha@node0 pgdb]$ pgpool -n -d & /tmp/pgpool.log 2&&1 &[1] 22928[ha@node0 pgdb]$ psql -h 192.168.238.129 -p 9999 -d postgres -U haPassword for user ha: psql (9.4.5)Type "help" for help.&postgres=# insert into test values (8);INSERT 0 1postgres=# select *&id ----& 1& 2& 3& 4& 6& 8(6 rows)&node1节点:
[ha@localhost pgdb]$ ps -ef | grep postroot& & &
2124& & & 1& 0 Dec26 ?& & & & 00:00:00 /usr/libexec/postfix/masterpostfix& & 2147&
2124& 0 Dec26 ?& & & & 00:00:00 qmgr -l -t fifo -upostfix&
:01 ?& & & & 00:00:00 pickup -l -t fifo -uha& & & & 13395& & & 1& 0 06:06 pts/3& & 00:00:00 /home/ha/pgdb/bin/postgresha& & & & 1& 0 06:06 ?& & & & 00:00:00 postgres: checkpointer process&
ha& & & & 1& 0 06:06 ?& & & & 00:00:00 postgres: writer process&
ha& & & & 1& 0 06:06 ?& & & & 00:00:00 postgres: wal writer process&
ha& & & & 1& 0 06:06 ?& & & & 00:00:00 postgres: autovacuum launcher process&
ha& & & & 1& 0 06:06 ?& & & & 00:00:00 postgres: stats collector process&
ha& & & & 1& 0 06:07 ?& & & & 00:00:00 postgres: wal sender process rep 192.168.238.131(59415) streaming 0/ha& & & & 13418&
:07 pts/3& & 00:00:00 grep post[ha@localhost pgdb]$ kill -9 13395&pgpool节点:
postgres=# insert into test values (8);server closed the connection unexpectedly& & This probably means the server terminated abnormally& & before or while processing the request.The connection to the server was lost. Attempting reset: Succeeded.postgres=# insert into test values (8);INSERT 0 1postgres=# insert into test values (8);INSERT 0 1postgres=# select *&id ----& 1& 2& 3& 4& 6& 8& 8& 8(8 rows)
2.两个pgpool节点
pgpool:192.168.238.129 &span&&/span& pgpool:192.168.238.131node1:192.168.238.130node2:192.168.238.131
c.配置互信,同上。d.数据库节点配置,同上。e.pgpool配置node1
f.配置pgpool(主)
listen_addresses = '*'backend_hostname0 = 'node1'backend_port0 = 5432backend_weight0 = 1backend_data_directory0 = '/home/ha/pgdb/data/'backend_flag0 = 'ALLOW_TO_FAILOVER'backend_hostname1 = 'node2'backend_port1 = 5432backend_weight1 = 1backend_data_directory1 = '/home/ha/pgdb/data/'backend_flag1 = 'ALLOW_TO_FAILOVER'enable_pool_hba = onauthenticationpool_passwd = 'pool_passwd'pid_file_name = '/home/ha/pgpool/pgpool.pid'logdir = '/tmp/log'master_slave_mode = onmaster_slave_sub_mode = 'stream'sr_check_period =2sr_check_user = 'ha'sr_check_password = 'ha'health_check_period = 1health_check_timeout = 20health_check_user = 'ha'health_check_password = 'ha'failover_command = '/home/ha/pgpool/fail.sh %H'recovery_user = 'ha'recovery_password = 'ha'use_watchdog = onwd_hostname = 'node1'& & #本端delegate_IP = '192.168.238.151'#利用ifconfig,查看网卡if_up_cmd = 'ifconfig eth1:0 inet $_IP_$ netmask 255.255.255.0'if_down_cmd = 'ifconfig eth1:0 down'heartbeat_destination0 = 'node2' #对端heartbeat_device0 = 'eth0'other_pgpool_hostname0 = 'node2' #对端other_pgpool_port0 =9999other_wd_port0 = 9000
g.配置pgpool(从)&listen_addresses = '*'backend_hostname0 = 'node1'backend_port0 = 5432backend_weight0 = 1backend_data_directory0 = '/home/ha/pgdb/data/'backend_flag0 = 'ALLOW_TO_FAILOVER'backend_hostname1 = 'node2'backend_port1 = 5432backend_weight1 = 1backend_data_directory1 = '/home/ha/pgdb/data/'backend_flag1 = 'ALLOW_TO_FAILOVER'enable_pool_hba = onauthenticationpool_passwd = 'pool_passwd'pid_file_name = '/home/ha/pgpool/pgpool.pid'logdir = '/tmp/log'master_slave_mode = onmaster_slave_sub_mode = 'stream'sr_check_period =2sr_check_user = 'ha'sr_check_password = 'ha'health_check_period = 1health_check_timeout = 20health_check_user = 'ha'health_check_password = 'ha'failover_command = '/home/ha/pgpool/fail.sh %H'recovery_user = 'ha'recovery_password = 'ha'use_watchdog = onwd_hostname = 'node2'& & #本端delegate_IP = '192.168.238.151'#利用ifconfig,查看网卡if_up_cmd = 'ifconfig eth1:0 inet $_IP_$ netmask 255.255.255.0'if_down_cmd = 'ifconfig eth1:0 down'heartbeat_destination0 = 'node1' #对端heartbeat_device0 = 'eth1'other_pgpool_hostname0 = 'node1' #对端other_pgpool_port0 =9999other_wd_port0 = 9000
h.fail.sh&# Failover command for streaming replication.# This script assumes that DB node 0 is primary, and 1 is standby.## If standby goes down, do nothing. If primary goes down, create a# trigger file so that standby takes over primary node.## Arguments: $1: failed node id. $2: new master hostname. $3: path to# trigger file.&new_master=$1trigger_command="/home/ha/pgdb/bin/pg_ctl -D /home/ha/data start"&# Do nothing if standby goes down.if [ $failed_node = 1 ]; then& & & & exit 0;fi&# Create the trigger file./usr/bin/ssh -T $new_master $trigger_command&exit 0;&& & & & & & i.建立pool_passwd
pg_md5 -m -p -u postgres pool_passwd
j.测试&#数据库、pgpool启动[ha@node0 pgdb]$ psql -h 192.168.238.151 -p 9999 -d postgres -U haPassword for user ha: psql (9.4.5)Type "help" for help.&postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# --杀掉node1的数据库进程postgres=# insert into test values (9);server closed the connection unexpectedly& & This probably means the server terminated abnormally& & before or while processing the request.The connection to the server was lost. Attempting reset: Succeeded.postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1--杀掉node1的pgpool进程postgres=# insert into test values (9);server closed the connection unexpectedly& & This probably means the server terminated abnormally& & before or while processing the request.The connection to the server was lost. Attempting reset: Succeeded.postgres=# insert into test values (9);INSERT 0 1postgres=# insert into test values (9);INSERT 0 1postgres=#
------------------------------------华丽丽的分割线------------------------------------
Server 14.04 下安装 PostgreSQL 9.3.5 数据库&
6.3环境下yum安装PostgreSQL 9.3
PostgreSQL缓存详述
Windows平台编译 PostgreSQL
Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装
Ubuntu上的phppgAdmin安装及配置
CentOS平台下安装PostgreSQL9.3
PostgreSQL配置Streaming Replication集群
如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin&
------------------------------------华丽丽的分割线------------------------------------
PostgreSQL 的详细介绍:PostgreSQL 的下载地址:
本文永久更新链接地址:
相关资讯 & & &
& (08月14日)
& (02月12日)
& (12/30/:29)
& (03月04日)
& (01月19日)
& (12/21/:21)
   同意评论声明
   发表
尊重网上道德,遵守中华人民共和国的各项有关法律法规
承担一切因您的行为而直接或间接导致的民事或刑事法律责任
本站管理人员有权保留或删除其管辖留言中的任意内容
本站有权在网站内转载或引用您的评论
参与本评论即表明您已经阅读并接受上述条款}

我要回帖

更多关于 postgresql 高可用 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信