使用CRM方式管理Heartbeat

Heartbeat 是一个基于Linux开源的,被广泛使用的高可用集群系统。主要包括心跳服务和资源接管两个高可用集群组件。其资源管理方式可以使用基于haresource文件方式进行管理,也可以通过基于CIB方式进行管理。基于CIB方式其集群资源采用了xml格式来保存其资源配置信息。可以把基于haresouce文件管理的资源转换为基于CIB方式管理。其最大的优势是通过CIB方式管理使得集群的管理更方便和易于监控。本文主要描述crm方式命令行管理heartbeat。

一、相关配置文件描述
###节点信息
# more /etc/hosts
127.0.0.1 localhost.localdomain localhost

#public ip
192.168.21.10 orasrv1.xlk.com orasrv1
192.168.21.13 orasrv2.xlk.com orasrv2

#private ip
192.168.144.128 orasrv1-priv.xlk.com orasrv1-priv
192.168.144.129 orasrv2-priv.xlk.com orasrv2-priv

###配置文件信息
# cat /etc/ha.d/ha.cf        ###ha.cf文件
logfile /var/log/ha-log
keepalive      2
deadtime        15
warntime        30
initdead        60
udpport        694
bcast          eth0
auto_failback  off
node orasrv1.xlk.com
node orasrv2.xlk.com
ping 192.168.21.254
respawn hacluster  /usr/lib64/heartbeat/ipfail
crm respawn ###此项用于定义开启crm方式,或者说是pacemaker(heartbeat V3后)

#CRM与Resources并不兼容,如果ha.cf中定义了CRM,HA服务不会去读取/etc/ha.d/haresources配置文件
#在日志文件中会看到这样的提示:This file is not used because crm is enabled

# cat /etc/ha.d/authkeys ###authkeys文件
auth 1
1 md5 74a6a503b5bec9373b22bb630608df41

# more /etc/ha.d/haresources  ###资源文件
orasrv1.xlk.com IPaddr::192.168.21.1 httpd

###使用ha_propagate同步配置文件,注意,不包含haresource文件
###ha_propagate使用的基于等效ssh下scp命令方式,同时连接到节点上使用chkconfig开启heartbeat自启动
[root@orasrv1 heartbeat]# /usr/lib64/heartbeat/ha_propagate

二、启动heartbeat服务
# service heartbeat start                ###节点1
# ssh orasrv2 'service heartbeat start'  ###节点2
# netstat -nltp|grep 5560                ###查看mgmtd端口
tcp        0      0 0.0.0.0:5560                0.0.0.0:*                  LISTEN      4128/mgmtd         
# ssh orasrv2 'netstat -nltp|grep 5560'
tcp        0      0 0.0.0.0:5560                0.0.0.0:*                  LISTEN      3434/mgmtd   

###查看heartbeat相关进程
# ps -AHfww | grep heartbeat |grep -v grep
root      5564      1  0 Nov11 ?        00:00:40  heartbeat: master control process
nobody    5567  5564  0 Nov11 ?        00:00:04    heartbeat: FIFO reader       
nobody    5568  5564  0 Nov11 ?        00:00:09    heartbeat: write: bcast eth0 
nobody    5569  5564  0 Nov11 ?        00:00:03    heartbeat: read: bcast eth0 
nobody    5570  5564  0 Nov11 ?        00:00:07    heartbeat: write: ping 192.168.21.254
nobody    5571  5564  0 Nov11 ?        00:00:02    heartbeat: read: ping 192.168.21.254
496        5575  5564  0 Nov11 ?        00:00:04    /usr/lib64/heartbeat/ccm
496        5576  5564  0 Nov11 ?        00:00:07    /usr/lib64/heartbeat/cib
nobody    5577  5564  0 Nov11 ?        00:00:03    /usr/lib64/heartbeat/lrmd -r
nobody    5578  5564  0 Nov11 ?        00:00:04    /usr/lib64/heartbeat/stonithd
496        5579  5564  0 Nov11 ?        00:00:02    /usr/lib64/heartbeat/attrd
496        5580  5564  0 Nov11 ?        00:00:03    /usr/lib64/heartbeat/crmd
496        8978  5580  0 08:55 ?        00:00:00      /usr/lib64/heartbeat/tengine
496        8979  5580  0 08:55 ?        00:00:00      /usr/lib64/heartbeat/pengine
root      5581  5564  0 Nov11 ?        00:00:03    /usr/lib64/heartbeat/mgmtd -v

###下面可以查看到了自动生成的cib.xml文件,此文件不含具体资源信息
# cd /var/lib/heartbeat/crm
# ls -hltr
total 16K
-rw------- 2 hacluster haclient 484 Nov 11 16:54 cib.xml.last
-rw------- 2 hacluster haclient 484 Nov 11 16:54 cib.xml
-rw-r--r-- 2 hacluster haclient  32 Nov 11 16:54 cib.xml.sig.last
-rw-r--r-- 2 hacluster haclient  32 Nov 11 16:54 cib.xml.sig

# ifconfig |grep eth0:0
# ssh orasrv2 'ifconfig |grep eth0:0 -A2'  ###此时vip在第二个节点
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:F1:2E:E4 
          inet addr:192.168.21.1  Bcast:192.168.21.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

三、使用crm相关命令管理集群
crm相关命令行工具,通常位于/usr/sbin
所有的命令均可以通过<command> --help 方式来获取帮助

    crm_verify    --校验集群配置文件
    crm_uuid      --查看节点的uuid
    crm_standby  --操作节点的备用属性以确定资源是否可在此节点上运行
    crm_resource  --资源管理工具
    crm_mon      --集群健康状态监控
    crm_master    --管理主/从属资源的自选设置,以在给定节点上提升
    crm_failcount --管理记录每个资源的故障计数的计数器。
    crm_diff      --识别对群集配置所做的更改,并将增补程序应用到配置文件
    crm_attribute --允许查询、修改和删除节点属性和群集选项。
    crmadmin      --控制群集资源管理器
    crm_sh        --crm命令提示符下的工具集
    cibadmin      --提供对群集配置的直接访问

# crm_verify -x /var/lib/heartbeat/crm/cib.xml  ###校验配置文件
# crm_uuid                                      ###查看节点uuid
512505ec-63f0-4a51-be40-58ba0e2f613d
# ssh orasrv2 crm_uuid
6ae0f853-b98c-470b-9ce9-4cd0077179ee

# crm_resource --list                          ###查看资源,当前无任何资源
NO resources configured

# crm_mon -1                                    ###集群状态监控,当前2节点在线
============
Last updated: Wed Nov 11 17:14:18 2015
Current DC: orasrv2.xlk.com (6ae0f853-b98c-470b-9ce9-4cd0077179ee)
2 Nodes configured.
0 Resources configured.
============
Node: orasrv2.xlk.com (6ae0f853-b98c-470b-9ce9-4cd0077179ee): online
Node: orasrv1.xlk.com (512505ec-63f0-4a51-be40-58ba0e2f613d): online

haresources文件资源转到到CIB xml
###使用haresources2cib.py将haresource文件内容转换到CIB xml文件
# /usr/lib64/heartbeat/haresources2cib.py
cib.xml or cib.xml.sig exist in /var/lib/heartbeat/crm/
please remove them before generating new cib.xml
# mkdir /var/lib/heartbeat/crm/old
# mv /var/lib/heartbeat/crm/cib* /var/lib/heartbeat/crm/old
# /usr/lib64/heartbeat/haresources2cib.py
# ls /var/lib/heartbeat/crm/
cib.xml  old

###重启2节点
# service heartbeat restart                ###节点1
# ssh orasrv2 'service heartbeat restart'  ###节点2

# crm_resource --list                      ###查看所有资源,有一组资源
Resource Group: group_1
    IPaddr_192_168_21_1 (ocf::heartbeat:IPaddr)
    httpd_2    (lsb:httpd)

[root@orasrv1 ~]# crm_resource -Q -r IPaddr_192_168_21_1 ###查询指定资源
Resource Group: group_1
    IPaddr_192_168_21_1 (ocf::heartbeat:IPaddr)
    httpd_2    (lsb:httpd)                             

[root@orasrv1 ~]# crm_resource -QW -r IPaddr_192_168_21_1 ###查询资源所在的节点
orasrv1.xlk.com

# crmadmin --version
HA Version 2.1.4, CRM Version 2.0 (CIB feature set 2.0) node: aa909246edb386137b986c5773344b98c6969999

[root@orasrv1 ~]# crmadmin --nodes                  ###查询节点成员名
normal node: orasrv2.xlk.com (6ae0f853-b98c-470b-9ce9-4cd0077179ee)
normal node: orasrv1.xlk.com (512505ec-63f0-4a51-be40-58ba0e2f613d)

# crm_sh
/usr/sbin/crm_sh:31: DeprecationWarning: The popen2 module is deprecated.  Use the subprocess module.
  from popen2 import Popen3
crm # help
Usage: crm (nodes|config|resources)
crm # nodes
crm nodes # help
Usage: nodes (status|list)
crm nodes # list
  <node id="6ae0f853-b98c-470b-9ce9-4cd0077179ee" uname="orasrv2.xlk.com" type="normal"/>
  <node id="512505ec-63f0-4a51-be40-58ba0e2f613d" uname="orasrv1.xlk.com" type="normal"/>

crm nodes # config  --无法切换到config路径下
crm nodes # help
Usage: nodes (status|list)
crm nodes # resources  --同样无法切换到config路径下
crm nodes # help
Usage: nodes (status|list)

###验证httpd服务,从下面的查询可以看出,当前的服务在第2个节点
# curl http://192.168.21.1/
orasrv1.xlk.com

###更多命令的详细描述,请参考:
http://www.novell.com/zh-cn/documentation/sle_ha/book_sleha/data/cha_ha_management.html

四、切换测试
[root@orasrv1 crm]# crm_standby -v true
[root@orasrv1 crm]# curl http://192.168.21.1/
orasrv2.xlk.com

###也可以通过指定节点的方式进行切换
###假定节点2为主,则可以在节点1上执行一下命令将其切换为备
# crm_standby -v true -U orasrv2.xlk.com 

五、小结
1、在heartbeat v2之后的版本,可以将haresources方式管理的资源转化为基于CIB方式进行管理
2、使用CRM(CIB)管理,需要配置ha.cf,添加crm respawn
3、CRM与haresources方式并不兼容,即二者选其一
4、可以使用crm相关命令对集群进行相关配置,监控,切换等管理
5、使用CRM管理方式,也可以基于图形化界面进行管理,本文略

猜你喜欢

转载自www.linuxidc.com/Linux/2015-11/125209.htm