nginx+keepalived实现双机热备高可用
2015-06-08 00:00
633 查看
转载自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已经指向了从服务器。
安装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已经指向了从服务器。
相关文章推荐
- 网易云音乐8亿用户背后的伤疤
- windows server域用户提升到本地更高权限组中的方法
- 管理 Linux 系统中的用户
- 服务器托管 火爆机遇减轻建站负担
- 个人用户防黑指南
- SQL Server提示"选定的用户拥有对象,所以无法除去该用户”
- PowerShell查看进程的所属用户
- C#创建windows系统用户的方法
- MongoDB系列教程(四):设置用户访问权限
- 用户的详细注册和判断
- Oracle 添加用户并赋权,修改密码,解锁,删除用户的方法
- SQL Server"错误 21002: [SQL-DMO]用户 * 已经存在问题解决
- mysql误删root用户恢复方法
- php+mysql实现用户注册登陆的方法
- oracle创建删除用户示例分享(oracle删除用户命令及授权)
- php后台如何避免用户直接进入方法实例
- Windwos服务器远程桌面限制用户使用同一个会话的3种设置方法
- MySQL错误ERROR 2002 (HY000): Can't connect to local MySQL server through socket
- 使SQL用户只能看到自己拥有权限的库(图文教程)
- oracle用户权限、角色管理详解