corosync+ pacemaker实现pg流复制自动切换(一)
corosync + pacemaker + postgres_streaming_replication 说明: 该文档用于说明以corosync+pacemaker的方式实现PostgreSQL流复制自动切换。注意内容包括有关corosync/pacemaker知识总结以及整个环境的搭建过程和问题处理。 一、介绍Corosync Corosync是由OpenAIS项目分离独立出来的项目,分离后能实现HA信息传输功能的就成为了Corosync,因此Corosync 60%的代码来源于OpenAIS。 Corosync与分离出来的Heartbeat类似,都属于集群信息层,负责传送集群信息以及节点间的心跳信息,单纯HA软件都不存在管理资源的功能,而是需要依赖上层的CRM来管理资源。目前最著名的资源管理器为Pacemaker,Corosync+Pacemaker也成为高可用集群方案中的最佳组合。
Pacemaker Pacemaker,即Cluster Resource Manager(CRM),管理整个HA,客户端通过pacemaker管理监控整个集群。 常用的集群管理工具: (1)基于命令行 crm shell/pcs (2)基于图形化 pygui/hawk/lcmc/pcs Pacemaker内部组件、模块关系图: 二、环境2.1 OS#cat/etc/issue CentOSrelease6.4(Final) Kernelronanm #uname-a Linuxnode12.6.32-358.el6.x86_64#1SMPFriFeb2200:31:26UTC2013x86_64x86_64x86_64GNU/Linux 2.2 IP
node1: eth0 192.168.100.201/24 GW 192.168.100.1 ---真实地址 eth1 10.10.10.1/24 ---心跳地址 eth2 192.168.1.1/24 ---流复制地址 node2: eth0 192.168.100.202/24 GW 192.168.100.1 ---真实地址 eth1 10.10.10.2/24 ---心跳地址 eth2 192.168.1.2/24 ---流复制地址 虚拟地址: eth0:0 192.168.100.213/24 ---vip-master eth0:0 192.168.100.214/24 ---vip-slave eth2:0 192.168.1.3/24 ---vip-rep 2.3软件版本#rpm-qa|grepcorosync corosync-1.4.5-2.3.x86_64 corosynclib-1.4.5-2.3.x86_64 #rpm-qa|greppacemaker pacemaker-libs-1.1.10-14.el6_5.2.x86_64 pacemaker-cli-1.1.10-14.el6_5.2.x86_64 pacemaker-1.1.10-14.el6_5.2.x86_64 pacemaker-cluster-libs-1.1.10-14.el6_5.2.x86_64 #rpm-qa|grepcrmsh crmsh-1.2.6-6.1.x86_64 PostgreSQL Version:9.1.4 三、安装3.1设置YUM源#cat/etc/yum.repos.d/ha-clustering.repo [haclustering] name=HAClustering baseurl=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/ enabled=1 gpgcheck=0 3.2安装pacemaker/corosync/crmsh#yuminstallpacemakercorosynccrmsh 安装后会在/usr/lib/ocf/resource.d下生成相应的ocf资源脚本,如下: #cd/usr/lib/ocf/resource.d/ [root@node1resource.d]#ls heartbeatpacemakerredhat 通过命令查看资源脚本: [root@node1resource.d]#crmralistocf ASEHAagent.shAoEtargetAudibleAlarmCTDBClusterMonDelayDummy EvmsSCCEvmsdFilesystemHealthCPUHealthSMARTICPIPaddr IPaddr2IPsrcaddrIPv6addrLVMLinuxSCSIMailToManageRAID ManageVEPure-FTPdRaid1RouteSAPDatabaseSAPInstanceSendArp ServeRAIDSphinxSearchDaemonSquidStatefulSysInfoSystemHealthVIPArip VirtualDomainWASWAS6WinPopupXenXinetdanything apacheapache.shasteriskclusterfs.shconntrackdcontrolddb2 dhcpddrbddrbd.sheDir88ethmonitorexportfsfio fs.shiSCSILogicalUnitiSCSITargetidsip.shiscsijboss ldirectordlvm.shlvm_by_lv.shlvm_by_vg.shlxcmysqlmysql-proxy mysql.shnamednamed.shnetfs.shnfsclient.shnfsexport.shnfsserver nfsserver.shnginxocf-shellfuncsopenldap.shoracleoracledb.shorainstance.sh oralistener.shoralsnrpgsqlpingpingdportblockpostfix postgres-8.shpoundproftpdremotersyncdrsyslogsamba.sh script.shscsi2reservationservice.shsfexslapdsmb.shsvclib_nfslock symlinksyslog-ngtomcattomcat-5.shtomcat-6.shvarnishvm.sh vmwarezabbixserver 启动corosync: [root@node1~]#servicecorosyncstart StartingCorosyncClusterEngine(corosync):[OK] [root@node2~]#servicecorosyncstart StartingCorosyncClusterEngine(corosync):[OK] [root@node2~]#crmstatus Lastupdated:SatJan1807:00:342014 Lastchange:SatJan1806:58:112014viacrmdonnode1 Stack:classicopenais(withplugin) CurrentDC:node1-partitionwithquorum Version:1.1.10-14.el6_5.2-368c726 2Nodesconfigured,2expectedvotes 0Resourcesconfigured Online:[node1node2] 若出现以下错误可先禁止掉stonith,该错误是因为stonith未配置导致,错误如下: crm_verify[4921]: 2014/01/10_07:34:34 ERROR: unpack_resources: Resource start-up disabled since no STONITH resources have been defined crm_verify[4921]: 2014/01/10_07:34:34 ERROR: unpack_resources: Either configure some or disable STONITH with the stonith-enabled option crm_verify[4921]: 2014/01/10_07:34:34 ERROR: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity 禁止stonith(只在一个节点上执行即可): [root@node1~]#crmconfigurepropertystonith-enabled=false 3.3安装PostgreSQL 安装目录为/opt/pgsql {安装过程略} 为postgres用户配置环境变量: [postgres@node1~]$cat.bash_profile #.bash_profile #Getthealiasesandfunctions if[-f~/.bashrc];then .~/.bashrc fi #Userspecificenvironmentandstartupprograms exportPATH=/opt/pgsql/bin:$PATH:$HOME/bin exportPGDATA=/opt/pgsql/data exportPGUSER=postgres exportPGPORT=5432 exportLD_LIBRARY_PATH=/opt/pgsql/lib:$LD_LIBRARY_PATH 四、配置4.1 hosts设置#vim/etc/hosts 192.168.100.201node1 192.168.100.202node2 4.2配置corosync [root@node1~]#cd/etc/corosync/ [root@node1corosync]#ls corosync.conf.examplecorosync.conf.example.udpuservice.duidgid.d [root@node1corosync]#cpcorosync.conf.examplecorosync.conf [root@node1corosync]#vimcorosync.conf compatibility:whitetank//兼容旧版本 totem{//节点间通信协议定义 version:2 secauth:on//是否开启安全认证 threads:0 interface{//心跳配置 ringnumber:0 bindnetaddr:10.10.10.0//绑定网络 mcastaddr:226.94.1.1//向外发送多播的地址 mcastport:5405//多播端口 ttl:1 } } logging{//日志设置 fileline:off to_stderr:no//是否发送错误信息到标准输出 to_logfile:yes//是否记录到日志文件 to_syslog:yes//是否记录到系统日志 logfile:/var/log/cluster/corosync.log//日志文件,注意/var/log/cluster目录必须存在 debug:off timestamp:on//日志中是否标记时间 logger_subsys{ subsys:AMF debug:off } } amf{ mode:disabled } service{ ver:0 name:pacemaker//启用pacemaker } aisexec{ user:root group:root }
4.3生成密钥{默认利用random生成,但如果中断的系统随机数不够用就需要较长的时间,此时可以通过urandom来替代random} [root@node1corosync]#mv/dev/random/dev/random.bak [root@node1corosync]#ln-s/dev/urandom/dev/random [root@node1corosync]#corosync-keygen CorosyncClusterEngineAuthenticationkeygenerator. Gathering1024bitsforkeyfrom/dev/random. Presskeysonyourkeyboardtogenerateentropy. Writingcorosynckeyto/etc/corosync/authkey. 4.4 SSH互信配置 node1 -> node2 : [root@node1~]#cd.ssh/ [root@node1.ssh]#ssh-keygen-trsa Generatingpublic/privatersakeypair. Enterfileinwhichtosavethekey(/root/.ssh/id_rsa): Enterpassphrase(emptyfornopassphrase): Entersamepassphraseagain: Youridentificationhasbeensavedin/root/.ssh/id_rsa. Yourpublickeyhasbeensavedin/root/.ssh/id_rsa.pub. Thekeyfingerprintis: 2c:ed:1e:a6:a7:cd:e3:b2:7c:de:aa:ff:63:28:9a:19root@node1 Thekey'srandomartimageis: +--[RSA2048]----+ || || || |o| |.S| |o| |E+.| |=o*=oo| |+.*%O=o.| +-----------------+ [root@node1.ssh]#ssh-copy-id-iid_rsa.pubnode2 Theauthenticityofhost'node2(192.168.100.202)'can'tbeestablished. RSAkeyfingerprintisbe:76:cd:29:af:59:76:11:6a:c7:7d:72:27:df:d1:02. Areyousureyouwanttocontinueconnecting(yes/no)?yes Warning:Permanentlyadded'node2,192.168.100.202'(RSA)tothelistofknownhosts. root@node2'spassword: Nowtryloggingintothemachine,with"ssh'node2'",andcheckin: .ssh/authorized_keys tomakesurewehaven'taddedextrakeysthatyouweren'texpecting. [root@node1.ssh]#sshnode2date SatJan1806:36:21CST2014 node2 -> node1 : [root@node2~]#cd.ssh/ [root@node2.ssh]#ssh-keygen-trsa [root@node2.ssh]#ssh-copy-id-iid_rsa.pubnode1 [root@node2.ssh]#sshnode1date SatJan1806:37:31CST2014 4.5同步配置 [root@node1corosync]#scpauthkeycorosync.confnode2:/etc/corosync/ authkey100%1280.1KB/s00:00 corosync.conf100%28082.7KB/s00:00 4.6下载替换脚本 虽然安装了上述软件后会生成pgsql资源脚本,但是其版本过旧,且自带的pgsql不能实现自动切换功能,所以在安装了pacemaker/corosync之后需要从网上下载进行替换,如下: https://github.com/ClusterLabs/resource-agents/tree/master/heartbeat 下载pgsql与ocf-shellfuncs.in 替换: #cppgsql/usr/lib/ocf/resource.d/heartbeat/ #cpocf-shellfuncs.in/usr/lib/ocf/lib/heartbeat/ocf-shellfuncs {注意要将ocf-shellfuncs.in名称改为ocf-shellfuncs,否则pgsql可能会找不到要用的函数。新下载的函数定义文件中添加了一些新功能函数,如ocf_local_nodename等} pgsql资源脚本特性: ●主节点失效切换 master宕掉时,RA检测到该问题并将master标记为stop,随后将slave提升为新的master。 ●异步与同步切换 如果slave宕掉或者LAN中存在问题,那么当设置为同步复制时包含写操作的事务将会被终止,也就意味着服务将停止。因此,为防止服务停止RA将会动态地将同步转换为异步复制。 ●初始启动时自动识别新旧数据 当两个或多个节点上的Pacemaker同时初始启动时,RA通过每个节点上最近的replay location进行比较,找出最新数据节点。这个拥有最新数据的节点将被认为是master。当然,若在一个节点上启动pacemaker或者该节点上的pacemaker是第一个被启动的,那么它也将成为master。RA依据停止前的数据状态进行裁定。 ●读负载均衡 由于slave节点可以处理只读事务,因此对于读操作可以通过虚拟另一个虚拟IP来实现读操作的负载均衡。 4.7启动corosync启动: [root@node1~]#servicecorosyncstart [root@node2~]#servicecorosyncstart 检测状态: [root@node1~]#crmstatus Lastupdated:TueJan2123:55:132014 Lastchange:TueJan2123:37:362014viacrm_attributeonnode1 Stack:classicopenais(withplugin) CurrentDC:node1-partitionwithquorum Version:1.1.10-14.el6_5.2-368c726 2Nodesconfigured,'宋体';font-size:13px;white-space:normal;">{corosync启动成功} |