(五)使用ansible部署corosync和pacemaker
5.1、定义corosync配置信息
[root@ansible-server ~]# vim corosync/roles/ha/files/corosync.conf
compatibility: whitetank #是否兼容旧版本的corosync
totem { #定义心跳信息传递信息
version: 2 #定义corosync版本
secauth: on #是否需要安全认证
threads: 0 #启动多少个线程处理心跳信息
interface {
ringnumber: 0 #起始号
bindnetaddr: 192.168.80.0 #绑定在哪个网络地址
mcastaddr: 226.94.1.1 #组播地址,为了与另一个节点传递心跳信息
mcastport: 5405 #组播地址端口号
ttl: 1
}
}
logging { #定义日志功能
fileline: off
to_stderr: no #是否将错误日志输出到终端
to_logfile: yes #是否启用专门的日志文件
to_syslog: no #是否将日志记录到linux默认日志文件中,即/var/log/messages,此项和to_logfile启动一项即可
logfile: /var/log/cluster/corosync.log #日志文件存放位置
debug: off #是否开启debug日志信息
timestamp: on #是否开启日志记录时间戳
logger_subsys {
subsys: AMF
debug: off
}
}
amf {
mode: disabled
}
service{ #设定使用pacemaker服务
ver: 0
name: pacemaker
}
aisexec{ #定义运行时使用的用户和组
user: root
group: root
}
备注:此文件可以在已安装的corosync下/etc/corosync/下有一corosync.conf.example模板信息,做好修改之后再传递给ansible-server即可。
5.2、定义node1和node2之间corosync所需的秘钥信息
[root@ansible-server ~]# ls corosync/roles/ha/files/authkey
corosync/roles/ha/files/authkey
备注:此文件可以在已安装好的corosync上执行corosync-keygen,此时需要你输入数据来产生随机数,建议使用重复安装某个程序来加快生成速度,然后拷贝到ansibe-server即可。
5.3、定义ha的tasks
目标:
安装corosync、pacemaker和httpd
拷贝authkey认证文件和corosync配置文件到各节点
[root@ansible-server ~]# vim corosync/roles/ha/tasks/main.yml
- name: install corosync、pacemaker and httpd
yum: name={{ item }} state=present #安装对应所需的程序包
with_items:
- corosync
- pacemaker
- httpd
tags: inst
- name: auth key file #拷贝认证文件到各节点
copy: src=authkey dest=/etc/corosync/authkey owner=root group=root mode=4600
tags: authkey
- name: configuration file #拷贝配置文件到各节点
copy: src=corosync.conf dest=/etc/corosync/corosync.conf
tags: config
notify: #当配置改变了,通知重启corosync
- restart corosync
- name: start corosync #启动corosync服务,并设置开机不自动启动
service: name=corosync state=started enabled=no
tags: start
- name: start httpd #启动httpd服务,并设定开机不自动启动
service: name=httpd state=started enabled=no
tags: start
5.4、定义ha的handlers文件
[root@ansible-server ~]# vim corosync/roles/ha/handlers/main.yml
- name: restart corosynce
service: name=corosynce state=restart
5.5、定义YAML文件
[root@ansible-server ~]# vim corosync/ha.yml
- name: install and config corosync
remote_user: root
hosts: hbhosts
roles:
- common
- ha
- name: install nfs
remote_user: root
hosts: nfs-Server
roles:
- nfs
5.6、执行ansible-play自动部署corosync和pacemaker设置
[root@ansible-server ~]# ansible-playbook corosync/ha.yml
(六)使用ansible安装crmsh工具
所需程序包:
[root@ansible-server crmsh]# ll files/
-rw-r--r-- 1 root root 495332 4月 27 23:44 crmsh-1.2.6-4.el6.x86_64.rpm
-rw-r--r-- 1 root root 49960 4月 27 23:44 pssh-2.3.1-2.el6.x86_64.rpm
6.1、使用ansible安装crmsh
- name: copy crmsh and pssh #拷贝程序包到各节点
copy: src={{ item }} dest=/tmp/
with_items:
- crmsh-1.2.6-4.el6.x86_64.rpm
- pssh-2.3.1-2.el6.x86_64.rpm
- name: install crmsh and pssh #安装两个程序包
yum: name={{ item }} state=present
with_items:
- /tmp/pssh-2.3.1-2.el6.x86_64.rpm
- /tmp/crmsh-1.2.6-4.el6.x86_64.rpm
6.2、定义YAML文件
[root@ansible-server ~]# vim corosync/ha.yml
- name: install and config corosync
remote_user: root
hosts: hbhosts
roles:
- common
- ha
- crmsh
- name: install nfs
remote_user: root
hosts: nfs-Server
roles:
- nfs
6.3、执行ansible-play安装crmsh
[root@ansible-server ~]# ansible-playbook corosync/ha.yml
(七)使用crmsh配置http高可用
7.1、准备工作
[root@node1 ~]# crm
crm(live)# configure
crm(live)configure# property stonith-enabled=false #默认情况下,如果没有stonith设备,会不允许启用,所以我们要设置为安全忽略
crm(live)configure# property no-quorum-policy=ignore #因为我们只有2个节点,当我们其中一个节点下线了,那么其将无法定票数达不到一半以上,所有如果只有两个节点,必须将其使用安全忽略,否则节点将无法转移
crm(live)configure# verify #校验是配置否存在问题
crm(live)configure# commit #如无问题的话,提交所修改的配置
7.2、定义资源
包括webip,webserver,webstore
crm(live)configure# primitive webip ocf:IPaddr params ip=192.168.80.200 op monitor interval="30s" timeout="20s"
crm(live)configure# primitive webserver lsb:httpd op monitor interval="30s" timeout="20s"
crm(live)configure# primitive webstore ocf:Filesystem params device="192.168.80.188:/web/htdocs" directory="/var/www/html" fstype="nfs" op monitor interval="60s" timeout="40s" op start timeout="60s" interval="0" op stop timeout="60s" interval="0"
crm(live)configure# verify
7.3、定义组和顺序约束
crm(live)configure# group webservice webip webstore webserver
crm(live)configure# order webip_before_webstore_before_webserver inf: webip webstore webserver
crm(live)configure# verify
crm(live)configure# commit
7.4、检查节点和资源是否正常
crm(live)# status
Last updated: Fri Apr 29 05:46:15 2016
Last change: Thu Aug 13 17:23:52 2015 via cibadmin on node1.windchaser.com
Stack: classic openais (with plugin)
Current DC: node2.windchaser.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
3 Resources configured
Online: [ node1.windchaser.com node2.windchaser.com ]
Resource Group: webservice
webip (ocf::heartbeat:IPaddr): Started node1.windchaser.com
webstore (ocf::heartbeat:Filesystem): Started node1.windchaser.com
webserver (lsb:httpd): Started node1.windchaser.com
(八)验证
1)、使用客户端访问webip,可以正常查看到对应的网址

2)、将node1下线
[root@node1 ~]# crm node standby
3)、再次查看节点以及资源状态
[root@node1 ~]# crm status
Online: [ node2.windchaser.com ]
Resource Group: webservice
webip (ocf::heartbeat:IPaddr): Started node2.windchaser.com
webstore (ocf::heartbeat:Filesystem): Started node2.windchaser.com
webserver (lsb:httpd): Started node2.windchaser.com
发现资源已转移至node2,重新使用客户端访问webip,发现可正常使用
4)、将node1节点重新上线,此时可正常使用。
[root@node1 ~]# crm node online
(九)需要注意的地方
node1和node2的时间必须同步
node1和node2必须可以正常解析对方的主机名和IP地址
Ansible :一个配置管理和IT自动化工具 http://www.linuxidc.com/Linux/2014-11/109365.htm
Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-04/130844.htm
