首页 > 代码库 > keepalived+LVS搭建高可用负载均衡系统

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

相关架构设置:

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/shvip=192.168.137.6. /etc/rc.d/init.d/functionscase "$1" instart)ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $viproute add -host $vip dev lo:0echo "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:0 downroute del $vip >/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";;*)echo "Usage: $0 {start|stop}"exit 1esac

DS master上的keepalived的配置文件:

[root@localhost ~]# cat /etc/keepalived/keepalived.confglobal_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.confglobal_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/messagesFeb  2 16:22:04 localhost Keepalived_healthcheckers[2498]: Activating healthchecker for service [192.168.137.100]:80Feb  2 16:22:04 localhost Keepalived_healthcheckers[2498]: Activating healthchecker for service [192.168.137.200]:80Feb  2 16:22:04 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Transition to MASTER STATEFeb  2 16:22:04 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Received lower prio advert, forcing new electionFeb  2 16:22:05 localhost Keepalived_healthcheckers[2498]: TCP connection to [192.168.137.200]:80 failed !!!Feb  2 16:22:05 localhost Keepalived_healthcheckers[2498]: Removing service [192.168.137.200]:80 from VS [192.168.137.6]:80Feb  2 16:22:05 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Entering MASTER STATEFeb  2 16:22:05 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) setting protocol VIPs.Feb  2 16:22:05 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.137.6Feb  2 16:22:05 localhost Keepalived_healthcheckers[2498]: Netlink reflector reports IP 192.168.137.6 addedFeb  2 16:22:10 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.137.6

查看分发配置:

[root@localhost ~]# ipvsadmIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler Flags  -> RemoteAddress:Port           Forward Weight ActiveConn InActConnTCP  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 ~]#

 

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