LVS+Keepalived的安装部署
前端调度master:IP->10.252.3.10
前端调度backup:IP->10.252.3.11
VIP:10.252.3.12
RealServer:10.252.3.15
RealServer:10.252.3.16
1:主备一起安装keepalivedipvsadm
yum installkeepalived ipvsadm –y
注:使用ipvsadm –Ln可以来查看LVS的调度情况
====================================================================================
2:配置/etc/keepalived/keepalived.conf
主机10.252.3.10配置如下:
vrrp_instanceVI_1 {
state MASTER #主备的区别点,标识
interface eth1 #对应的网卡
virtual_router_id 60
priority 101 #优先级,谁做主(值越大,优先级越高)
advert_int 2
authentication { #验证方式,以及密码必须主备一样
auth_type PASS
auth_pass 1234 #相对应的密码
}
virtual_ipaddress { #切换方式。切换虚拟IP
10.252.3.12/4
}
}
Virtual_server10.252.3.12 80 { #对应的端口可以在这里修改
delay loop 6 #健康检查时间
lb_algo rr #均衡调度算法一共有十二种。这里采用轮询的方法
lb_kind DR #负载均衡转发规则有三种:NAT,DR,TUN
real_server 10.252.3.15 80 {
weight 3
TCP_CHECK { #tcp的轮询检查设置
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 10.252.3.16 80 {
weight 3
TCP_CHECK { #tcp的轮询检查设置
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
备机配置: 10.252.3.11
Backup的keepalived配置主要是修改两个地方,其他的都一样:
state BACKUP #主备的区别点,标识
priority 100 #优先级,谁做主(值越大,优先级越高)
====================================================================================
3:在realserver上运行脚本
由于使用的方式为DR,我们需要在实际虚拟机上部署DIP地址与VIP地址进行通信
编写lvs_init.sh的脚本如下所示,后端两台虚拟机的lvs_init的脚本都一样:
#!/bin/bash
#description: Config realserver lo and apply noarp
SNS_VIP= 10.252.3.12
./etc/rc.d/init.d/functions
case"$1" in
start)
ifconfig lo:0 $SNS_VIP netmask255.255.255.255 broadcast $SNS_VIP up#设置lo:0的IP地址
/sbin/route add -host $SNS_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
sysctl -p >/dev/null 2>&1 #更新配置
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null2>&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 "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
给脚本权限 chmod +x /data/script/lvs_init.sh
./lvs_init start/stop 运行和停止脚本
Service keepalived start
使用ipvsadm –Ln来查看lvs负载均衡,可以使用webbech来测试。