keepalived+LVS搭建高可用负载均衡系统

时间:2023-03-08 15:34:17

相关架构设置:

1)vip : 192.168.137.6

2)DS master ip : 192.168.137.8

3)DS backup ip : 192.168.137.9

4)RS 1 ip: 192.168.137.100

5)RS 2 ip: 192.168.137.200

两台RS上的配置脚本:lvsrs

[root@localhost init.d]# cat /etc/init.d/lvsrs
#!/bin/sh
vip=192.168.137.6
. /etc/rc.d/init.d/functions case "$1" in
start)
ifconfig lo: $vip netmask 255.255.255.255 broadcast $vip
route add -host $vip dev lo:
echo "" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "" > /proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p > /dev/null >&
echo "Real server start ok"
;; stop)
ifconfig lo: down
route del $vip >/dev/null >&
echo "" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "" > /proc/sys/net/ipv4/conf/all/arp_announce
echo "Real server stoped"
;; *)
echo "Usage: $0 {start|stop}"
exit
esac

DS master上的keepalived的配置文件:

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
global_defs {
#notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
#}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.137.1
#smtp_connect_timeout 30
router_id LVS_DS_IP
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.137.6
}
} virtual_server 192.168.137.6 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP real_server 192.168.137.100 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
} real_server 192.168.137.200 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

DS backup上的keepalived的配置文件:

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
global_defs {
#notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
#}
#notification_email_from Alexandre.Cassen@firewall.loc
#smtp_server 192.168.137.1
#smtp_connect_timeout 30
router_id LVS_DS_IP
} vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.137.6
}
} virtual_server 192.168.137.6 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 0
protocol TCP real_server 192.168.137.100 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
} real_server 192.168.137.200 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

启动命令:

service keepalived start | stop

service lvsrs start | stop

查看DS上的keepalived是否配置成功:

tail -f /var/log/message

[root@localhost ~]# tail -f /var/log/messages
Feb :: localhost Keepalived_healthcheckers[]: Activating healthchecker for service [192.168.137.100]:
Feb :: localhost Keepalived_healthcheckers[]: Activating healthchecker for service [192.168.137.200]:
Feb :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Feb :: localhost Keepalived_healthcheckers[]: TCP connection to [192.168.137.200]: failed !!!
Feb :: localhost Keepalived_healthcheckers[]: Removing service [192.168.137.200]: from VS [192.168.137.6]:
Feb :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.137.6
Feb :: localhost Keepalived_healthcheckers[]: Netlink reflector reports IP 192.168.137.6 added
Feb :: localhost Keepalived_vrrp[]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.137.6

查看分发配置:

[root@localhost ~]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.137.6:http wrr
-> 192.168.137.100:http Route 3 0 0
-> 192.168.137.200:http Route 3 3 0
[root@localhost ~]#