postgresql9.1.4+pacemker实现Master-Slave.doc_第1页
postgresql9.1.4+pacemker实现Master-Slave.doc_第2页
postgresql9.1.4+pacemker实现Master-Slave.doc_第3页
postgresql9.1.4+pacemker实现Master-Slave.doc_第4页
postgresql9.1.4+pacemker实现Master-Slave.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于Pacemaker实现postgreSQL9.1.4的主从备份本文主要介绍使用Pacemaker、heartbeat实现postgresql9.1.4的基于Streaming Replication的hot standby。其中postgresql9.1的resource agent (RA)的下载地址为:/t-matsuo/resource-agents/blob/pgsql-rep-master/heartbeat/pgsql。本文的pacemaker配置都是基于该RA的。场景: 两台机器 ip分别为05和04,主机名分别为: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_standbymax_wal_senders=2wal_keep_segments=32archive_mode=onarchive_command=cp %p /data/archive/%fhot_standby=on4) 修改配置文件pg_hba.conf,添加如下内容:host replication repl 04/32 trusthost all all 04/32 trust5) 在data目录下创建archive目录:mkdir /data/archive;6) 手动启动postgresql:/pginstall/bin/pg_ctl start -D /data -l /data/postgresql.log7) 登录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 /datascp /data.tar 04:/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 local0logfile /var/log/ha-logdebugfile /var/log/ha-debugbcast eth0warntime 3deadtime 6initdead 60keepalive 1node h105node h104crm respawn配置了两个节点h105和h104.把该文件copy到h104上:scp /etc/ha.d/ha.cf 04:/etc/ha.d/2) 为HeartBeat创建key文件:签名算法可以是md5 sha1 crc crc32c,这里采用sha1,节点h104上需要拷贝这个key文件, 两边的key文件必须相同!(echo -ne auth 1n1 sha1 ;dd if=/dev/urandom bs=512 count=1 | openssl sha1 ) /etc/ha.d/authkeys chmod 600 /etc/ha.d/authkeyspacemaker的配置: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=-p 5432 pgdba=postgres rep_mode=async repuser=repl primary_conninfo_opt=password=repl node_list=h105 h104 master_ip=05 restore_command=scp 05:/data/archive/%f %p op start interval=0s timeout=60s on-fail=restart op monitor interval=7s timeout=60s on-fail=restart op monitor interval=2s role=Master timeout=70s on-fail=restart op promote interval=0s timeout=60s on-fail=restart op demote interval=0s timeout=60s on-fail=block op stop interval=0s timeout=60s on-fail=blockms msPostgres postgresql meta master-max=1 master-node-max=1 clone-max=2 clone-node-max=1 notify=truelocation rsc_location msPostgres rule $id=rsc_location-rule $role=Master 200: #uname eq h105 rule $id=rsc_location-rule-0 $role=Master 100: #uname eq h104property $id=cib-bootstrap-options dc-version=1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e8 cluster-infrastructure=Heartbeat crmd-transition-delay=0s stonith-enabled=false no-quorum-policy=ignore last-lrm-refresh=1341305646 expected-quorum-votes=2 symmetric-cluster=true startup-fencing=falsersc_defaults $id=rsc-options resource-stickiness=INFINITY migration-threshold=1pacemaker的配置只需要在一个节点上进行就可以了,因为pacemaker在启动后会自动把配置文件同步到在/etc/ha.d/ha.cf中配置的所有节点上。4) 在h105上启动heartbeat:/etc/init.d/heartbeat start启动完成后执行crm_mon 显示如下信息:=Last updated: Mon Jul 16 14:00:53 2012Stack: HeartbeatCurrent DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorumVersion: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e82 Nodes configured, 2 expected votes0 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 2012Stack: HeartbeatCurrent DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorumVersion: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e82 Nodes configured, 2 expected votes1 Resources configured.=Online: h105 OFFLINE: h104 Master/Slave Set: msPostgres Slaves: h105 Stopped: postgresql:1 一段时间后,显示如下信息:=Last updated: Mon Jul 16 14:03:31 2012Stack: HeartbeatCurrent DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorumVersion: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e82 Nodes configured, 2 expected votes1 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 2012Stack: HeartbeatCurrent DC: h105 (2319a521-3776-4ae9-b434-b44d1811ccb8) - partition with quorumVersion: 1.0.11-9af47ddebcad19e35a61b2a20301dc038018e8e82 Nodes configured, 2 expected votes1 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 日志归档的命令,该命令把要归档的日志copy到/data/archive/目录下;hot_standby=on 开启热备模式,在本例中master节点也要配置该参数,否则master在启动时会出错,该参数可以时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后面的参数是管理该资源的资源代理(RA)pgsql的参数,op后面是RA提供的一些操作。rep_mode=async 复制模式none/async/sync:默认是none; node_list=h105 h104 节点链表:包含master和所有slave节点的主机名; master_ip=05 master节点ip,主要用在配置slave节点的recovery.conf中的primary_conninfo参数;restore_command=scp 05:/data/archive/%f %p 归档恢复命令:要用在配置slave节点的recovery.conf中的 restore_command参数ms msPostgres postgresql 定义一个master/slave资源,meta后面为该资源的一些属性:master-max=1 master节点的最大个数;master-node-max=1 每个节点上master的最大值;clone-max=2 克隆的最大个数;clone-node-max=1 每个节点上克隆的最大个数;notify=true 4 遇到的问题和解决办法1.unknow error查看输出的日志信息,一般而言unknow error类型的错误是在postgresql启动之前产生的,可能是环境变量等因素,我遇到的是LD_LBRARY_PATH环境变量没有设置,导致在启动的时候postgresql找不到库文件,执行export LD_LBRARY_PATH=XXX/lib即可(XXX为你的postgresql的安装路径,可能需要在root和postgres这两个用户下都执行上面的语句。)2.unknow exec error一般而言unknow exec error类型的错误是在postgresql启动过程中或者启动之后产生的,可能是postgresql的配置不当导致,可以查看postgresql的输出日志查找原因。pacemaker的日志信息和RA输出的日志信息,默认的情况下在/var/log/messages中,该文件的内容很丰富,可以协助查找问题。3.在pacemaker配置来虚拟ip后,slave启动失败?在配置了虚拟ip后,slave启动失败,查看postgresql输出日志看到如下错误:FATAL: timeline 2 of the primary does not match recovery target timeline 1该问题出现的原因是,使用虚拟ip后如果需要执行ssh命令,则需要认证。虽然我们在开始交换了两个主机的密匙,但是虚拟ip和两个主机的ip均不一样,导致在postgresql在执行恢复命令scp 00:/var/lib/pgsql/data/archive/%f %p时链接不到主机,从而导致错误。修改方法是:在从机上手动使用ssh登录 00即可,即执行ssh 00.4.启动heartbeat时,报在/usr/etc/ha.d/下找不到ha.cf配置文件?解决办法:重新编译安装glue,resource,heartbeat,pacemaker,在编译之前的configure 加上sysconfdir=/etc参数。5. 故障切换时,可能会遇到promote操作超时。在pacemaker中为postgres

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论