注: LVS + Keepalived 不知道为什么出现一个很郁闷的问题.......
-------------------------------------------------------------------------
原来的拓扑是:
LVS 主 + WEB1
LVS 从 + WEB2
IP 是3个外网IP WEB1 123.123.123.121 WEB2 123.123.123.122
VIP 123.123.123.123
直接访问 WEB1 123.123.123.121 正常
直接访问 WEB2 123.123.123.122 正常
访问VIP 不行 VIP 可以ping通····
关闭 WEB1 服务 VIP 可以了.... 郁闷....是否VIP 80 与 WEB1 80端口冲突?
--------------------------------------------------------------------------
转入正题.......
拓扑
WEB1 + Keepalived 主 IP 123.123.123.121
WEB2 + Keepalived 从 IP 123.123.123.122
VIP 123.123.123.123
两台机都安装 Keepalived
下载 wget http://www.keepalived.org/software/keepalived-1.2.1.tar.gz
tar zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure
make
make install
将keepalived做成启动脚务:
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
【service keepalived start 可启动,这里暂时不启动,等修改完配置文件以后再启动】
Keealived的配置文件修改..
vi /etc/keepalived/keepalived.conf
【主 的Keepalived 的配置文件设置】
--------------------------------------------------------------------------------
vrrp_script chk_http_port {
script "/opt/local/nginx/nginx_pid.sh" 监控脚本
interval 2 监控时间
weight 2
}
vrrp_instance VI_1 {
state MASTER 主
interface eth0 监控网卡
virtual_router_id 51 这个两台服务器必须一样
priority 101 权重值 高于 从
authentication {
auth_type PASS 加密
auth_pass 1111 与从想同
}
track_script {
chk_http_port 执行监控的服务
}
virtual_ipaddress {
123.123.123.123 VIP
}
}
------------------------------------------------------------------------
【从 的Keepalived 的配置文件设置】
--------------------------------------------------------------------------------
vrrp_script chk_http_port {
script "/opt/local/nginx/nginx_pid.sh" 监控脚本
interval 2 监控时间
weight 2
}
vrrp_instance VI_1 {
state BACKUP 从
interface eth0 监控网卡
virtual_router_id 51 这个两台服务器必须一样
priority 100 权重值 低于 主
authentication {
auth_type PASS 加密
auth_pass 1111 与从想同
}
track_script {
chk_http_port 执行监控的服务
}
virtual_ipaddress {
123.123.123.123 VIP
}
}
------------------------------------------------------------------------
【编写Nginx 的监控脚本】
----------------------------------------------------------------------
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/opt/local/nginx/sbin/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
------------------------------------------------------------------------
WEB1 WEB2 分别启动...Nginx 与 Keepalived 测试一下...
# ip add list 查看 eth0 是否绑定 vip
关闭 主 Keepalived 查看是否 从 会 接管 VIP