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
下一篇 2015-06-30

相关推荐

  • 软链接与硬链接

    软链接 硬链接

    2017-11-18
  • Linux 用户, 组和权限

    用户, 组和权限 Linux登陆需要用户名、密码。/etc/passwd 文件保存用户名。登录linux时,Linux 先查找 /etc/passwd 文件中是否有这个用户名,没有则跳出,有则读取用户名的user ID 、 group ID 、用户名对应的根目录路径以及所使用的 shell ,最后在 /etc/shadow 中核对该 UI…

    2017-07-22
  • Linux网络管理基础

    Linux网络管理基础 动态路由 Bonding Network Teaming 静态路由实验 Linux的网络管理,了解基本的网络知识是基础,除此,要掌握好ifconig命令、ip命令、nmcli命令(CentOS 7),以及涉及到网络的配置文件。 配置动态路由: 通过守护进程获取动态路由,安装quagga包,支持RIP、OSPF、BGP,通过命令vtys…

    Linux干货 2016-09-09
  • 简述计算机硬件组成,linux系统及简单命令详解

    1、计算机的硬件组成 (1)cpu(中央处理器):主要由运算器和控制器组成,它的运算器主要负责程序运算与逻辑判断,控制器则主要协调组件与各个设备之间工作。 (2)存储设备:存储设备又分为外存储设备,是用来存储数据信息的设备,它包括,硬盘,u盘,光盘,软盘。而其中内存(ram)和主板上的rom是属于内存储设备,内存是只读存储器,它用来暂时记录数据信息供给cpu…

    2017-07-09
  • nginx配置(二)

    ngx_http_gzip_module: The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half…

    Linux干货 2017-05-08
  • tar,cpio,rpm,yum的使用详解(适合初学者)

    基于linux下的tar包管理和yum源配置 友情提示:对于这些实际操作的,还是需要进行手工的尝试,由于这几个对于包管理的是比较常用和实用的, 所以,一定要熟练使用。 1.tar tar是对于系统中文件,目录等进行打包,解包的工具。tar也可以对文件进行打包存放到别的目录下, 所以在很早的时候,一些老的工程师经常使用tar对文件进行备份。很大程度上,所谓的t…

    Linux干货 2016-08-21