安装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已经指向了从服务器。