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

Wesley13
• 阅读 499

转载自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已经指向了从服务器。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
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
Wesley13 Wesley13
2年前
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
Wesley13 Wesley13
2年前
P2P技术揭秘.P2P网络技术原理与典型系统开发
Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这