nginx+keepalived实现双机热备高可用

转载自www.fwqtg.net服务器托管网

安装nginx(两台安装配置都一样!)
#添加运行nginx的用户和组www

groupaddwww
useradd-gwwwwww
#安装依赖
yuminstallpcrepcre-devel
#开始安装nginx
wgethttp://sysoev.ru/nginx/nginx-0.7.51.tar.gz
tarzxvfnginx-0.7.51.tar.gz
cdnginx-0.7.51/
./configure�user=www�group=www�prefix=/usr/local/webserver/nginx�with-http_stub_status_module�with-http_ssl_module
make&&makeinstall
#keepalived安装
yuminstallopenssl-devel
cd/tmp
wgethttp://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tarxzfkeepalived-1.2.2.tar.gz
cdkeepalived-1.2.2
./configure
make&&makeinstall
cp/usr/local/etc/rc.d/init.d/keepalived/etc/init.d/
cp/usr/local/etc/sysconfig/keepalived/etc/sysconfig/
chmod+x/etc/init.d/keepalived
chkconfig�addkeepalived
chkconfigkeepalivedon
mkdir/etc/keepalived
ln-s/usr/local/sbin/keepalived/usr/sbin/
keepalived的配置
更详细的keepalived配置文件说明可以执行mankeepalived.conf查看。
我们假设主服务器IP:192.168.1.103,从服务器ip:192.168.1.101虚拟ip:192.168.1.110
下面对主服务器的keepalived进行配置:

vi/etc/keepalived/keepalived.conf
global_defs{
notification_email{
admin@centos.bz
}
notification_email_fromkeepalived@domain.com
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_scriptchk_http_port{
script“/opt/nginx_pid.sh”
interval2
weight2
}
vrrp_instanceVI_1{
stateMASTER############辅机为BACKUP
interfaceeth0
virtual_router_id51
mcast_src_ip192.168.1.103
priority102###########权值要比back高
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port###执行监控的服务
}
virtual_ipaddress{
192.168.1.110
}
}

从服务器:

global_defs{
notification_email{
admin@centos.bz
}
notification_email_fromkeepalived@domain.com
smtp_server127.0.0.1
smtp_connect_timeout30
router_idLVS_DEVEL
}
vrrp_scriptchk_http_port{
script“/opt/nginx_pid.sh”
interval2
weight2
}
vrrp_instanceVI_1{
stateBACKUP
interfaceeth0
virtual_router_id51
mcast_src_ip192.168.1.101
priority101##########权值要比master低。。
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
chk_http_port###执行监控的服务
}
virtual_ipaddress{
192.168.1.110
}
}
之后分别在主从服务器建立nginx的监控脚本:

vi/opt/nginx_pid.sh
#!/bin/bash
A=`ps-Cnginx�no-header|wc-l`
if[$A-eq0];then
/usr/local/nginx/sbin/nginx
sleep3
if[`ps-Cnginx--no-header|wc-l`-eq0];then
killallkeepalived
fi
fi
然后分别启动主从服务器的keepalived:

servicekeepalivedstart
keepalived的测试
我们在主服务器上执行命令ipa,显示如下:

2:eth0:mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:aa:a1:e4brdff:ff:ff:ff:ff:ff
inet192.168.1.103/24brd255.255.255.255scopeglobaleth0
inet192.168.1.110/32scopeglobaleth0
证明主服务器已经绑定了虚拟ip192.168.1.110
在从服务器上执行命令ipa,显示如下:

2:eth0:mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:2b:94:3bbrdff:ff:ff:ff:ff:ff
inet192.168.1.101/24brd255.255.255.255scopeglobaleth0
显示表明从服务器上没有绑定vip192.168.1.110,只有本机真实ip192.168.1.101
下面我们停止主服务器的nginx进程,再看看ip绑定情况:
主服务器的情况:

2:eth0:mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:aa:a1:e4brdff:ff:ff:ff:ff:ff
inet192.168.1.103/24brd255.255.255.255scopeglobaleth0
从服务器的情况:

2:eth0:mtu1500qdiscpfifo_fastqlen1000
link/ether00:0c:29:2b:94:3bbrdff:ff:ff:ff:ff:ff
inet192.168.1.101/24brd255.255.255.255scopeglobaleth0
inet192.168.1.110/32scopeglobaleth0
由此可见vip已经指向了从服务器。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

4. Nginx模块

Nginx官方模块1.ngx\_http\_stub\_status\_modulehttp://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html。(https://www.oschina.net/action/GoToLink?urlhttp%3A%2