1、环境介绍:
系统:centos 6.7
keepalived VIP1 :10.0.5.155
keepalived VIP2:10.0.5.156
Lvs:节点
node-5-150: 10.0.5.150
node-5-152: 10.0.5.152
Real-Server节点:
node-5-153: 10.0.5.153
node-5-154: 10.0.5.154
2、lvs+keepalived软件部署
#安装epel源
yum install -y epel-release
#安装keepalived ipvsadm
yum install -y keepalived ipvsadm
#查看安装信息
yum info keepalived ipvsadm
#查看是否安装完成以及显示文件路径
rpm -ql keepalived ipvsadm
3、keepalived-lvs配置文件
Lvs:节点
node-5-150: 10.0.5.150
VIP主:10.0.5.155
VIP备:10.0.5.156
[root@node-5-150 keepalived]#cat /etc/keepalived/keepalived.conf
#全局定义部分global_defs {notification_email { #设置报警邮件地址,可以设置多个acassen@firewall.loc #每行一个。注意,如果要开启邮件报警failover@firewall.loc #需要开启本机的 Sendmail 服务sysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.loc #设置邮件的发送地址smtp_server 127.0.0.1 #设置smtp server地址smtp_connect_timeout 30 #设置连接 smtp server 的超时时间router_id node_05 #表示运行 Keepalived服务器的一个标识。发邮件时显示在邮件主题中的信息}#vrrp 实例定义部分vrrp_instance VI_1 {state MASTER #指定 Keepalived 的角色,MASTER表示此主机是主服务器,BACKUP表示此主机是备用服务器.interface eth0 #指定监测网络的接口virtual_router_id 51 #虚拟路由标识,这个标识是一个数字,同一个 vrrp 实例使用唯一的标识,即同一个vrrp_instance下,MASTER和BACKUP 必须是一致的priority 100 #定义优先级,数字越大,优先级越高。在一个vrrp_instance下,MASTER的优先级必须大于BACKUP的优先级advert_int 1 #设定MASTER与BACKUP 负载均衡器之间同步检查的时间间隔,单位是秒authentication { #设定验证类型和密码auth_type PASS #设置验证类型,主要有PASS和AH两种auth_pass 1111 #设置验证密码,在一个vrrp_instance下,MASTER与BACKUP 必须使用相同的密码才能正常通信}virtual_ipaddress {10.0.5.155 #设置虚拟IP地址,可以设置多个虚拟IP地址,每行一个}}vrrp_instance VI_2 {state BACKUPinterface eth0virtual_router_id 52priority 60advert_int 1authentication {auth_type PASSauth_pass 2222}virtual_ipaddress {10.0.5.156}}#虚拟服务器定义部分virtual_server 10.0.5.155 80 { #设置虚拟服务器,需要指定虚拟IP地址和服务端口IP与端口之间用空格隔开delay_loop 6 #设置健康检查的时间间隔,单位是秒lb_algo rr #设置负载调度算法,这里设置为rr,即轮询算法lb_kind DR #设置LVS实现负载均衡的机制,有 NAT、TUN和DR三个模式可选nat_mask 255.255.255.0persistence_timeout 0#会话保持时间,单位是秒。这个选项对动态网页是非常有用的,为集群系统中的 session共享提供了一个很好的解决方案。有了这个会话保持功能,用户的请求会被一直分发到某个服务节点,直到超过这个会话的保持时间。需要注意的是,这个会话保持时间是最大无响应超时时间,也就是说,用户在操作动态页面时,如果在 50秒内没有执行任何操作,那么接下来的操作会被分发到另外节点,但是如果用户一直在操作动态页面,则不受50秒的时间限制。protocol TCP #指定转发协议类型,有TCP和UDP两种#sorry_server 127.0.0.1 80real_server 10.0.5.153 80 { #配置服务节点 1,需要指定 real server的真实IP地址和端口,IP与端口之间用空格隔开weight 1#配置服务节点的权值,权值大小用数字表示,数字越大,权值越高,设置权值的大小可以为不同性能的服务器分配不同的负载,可以为性能高的服务器设置较高的权值,而为性能较低的服务器设置相对较低的权值,这样才能合理地利用和分配了系统资源TCP_CHECK { #realserve的状态检测设置部分,单位是秒connect_timeout 3 #表示3秒无响应超时nb_get_retry 3 #表示重试次数delay_before_retry 3 #表示重试间隔connect_port 80 #检查80端口}}real_server 10.0.5.154 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}}virtual_server 10.0.5.156 80 {delay_loop 6lb_algo rrlb_kind DRnat_mask 255.255.255.0persistence_timeout 0protocol TCP#sorry_server 127.0.0.1 80real_server 10.0.5.153 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}real_server 10.0.5.154 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}}
Lvs:节点
node-5-152: 10.0.5.152
VIP主:10.0.5.156
VIP备:10.0.5.155
[root@node-5-152 keepalived]# cat /etc/keepalived/keepalived.conf
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 127.0.0.1smtp_connect_timeout 30router_id node_05}vrrp_instance VI_1 {state BACKUPinterface eth0virtual_router_id 51priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.0.5.155}}vrrp_instance VI_2 {state MASTERinterface eth0virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 2222}virtual_ipaddress {10.0.5.156}}virtual_server 10.0.5.155 80 {delay_loop 6lb_algo rrlb_kind DRnat_mask 255.255.255.0persistence_timeout 0protocol TCP#sorry_server 127.0.0.1 80real_server 10.0.5.153 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}real_server 10.0.5.154 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}}virtual_server 10.0.5.156 80 {delay_loop 6lb_algo rrlb_kind DRnat_mask 255.255.255.0persistence_timeout 0protocol TCP#sorry_server 127.0.0.1 80real_server 10.0.5.153 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}real_server 10.0.5.154 80 {weight 1TCP_CHECK {connect_timeout 3nb_get_retry 3delay_before_retry 3connect_port 80}}}
4、Real-Server节点配置文件
node-5-153: 10.0.5.153
node-5-154: 10.0.5.154
节点配置和路径一样
[root@node-5-153 ~]# cat /etc/init.d/lvsrs
#!/bin/bashsh /etc/rc.d/init.d/functionsVIP1=10.0.5.155VIP2=10.0.5.156case “$1” instart)ifconfig lo:1 $VIP1 netmask 255.255.255.255 broadcast $VIP1/sbin/route add -host $VIP1 dev lo:1ifconfig lo:2 $VIP2 netmask 255.255.255.255 broadcast $VIP2/sbin/route add -host $VIP2 dev lo:2echo “1” >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho “2” >/proc/sys/net/ipv4/conf/lo/arp_announceecho “1” >/proc/sys/net/ipv4/conf/all/arp_ignoreecho “2” >/proc/sys/net/ipv4/conf/all/arp_announcesysctl -p >/dev/null 2>&1echo “Real Server Start OK”;;stop)ifconfig lo:1 downroute del $VIP1 >/dev/null 2>&1ifconfig lo:2 downroute del $VIP2 >/dev/null 2>&1echo “0” >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho “0” >/proc/sys/net/ipv4/conf/lo/arp_announceecho “0” >/proc/sys/net/ipv4/conf/all/arp_ignoreecho “0” >/proc/sys/net/ipv4/conf/all/arp_announceecho “Real Server Stoped”;;status)status1=`/sbin/ifconfig lo:1 | grep $VIP1`status2=`netstat -rn | grep “lo:1” | grep $VIP1`if [ ! “$status1” -o ! “status2” ];thenecho “Lvs-DR real server Stopped.”elseecho “LVS-DR Running.”fistatus1=`/sbin/ifconfig lo:2 | grep $VIP2`status2=`netstat -rn | grep “lo:2” | grep $VIP2`if [ ! “$status1” -o ! “status2” ];thenecho “LVS-DR real server Stopped.”elseecho “LVS-DR Running.”fi;;*)# Invalid entry.echo “$0: Usage: $0 {start|status|stop}”exit 1;;esacexit 0
5、LVS节点:watch ipvsadm -Ln
Real Server节点: