使用corosync + pacemaker + crmsh实现nginx的高可用集群

实验环境:
两个节点:server2 server3
server2:172.25.65.2
server3:172.25.65.3


配置yum源:

添加如下几个包
[HighAvailability]
name=HighAvailability
baseurl=http://172.25.65.250/rhel6.5/HighAvailability
gpgcheck=0

[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.65.250/rhel6.5/LoadBalancer
gpgcheck=0

[ResilientStorage]
name=ResilientStorage
baseurl=http://172.25.65.250/rhel6.5/ResilientStorage
gpgcheck=0

[ScalableFileSystem]
name=ScalableFileSystem
baseurl=http://172.25.65.250/rhel6.5/ScalableFileSystem
gpgcheck=0

在两个节点上都安装:corosync 和 pacemaker

yum install corosync pacemaker -y

编辑配置文件

mv /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf
vim /etc/corosync/corosync.conf
cat /etc/corosync/corosync.conf
# Please read the corosync.conf.5 manual page
compatibility: whitetank

totem {
    version: 2
    secauth: on       #开启安全认证 
    threads: 0        #线程数 0 表示不基于线程模式工作而是进程
    interface {
        ringnumber: 0 #环数目   一般保持为0
        bindnetaddr: 172.25.67.0   #网络地址     更改为主机所在网络的网络地址
        mcastaddr: 226.94.1.1  #多播地址ip addr 可查看
        mcastport: 5405   #多播地址监听端口
        ttl: 1
    }
}

logging {
    fileline: off
    to_stderr: no
    to_logfile: yes   #是否记录日志 
    to_syslog: yes
    logfile: /var/log/cluster/corosync.log
    debug: off
    timestamp: on
    logger_subsys {
        subsys: AMF
        debug: off
    }
}


amf {
    mode: disabled
}
###添加如下行 使pacemaker 以corosync插件方式运行
service {
  ver:  0
  name: pacemaker
  use_mgmtd: yes
   }

配置文件

生成corosync的密钥文件
corosync-keygen是从/dev/random中读取随机数,如果此熵池中随机数过少,可能导致无法生成密钥,所以打开图形化界面敲键盘随即录入。

corosync-keygen  -l
将corosync.conf 和authkey复制到server2中 
scp -p authkey corosync.conf root@172.25.67.2:/etc/corosync/

安装crmsh

yum  localinstall crmsh-1.2.6-0.rc2.2.1.x86_64.rpm pssh-2.3.1-2.1.x86_64.rpm 

RHEL自6.4起不再提供集群的命令行配置工具crmsh,转而使用pcs;所以如果想使用crmsh可以自行安装

在两个节点都启动corosync服务

service corosync start

这里写图片描述
这里写图片描述

使用crmsh命令查看集群节点的启动状态
将no-quorum-policy 设置为ignore ,在两个节点中,当节点达不到法定票数时,即两个节点一个坏了,没法投票,正常的节点达不到法定票数,此时如果是默认参数,即正常的机器不能工作,所以需要该为ignore,使正常机器接管
corosync默认启用了stonith,而当前集群并没有相应的stonith设备,因此此默认配置目前尚不可用

crm
crm(live)# configure
crm(live)configure#  property no-quorum-policy=ignore  #将no-quorum-policy 设置为ignore 
crm configure property stonith-enabled=false    
crm(live)configure# show#使用crmsh命令查看当前的配置信息

查看当前的配置信息

verify
crm(live)configure# commit  #保存提交

将nginx加入系统服务

vim /etc/init.d/nginx

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 

esac  

chmod 755 /etc/init.d/nginx
chkconfig --add nginx

测试是否添加成功

service nginx stop
service nginx start

测试是否添加成功

配置crmsh

crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=172.25.65.100 op monitor interval=30s timeout=20s on-fail=restart 
crm(live)configure# primitive nginx_res lsb:nginx 
crm(live)configure# colocation nginx_web inf: nginx_res webip #定义排列约束
crm(live)configure# order nginx_after_ip mandatory: webip nginx_res #定义资源启动顺序
crm(live)configure# verify #审核
crm(live)configure# commit #提交
crm(live)configure# show 

配置crmsh

监控集群状态

crm_mon

监控集群状态

建立测试文件


在servevr2中:
vim /usr/local/nginx/html/index.html
在最后一行添加:
<h1>22222222222222222222</h1>

在server3中:
vim /usr/local/nginx/html/index.html
在最后一行添加:
<h1>33333333333333333333</h1>

建立测试文件

测试
访问172.25.65.100
此时请求在server2上
访问结果如下
这里写图片描述
在server2中执行crm node standby命令,将server2节点停用
再次访问172.25.65.100
此时请求将转到server3上
这里写图片描述
在server3上执行crm node standby命令,将server3节点停用
在server2上执行crm node online 命令,将server2节点激活
访问172.25.65.100
请求将转到server2上
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值