LVS原理请看:http://ixdba.blog.51cto.com/2895551/552947
LVS-MASTER 192.168.100.113
LVS-BACKUP 192.168.100.116
REALSERVER1 192.168.100.114
REALSERVER2 192.168.100.115
VIP 192.168.100.125
一. 在LVS-MASTER跟LVS-BACKUP安装keepalived跟ipvsadm,我选择yum安装
[[email protected] ~]# yum install -y ipvsadm keepalived
安装完成后配置keepalived
LVS-MASTER配置:
[[email protected] ~]# cd /etc/keepalived/
[[email protected] keepalived]# > keepalived.conf
[[email protected] keepalived]# vim keepalived.conf
将此段代码复制进去
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.80.1
smtp_connection_timeout 30
router_id LVS_DEVEL # 设置lvs的id,在一个网络内应该是唯一的
}
vrrp_instance VI_1 {
state MASTER #指定Keepalived的角色,MASTER为主,BACKUP为备
interface eth0 #设置对外服务的接口
virtual_router_id 51 #虚拟路由编号,主备要一致
priority 100 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR
advert_int 1 #检查间隔,默认为1s
authentication { #设置验证类型和密码,master和buckup一定要设置一样
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.100.125 #定义虚拟IP(VIP)为192.168.100.125,可多设,每行一个
}
}
# 定义对外提供服务的LVS的VIP以及port
virtual_server 192.168.100.125 80 {
delay_loop 6 # 设置健康检查时间,单位是秒
lb_algo wrr # 设置负载调度的算法为wlc
lb_kind DR # 设置LVS实现负载的机制,有NAT、TUN、DR三个模式
nat_mask 255.255.255.0
persistence_timeout 0 #会话保持时间,单位秒
protocol TCP
real_server 192.168.100.114 80 { # 指定real server1的IP地址
weight 3 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.100.115 80 { # 指定real server2的IP地址
weight 3 # 配置节点权值,数字越大权重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动keepalived 服务
[[email protected] ~]# service keepalived start
正在启动 keepalived: [确定]
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# chkconfig --list | grep keepalived
keepalived 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
LVS-BACKUP配置:
只需要将LVS-MASTER配置修改两处即可
state BACKUP #指定Keepalived的角色,MASTER为主,BACKUP为备
priority 90 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR
REALSERVER1跟REALSERVER2 配置
[[email protected] ~]# vi /etc/rc.d/init.d/realserver 把以下脚本贴到realserver里去
#!/bin/bash
#cription: config realserver lo and apply noarp
WEB_VIP=192.168.100.125 #虚拟vip,也就是虚拟ip
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP
/sbin/route add -host $WEB_VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/default/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/default/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $WEB_VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "0" > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo "0" > /proc/sys/net/ipv4/conf/default/arp_ignore
echo "0" > /proc/sys/net/ipv4/conf/default/arp_announce
echo "RealServer Stoped"
;;
status)
# Status of LVS-DR real server.
islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP`
isrothere=`netstat -rn | grep "lo:0" | grep $WEB_VIP`
if [ ! "$islothere" -o ! "isrothere" ];then
# Either the route or the lo:0 device
# not found.
echo "LVS-DR real server Stopped."
else
echo "LVS-DR Running."
fi
;;
*)
# Invalid entry.
echo "$0: Usage: $0 {start|status|stop}"
exit 1
;;
esac
exit 0
开启服务
[[email protected] ~]# service realserver start
RealServer Start OK
测试。。。。。。。。。。。。。。。。。。。。。。。。。
关闭192.168.100.115 REALSERVER2 的nginx 服务器
LVS-MASTER上 查看日志
[[email protected] ~]# tail -f /var/log/messages
然后再开启服务,看看日志
模拟LVS-MASTER 宕机 在LVS-BACKUP 查看日志
关闭keepalived 服务
然后开启服务