基于Pacemaker实现postgreSQL9.1.4的主从备份
本文主要介绍使用Pacemaker、heartbeat实现postgresql9.1.4的基于Streaming Replication的hot standby。其中postgresql9.1的resource agent (RA)的下载地址为:https://github.com/t-matsuo/resource-agents/blob/pgsql-rep-master/heartbeat/pgsql。本文的pacemaker配置都是基于该RA的。
场景: 两台机器 ip分别为192.168.4.105和192.168.4.104,主机名分别为:h105和
h104,其中h105为master主机。
1 PostgreSQL的主机配置和从机配置
1) 在两台机器上安装postgresql9.1.4,pacemaker,heartbeat,其中pacemaker的安装方法参
考文档《pacemaker的安装步骤.doc》;
master主机:
2) 初始化数据库,本文假设数据目录在~/data,postgresql安装目录为~/pginstall;
3) 修改配置文件postgresql.conf,添加如下内容: listen_addresses='*' wal_level=hot_standby max_wal_senders=2 wal_keep_segments=32 archive_mode=on
archive_command='cp %p ~/data/archive/%f' hot_standby=on
4) 修改配置文件pg_hba.conf,添加如下内容:
host replication repl 192.168.4.104/32 trust
host all all 192.168.4.104/32 trust 5) 在data目录下创建archive目录:mkdir ~/data/archive; 6) 手动启动postgresql:
~/pginstall/bin/pg_ctl start -D ~/data -l ~/data/postgresql.log
7) 登录postgres数据库,创建repl超级用户,该用户用来进行流复制;
~/pginstall/bin/psql postgres postgres -p 5432 -c “create user repl superuser password 'repl';”
8) 对数据库做一次基础备份:
~/pginstall/bin/psql postgres postgres -p 5432 -c “select pg_start_backup('base');” 9) 打包当前数据目录,然后复制到h104机器上: tar -xvf ~/data.tar ~/data
scp ~/data.tar 192.168.4.104:~/
10) 结束当前的基础备份,并关闭当前的postgresql:
~/pginstall/bin/psql postgres postgres -p 5432 -c “select pg_stop_backup();” ~/pginstall/bin/pg_ctl stop -D ~/data -l ~/data/postgresql.log 注意:对当前数据库做基础备份这几步是必须要做的。 Slave机器:
11) 解压从主机copy的data.tar tar -xvf data.tar
12) 到此主机和从机的准备工作已经做完了。
注意:在slave 上不要自己配置recovery.conf 文件,因为RA给我们自动生成。既使我们自己配置了,也会被RA覆盖掉。
2 Pacemaker的配置
本文的pacemaker版本是1.0.9. heartbeat的配置:
1) h105上修改配置文件/etc/ha.d/ha.cf: logfacility local0
logfile /var/log/ha-log
debugfile /var/log/ha-debug bcast eth0 warntime 3 deadtime 6 initdead 60 keepalive 1 node h105 node h104 crm respawn
配置了两个节点h105和h104.
把该文件copy到h104上:scp /etc/ha.d/ha.cf 192.168.4.104:/etc/ha.d/
2) 为HeartBeat创建key文件:签名算法可以是md5 sha1 crc crc32c,这里采用sha1,节点h104上需要拷贝这个key文件, 两边的key文件必须相同!
(echo -ne \/etc/ha.d/authkeys
chmod 600 /etc/ha.d/authkeys
pacemaker的配置:
3) 在h105上新建pacemaker配置文件postgresql.crm,添加如下内容: primitive postgresql ocf:heartbeat:pgsql \\ params pgctl=\~/pginstall/bin/pg_ctl\ psql=\~/pginstall/bin/psql\ pgdata=\~/data\
logfile=\~/data/postgresql.log\start_opt=\pgdba=\rep_mode=\repuser=\
primary_conninfo_opt=\node_list=\master_ip=\
restore_command=\~/data/archive/%f %p\ op start interval=\ op monitor interval=\ op monitor interval=\t\
op promote interval=\ op demote interval=\ op stop interval=\ms msPostgres postgresql \\
meta master-max=\notify=\
location rsc_location msPostgres \\
rule $id=\ rule $id=\property $id=\
dc-version=\ cluster-infrastructure=\ crmd-transition-delay=\ stonith-enabled=\ no-quorum-policy=\ last-lrm-refresh=\ expected-quorum-votes=\ symmetric-cluster=\ startup-fencing=\rsc_defaults $id=\
resource-stickiness=\ migration-threshold=\ pacemaker的配置只需要在一个节点上进行就可以了,因为pacemaker在启动后会自动把配置文件同步到在/etc/ha.d/ha.cf中配置的所有节点上。 4) 在h105上启动heartbeat: /etc/init.d/heartbeat start
启动完成后执行crm_mon 显示如下信息: ============
Last updated: Mon Jul 16 14:00:53 2012 Stack: Heartbeat
Current DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorum Version: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e8 2 Nodes configured, 2 expected votes 0 Resources configured. ============
Online: [ h105 ] OFFLINE: [ h104 ]
5) 加载pacemaker配置文件postgresql.crm: crm configure load update postgresql.crm 执行crm_mon 显示如下信息: ============
Last updated: Mon Jul 16 14:03:17 2012 Stack: Heartbeat
Current DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorum Version: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e8 2 Nodes configured, 2 expected votes 1 Resources configured. ============
Online: [ h105 ] OFFLINE: [ h104 ]
Master/Slave Set: msPostgres Slaves: [ h105 ]
Stopped: [ postgresql:1 ] 一段时间后,显示如下信息: ============
Last updated: Mon Jul 16 14:03:31 2012 Stack: Heartbeat
Current DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorum Version: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e8 2 Nodes configured, 2 expected votes 1 Resources configured. ============
Online: [ h105 ] OFFLINE: [ h104 ]
Master/Slave Set: msPostgres Masters: [ h105]
Stopped: [ postgresql:1 ]
从上面的信息可以看出,pacemaker已经确定了h105为master。 6) 在h104上启动heartbeat: /etc/init.d/heartbeat start
启动完成后执行crm_mon -Af -1 显示如下信息: ============
Last updated: Mon Jul 16 13:56:56 2012 Stack: Heartbeat
Current DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with q uorum
Version: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e8 2 Nodes configured, 2 expected votes 1 Resources configured.
============
Online: [ h105 h104 ]
Master/Slave Set: msPostgres Masters: [ h105 ] Slaves: [ h104 ]
7) 至此postgreSQL9.1的Master/Slave配置和启动完成。
3 相关参数介绍
3.1 PostgreSQL配置参数
动。
archive_mode=on 开启日志归档模式;
archive_command='cp %p ~/data/archive/%f' 日志归档的命令,该命令把要归档的日志hot_standby=on 开启热备模式,在本例中master节点也要配置该参数,否则master
copy到~/data/archive/目录下;
在启动时会出错,该参数可以时standby 在master没有启动的情况下以read only 模式启
下面解释一下为什么master节点的postgresql也要配置hot_standby=on,
Pacemaker的Master/Slave模式的启动原理是所有的节点在启动时都以slave模式启动,然后Pacemaker再根据规则挑选一个节点promote为master节点。所以我们在h105上启动pacemaker时,h105上的postgresql会以standby模式启动,然后pacemaker再把它promote为master。hot_standby=on可以保证h105上的postgresql在没有master节点的情况下以standby的read only模式启动。
3.2 Pacemaker配置参数
primitive postgresql ocf:heartbeat:pgsql 配置一个postgresql资源, params后面的参数rep_mode=\ 复制模式[none/async/sync]:默认是none;
是管理该资源的资源代理(RA)pgsql的参数,op后面是RA提供的一些操作。 node_list=\ 节点链表:包含master和所有slave节点的主机名; master_ip=\ master节点ip,主要用在配置slave节点的recovery.conf中的primary_conninfo参数;
restore_command=\~/data/archive/%f %p\归档恢复命令:要用在配置slave节点的recovery.conf中的 restore_command参数 性:
ms msPostgres postgresql 定义一个master/slave资源,meta后面为该资源的一些属
搜索“diyifanwen.net”或“第一范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,第一范文网,提供最新高中教育postgresql9.1.4+pacemker实现Master-Slave 全文阅读和word下载服务。
相关推荐: