corosync + pacemaker搭建高可用http

一、实验图:

  2015-06-29_102551.png

二、实验环境准备:

 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

五、测试访问

2.png

 切换主机,再次访问:

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

2.png

原创文章,作者:马行空,如若转载,请注明出处:http://www.178linux.com/5764

(0)
马行空马行空
上一篇 2015-06-30 13:11
下一篇 2015-06-30 16:58

相关推荐

  • Linux软件包安装—-卷一软件包管理rpm方式安装程序

    rpm方式手动安装程序,可能需要”人工智能”的方式手动去处理程序依赖问题

    Linux干货 2017-12-02
  • 笔记–8.2–用户管理,权限管理

    ·硬链接,软链接区别 大小 链接数 设备,分区 类型 硬连接 一样大,显示的是本身的大小 会增加 不能跨分区 是一个文件,相同inode号,忘了位置可通过inode号找到源文件 软链接 不一样大,链接文件大小是路径的大小 不会增加 可以跨分区,跨设备 两个文件,不同inode号,像发小广告,忘了源连接,文件就找不到了 软链接格式:ln -s +绝对路径或相对…

    Linux干货 2016-08-05
  • linux密码的破解与保护

    找回root口令:   遇到密码丢失的情况,如果只是普通用户的密码丢失,那么可以让管理员重新设定密码。但是如果管理员用户的密码丢失,那该如何解决?   在 Linux环境中root 密码忘记还是可以救回来的!只要能够进入并且挂载 / , 然后重新设定一下root的密码,就救回来啦!这是因为开机流程中,若强制进入runlevel 1 时, …

    Linux干货 2016-09-22
  • N25期第二周作业

    1.Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示 ls 列出文件和目录命令 -a:显示所有档案及目录 -A:显示除隐藏文件”.”和”..”以外的所有文件 -C:多列显示结果,默认选项 -l:单列显示结果,以长格式显示目录下的内容列表 -F:在每个输出项后追加文件的类型标识符 &#822…

    Linux干货 2016-12-11
  • 位置变量在Shell脚本编程中的作用

    位置变量:         如果要向一个shell脚本传递信息,可以使用位置参数完成此功能;参数相关数目传入脚本,此数目可以任意多,但只有前9个可以被访问,使用shift命令可以改变这个限制;参数从第一个开始,在第九个结束;每个访问参数前要加$符号;第一个参数为0,表示预留保存实…

    Linux干货 2016-08-15
  • 马哥教育网络班21期+第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。 方法1: [root@zabbix ~]# who |awk '{print $1}'|sort -n|uniq monitor root 方法2: [root@zabbix ~]#…

    Linux干货 2016-07-22