corosync+pacemaker配置高可用集群(需要额外安装crm工具)

借鉴了网上不少博客

一、前期准备:准备两台主机node1和node2:

1.确保设置好IP地址,设置好hostname和修改/etc/sysconfig/network内hostname一致。同步时间:#ntpdate ntp.api.bz
2.node1和node2先做好互信。
node1:

  • #ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
    #cd ~/.ssh/
    #ssh-copy-id -i id_rsa.pub root@NODE2IP

node2:

  • #ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
    #cd ~/.ssh/
    #ssh-copy-id -i id_rsa.pub root@NODE1IP

测试一下:ssh NODE1IP 'ifconfig'
3.node1和node2:

  • #vim /etc/hosts
    加入:
  • #NODE1IP node1
    #NODE2IP node2

node2上测试:ping node1
4.关闭iptables和selinex
5.安装第三方yum源头:#yum install epel-release -y
6.安装好测试用的httpd:#yum install httpd -y
7.node1:

  • echo node1 > /var/www/html/index.html

node2:

  • echo node2 > /var/www/html/index.html

停止httpd服务,并关闭开机自启动。

二、开始安装

node1node2均要安装:
1.安装corosync和pacemaker。
2.heartbeat到了V3,已经吧crm单独划分出去,所以不需要再安装heartbeat。直接安装crmsh安装包。
###################################################################
vim /etc/yum.repos.d/HA.repo
##添加安装crmsh用的yum源。但是下载速度很慢,很大概率失败。
###################################################################
[network_ha-clustering_Stable]
name=Stable High Availability/Clustering packages (CentOS_CentOS-6)
type=rpm-md
baseurl=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/
gpgcheck=1
gpgkey=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6//repodata/repomd.xml.key
enabled=1
###################################################################
PS:如果不能安装,那么复制路径直接到页面上去下载缺少的安装包。
例如:yum install crmsh-3.0.0-6.1.noarch.rpm crmsh-scripts-3.0.0-6.1.noarch.rpm python-parallax-1.0.1-28.1.noarch.rpm
###################################################################

三、开始配置

node1:(只需要在一个节点上配置即可)
1.配置corosync

  • #cd /etc/corosync
    #cp corosync.conf.example corosync.conf
    #vim corosync.conf

修改

  • secath:on #指的是集群间认证开启,防止其他主机加入集群
    thrends:2 #并发开启的线程数。一般单核cpu修改下。多核cpu不需要修改
    bindnetaddr:192.168.126.0 #集群工作的网段
    mcastaddr:226.99.9.17 #多播地址,可查一下什么可选,尽量与别人的不一样
    to-syslog:no #关闭写入系统日志
    添加启动pacemaker
    service { ##启动corosync完成后,就启动pacemaker。
    ver:0
    name:pacemaker
    } #保存

2.准备密钥文件

  • #corosync-keygen

3.复制corosync.conf和密钥文件到node2:

  • #scp -p corosync.conf authkey node2:/etc/corosync/

四、启动服务

node1:
1.启动服务

  • #service corosync start

2.对服务进行检查,查看是否有问题。
3.查看流程大致

  • 查看corosync引擎是否正常启动
    查看初始化成员节点通知是否正常发出
    检查启动过程中是否有错误产生
    crm_mon查看状态

例如:
查看corosync引擎是否正常启动:

  • #grep -e "Corosync Cluster Engine" -e "configuration file" /var/log/messages
    Jun 14 19:02:08 node1 corosync[5103]: [MAIN ] Corosync Cluster Engine ('1.2.7'): started and ready to provide service.
    Jun 14 19:02:08 node1 corosync[5103]: [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'.
    Jun 14 19:02:08 node1 corosync[5103]: [MAIN ] Corosync Cluster Engine exiting with status 8 at main.c:1397.
    Jun 14 19:03:49 node1 corosync[5120]: [MAIN ] Corosync Cluster Engine ('1.2.7'): started and ready to provide service.
    Jun 14 19:03:49 node1 corosync[5120]: [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'.

查看初始化成员节点通知是否正常发出:

  • #grep TOTEM /var/log/messages
    Jun 14 19:03:49 node1 corosync[5120]: [TOTEM ] Initializing transport (UDP/IP).
    Jun 14 19:03:49 node1 corosync[5120]: [TOTEM ] Initializing transmit/receive security: libtomcrypt SOBER128/SHA1HMAC (mode 0).
    Jun 14 19:03:50 node1 corosync[5120]: [TOTEM ] The network interface [172.16.100.11] is now up.
    Jun 14 19:03:50 node1 corosync[5120]: [TOTEM ] A processor joined or left the membership and a new membership was formed.

检查启动过程中是否有错误产生:

  • #grep ERROR: /var/log/messages | grep -v unpack_resources

查看pacemaker是否正常启动:

  • #grep pcmk_startup /var/log/messages
    Jun 14 19:03:50 node1 corosync[5120]: [pcmk ] info: pcmk_startup: CRM: Initialized
    Jun 14 19:03:50 node1 corosync[5120]: [pcmk ] Logging: Initialized pcmk_startup
    Jun 14 19:03:50 node1 corosync[5120]: [pcmk ] info: pcmk_startup: Maximum core file size is: 4294967295
    Jun 14 19:03:50 node1 corosync[5120]: [pcmk ] info: pcmk_startup: Service: 9
    Jun 14 19:03:50 node1 corosync[5120]: [pcmk ] info: pcmk_startup: Local hostname: node1.magedu.com

4.均无问题后在node1上远程启动node2

  • #ssh node2 'service corosync start'

注意:启动node2需要在node1上使用如上命令进行,不要在node2节点上直接启动

五、配置集群

node1:(在一个节点上配置即可,会自动同步到node2)
1.crm有两种模式命令行模式(立即生效)和交互式模式(提交生效)。
2.我们使用虚拟机没有stonith设备,先关闭stonith。
#crm configure property stonith-enabled=false
3.开始配置webip资源

  • #crm

直接进入交互模式(verify检查,commit提交,show显示配置,edit直接手动修改配置文件)

  • #crm#resource
    #crm#resource#primitive webip ocf:heartbeat:IPaddr params ip=192.168.126.214 nic=192.168.126.254 nic=eth0 cidr_netmask=24
    #verify #检查
    #commit #提交
    #show xml #以xml格式显示配置条目
    #status #检查状态

(PS:启动关闭资源的方法:#crm#resource#start webip
#crm#resource#stop webip)
4.配置httpd资源

  • #crm#configure#primitive httpd lsb:httpd

5.查看配置的资源

  • #crm#configure#show

6.检查提交

  • #verify
    #commit
    (PS:有错误的时候可以键入edit直接进行文本界面来修改配置的资源)

node2:

  • #crm status或者#crm_mon来查看配置是否成功启动。

node1:

  • 变成备节点:
    #crm node standby
    变回上线状态:
    #crm node online

以上变更时,对node2上面的crm_mon进行观察,会发现有变化。

六、配置资源的约束

1.排列约束:(使两个资源在同一个节点运行)

  • #crm#configure#colocation httpd_with_webip INFINITY: httpd webip
    #crm#configure#show xml
    #crm#configure#verify
    #crm#configure#commit

2.顺序约束:(决定资源启动的先后次序。先启动webip后启动httpd)

  • #crm#configure#order webip_before_httpd mandatory: webip httpd
    #crm#configure#show xml
    #crm#configure#verify
    #crm#configure#commit

3.位置约束:(资源更倾向于某个节点)

  • #crm#configure#location webip_on_node1 webip 100: #uname eq node1

提交。

七、开始测试

客户机浏览器登录192.168.126.214
node1:

  • 变备件点
    #crm node standby
    刷新浏览器
    变回上线状态
    #crm node online
    查看资源是否都回到node1

猜你喜欢

转载自blog.51cto.com/13484913/2125623