LVS+keepalived配置備忘
LVS + keepalived實現負載均衡的文檔很多,這裡不再重復說明,只記錄在配置過程中容易忘記的地方。
1. 確保IP轉發功能開啟
Shell代碼
# sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1
方法有san種
1)sysctl net.ipv4.ip_forward=1
2) echo "1" > /proc/sys/net/ipv4/ip_forward
3) 修改sysctl.conf, net.ipv4.ip_forward = 1, 執行sysctl -p命令
2. virtual ip的配置
keepalived 采用RD轉發模式
測試拓撲結構
linux director
ip:192.168.1.199
vip:192.168.1.210
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
| |
real server real server
ip: 192.168.1.13 ip: 192.168.1.14
vip: 192.168.1.210 vip: 192.168.1.210
lvs客戶端也就是real server的vip,要和linux director的vip保持一致
lvs-real.sh
Shell代碼
#!/bin/bash
VIP=192.168.1.210
/etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $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 $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 "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
linux director上keepalived.conf
Shell代碼
! Configuration File for keepalived
global_defs {
notification_email {
#
[email protected]
}
#notification_email_from
[email protected]
#smtp_server 127.0.0.1
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
virtual_server 192.168.1.210 80 {
delay_loop 3
lb_algo rr
lb_kind DR
persistence_timeout 120
protocol TCP
real_server 192.168.1.13 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.1.14 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
說明:由於是測試lvs負責均衡功能,沒有配置vrrp_instance,在實際應用中,可以配置雙linux director。所以在linux director上手工添加vip:192.168.1.210,可以使用如下方法
Shell代碼
# ip addr add 192.168.1.210 dev eth0
3. keepalived DR轉發模式,需要將real server的ip與virtual ip設定在同一個網段
4. linux director 需要安裝ipvsadm,當ipvsadm啟動後可以查看它是否被加載
Shell代碼
# lsmod | grep ip_vs
ip_vs 94742 5
#lpvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.210:80 rr persistent 120
-> 192.168.1.13:80 Route 100 0 0
-> 192.168.1.14:80 Route 100 0 0
5. 如果配置都正確,需要考慮是否將ipvs框架編譯進keepalived
6. 如還不行,考慮其它原因
XXX TODO:NAT轉發模式的配置再測試