一、实验图:
二、实验环境准备:
1)时间同步(172.16.2.15是时间服务器)
[root@web1 ~]# crontab -e */5 * * * * /usr/sbin/ntpdate 172.16.2.15
[root@web2 ~]# crontab -e */5 * * * * /usr/sbin/ntpdate 172.16.2.15
2)确保web服务器可以基于主机名通信(修改hosts文件)
[root@web1 ~]# vim /etc/hosts 172.16.2.12 web1.linux.com web1 172.16.2.14 web2.linux.com web2
[root@web2 ~]# vim /etc/hosts 172.16.2.12 web1.linux.com web1 172.16.2.14 web2.iinux.com web2
3)确保可以基于ssh秘钥方式登录
[root@web1 ~]# ssh-keygen -P '' [root@web1 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.2.14
[root@web2 ~]# ssh-keygen -P '' [root@web2 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@172.16.2.12
4)测试:
[root@web1 ~]# date; ssh web2 'date'; Mon Jun 29 11:09:12 CST 2015 Mon Jun 29 11:09:12 CST 2015
[root@web2 ~]# date; ssh web1 'date' Mon Jun 29 11:09:42 CST 2015 Mon Jun 29 11:09:42 CST 2015
三、安装配置corosync;pacemaker
安装corosync
[root@web1 ~]# yum -y install corosync
[root@web2 ~]# yum -y install corosync
安装pacemaker
[root@web1 ~]# yum -y install pacemaker
[root@web2 ~]# yum -y install pacemaker
配置corosync
[root@web1 ~]# cd /etc/corosync \\切换到corosync的配置文件目录 [root@web1 corosync]# cp corosync.conf.example corosync.conf \\提供corosync的配置文件 [root@web1 corosync]# vim corosync.conf compatibility: whitetank \\兼容旧版本的corosync totem { \\用来设置监听集群之间心跳信息传递机制 version: 2 \\corosync的版本,默认即可 secauth: on \\启用安全秘钥机制 threads: 0 \\启动多少个线程传递心跳信息 interface { ringnumber: 0 \\循环次数 bindnetaddr: 172.16.2.0 \\绑定的网络地址,是一个网络地址不是具体的ip地址 mcastaddr: 237.225.10.1 \\定义组播地址 mcastport: 5405 \\定义监听端口号 ttl: 1 \\ 信息传递只允许在当前网络中传输 } } logging { fileline: off \\默认即可 to_stderr: no \\是否将错误输出到终端,no为禁止输出到终端,保持默认即可 to_logfile: yes \\记录为日志文件 logfile: /var/log/cluster/corosync.log \\记录日志文件路径 #to_syslog: yes\\是否启用rsyslog日志,这里已经启用corosync自己记录日志功能,所有这里注释掉,不使用 debug: off \\debug日志;只有在调试的时候开启 timestamp: on \\记录日志的时间戳,会有IO操作,根据实际情况开启 logger_subsys { subsys: AMF \\默认即可 debug: off \\默认即可 } } service { \\定义pacemaker以corosync的插件方式运行 ver: 0 \\版本;定义pacemaker的版本 name: pacemaker \\定义名称 } aisexec { \\定义运行的用户和用户组 user: root group: root }
生成秘钥
[root@web1 corosync]# corosync-keygen \\这里需要输入随机数,用于秘钥生成
复制相同的配置文件与秘钥给web2服务器
[root@web1 corosync]# scp -p authkey corosync.conf web2:/etc/corosync
启动corosync
[root@web1 corosync]# service corosync start
[root@web2 ~]# service corosync start
查看启动日志;确保corosync已正常启动
[root@web1 corosync]# grep -e "Corosync Cluster Engine" -e "configuration file" /var//log/cluster/corosync.log Jun 29 11:52:06 corosync [MAIN ] Corosync Cluster Engine ('1.4.7'): started and ready to provide service. Jun 29 11:52:06 corosync [MAIN ] Successfully read main configuration file '/etc/corosync/corosync.conf'.
[root@web1 corosync]# grep pcmk_startup /var//log/cluster/corosync.log Jun 29 11:52:06 corosync [pcmk ] info: pcmk_startup: CRM: Initialized Jun 29 11:52:06 corosync [pcmk ] Logging: Initialized pcmk_startup Jun 29 11:52:06 corosync [pcmk ] info: pcmk_startup: Maximum core file size is: 18446744073709551615 Jun 29 11:52:06 corosync [pcmk ] info: pcmk_startup: Service: 9 Jun 29 11:52:06 corosync [pcmk ] info: pcmk_startup: Local hostname: web1
由于试验是没有stonith设备所以此错误可以忽略 [root@web1 corosync]# grep NOTE /var//log/cluster/corosync.log Jun 29 11:52:29 [1765] web1 pengine: error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity Jun 29 11:52:29 [1765] web1 pengine: error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity Jun 29 11:52:52 [1765] web1 pengine: error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity Jun 29 11:52:53 [1765] web1 pengine: error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity Jun 29 11:52:53 [1765] web1 pengine: error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity
此错误信息可以忽略 [root@web1 corosync]# grep ERROR /var//log/cluster/corosync.log Jun 29 11:52:06 corosync [pcmk ] ERROR: process_ais_conf: You have configured a cluster using the Pacemaker plugin for Corosync. The plugin is not supported in this environment and will be removed very soon. Jun 29 11:52:06 corosync [pcmk ] ERROR: process_ais_conf: Please see Chapter 8 of 'Clusters from Scratch' (http://www.clusterlabs.org/doc) for details on using Pacemaker with CMAN Jun 29 11:52:29 [1765] web1 pengine: notice: process_pe_message: Configuration ERRORs found during PE processing. Please run "crm_verify -L" to identify issues. Jun 29 11:52:29 [1765] web1 pengine: notice: process_pe_message: Configuration ERRORs found during PE processing. Please run "crm_verify -L" to identify issues. Jun 29 11:52:52 [1765] web1 pengine: notice: process_pe_message: Configuration ERRORs found during PE processing. Please run "crm_verify -L" to identify issues. Jun 29 11:52:53 [1765] web1 pengine: notice: process_pe_message: Configuration ERRORs found during PE processing. Please run "crm_verify -L" to identify issues. Jun 29 11:52:53 [1765] web1 pengine: notice: process_pe_message: Configuration ERRORs found during PE processing. Please run "crm_verify -L" to identify issues. Jun 29 11:53:18 [1766] web1 crmd: error: do_log: FSA: Input I_ERROR from crmd_node_update_complete() received in state S_IDLE Jun 29 11:53:18 [1766] web1 crmd: notice: do_state_transition: State transition S_IDLE -> S_RECOVERY [ input=I_ERROR cause=C_FSA_INTERNAL origin=crmd_node_update_complete ] Jun 29 11:53:19 corosync [pcmk ] ERROR: pcmk_wait_dispatch: Child process crmd exited (pid=1766, rc=201)
此信息是提示没有stonith设备,可以忽略 [root@web1 corosync]# crm_verify -L -V error: unpack_resources: Resource start-up disabled since no STONITH resources have been defined error: unpack_resources: Either configure some or disable STONITH with the stonith-enabled option error: unpack_resources: NOTE: Clusters with shared data need STONITH to ensure data integrity Errors found during check: config not valid
查看监听端口
[root@web1 corosync]# ss -upln State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:647 *:* users:(("portreserve",934,6)) UNCONN 0 0 172.16.2.12:5404 *:* users:(("corosync",1756,13)) UNCONN 0 0 172.16.2.12:5405 *:* users:(("corosync",1756,14)) UNCONN 0 0 237.255.10.1:5405 *:* users:(("corosync",1756,10))
安装crmsh
添加yum源: [root@web1 corosync]# vim /etc/yum.repos.d/crmsh.repo [suse_crmsh] name=crmsh baseurl=http://download.opensuse.org/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/ enabled=1 gpgcheck=0 [root@web1 corosync]# yum -y install crmsh
[root@web1 corosync]# scp /etc/yum.repos.d/crmsh.repo web2:/etc/yum.repos.d/ [root@web2 ~]# yum -y install crmsh
四、配置高可用的http
配置文件共享
[root@nfs ~]# vim /etc/exports /www 172.16.2.0/24(rw,no_root_squash) [root@nfs ~]# mkdir /www [root@nfs ~]# echo "NFS" > /www/index.html [root@nfs ~]# service nfs start web1挂载测试: [root@web1 ~]# mount -t nfs 172.16.2.13:/www /mnt [root@web1 ~]# cat /mnt/index.html NFS [root@web1 ~]# umount /mnt web2挂载测试: [root@web2 ~]# mount -t nfs 172.16.2.13:/www /mnt [root@web2 ~]# cat /mnt/index.html NFS [root@web2 ~]# umount /mnt
修改全局属性
[root@web1 ~]# crm crm(live)# configure crm(live)configure# property stonith-enabled=false \\禁用stonith crm(live)configure# verify crm(live)configure# property no-quorum-policy=ignore \\如果是双节点,当没有法定票数时对资源的处理 crm(live)configure# verify crm(live)configure# property default-resource-stickiness=200 \\对资源的粘性 crm(live)configure# verify crm(live)configure# commit
配置vip资源:
crm(live)configure# primitive haweb_vip ocf:heartbeat:IPaddr params ip="172.16.2.10" nic="eth0" cidr_netmask="24" broadcast="172.16.2.10" op monitor interval=10s timeout=20s crm(live)configure# verify crm(live)configure# commit
配置nfs资源:
crm(live)configure# primitive haweb_nfs ocf:heartbeat:Filesystem params device="172.16.2.13:/www" directory="/var/www/html/" fstype="nfs" op monitor timeout=40s interval=20s op start timeout=60s op stop timeout=60s crm(live)configure# verify crm(live)configure# commit
配置http资源:
crm(live)configure# primitive haweb_http lsb:httpd op monitor timeout=15s interval=15s crm(live)configure# verify crm(live)configure# commit
定义顺序约束
crm(live)configure# order haweb_nfs_after_haweb_vip inf: haweb_vip haweb_nfs crm(live)configure# verify crm(live)configure# commit crm(live)configure# order haweb_http_after_haweb_nfs inf: haweb_nfs haweb_http crm(live)configure# verify crm(live)configure# commit
配置为组资源
crm(live)configure# group hawww haweb_vip haweb_nfs haweb_http crm(live)configure# verify crm(live)configure# commit
查看运行状态
crm(live)# status Last updated: Mon Jun 29 14:24:34 2015 Last change: Mon Jun 29 14:23:52 2015 Stack: classic openais (with plugin) Current DC: web2 - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 3 Resources configured Online: [ web1 web2 ] Resource Group: hawww haweb_vip (ocf::heartbeat:IPaddr): Started web1 haweb_nfs (ocf::heartbeat:Filesystem): Started web1 haweb_http (lsb:httpd): Started web1
五、测试访问
切换主机,再次访问:
crm(live)# node standby crm(live)# status Last updated: Mon Jun 29 14:28:00 2015 Last change: Mon Jun 29 14:27:37 2015 Stack: classic openais (with plugin) Current DC: web2 - partition with quorum Version: 1.1.11-97629de 2 Nodes configured, 2 expected votes 3 Resources configured Node web1: standby Online: [ web2 ] Resource Group: hawww haweb_vip (ocf::heartbeat:IPaddr): Started web2 haweb_nfs (ocf::heartbeat:Filesystem): Started web2 haweb_http (lsb:httpd): Started web2
原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/5764