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/sh
vip=192.168.137.6
. /etc/rc.d/init.d/functions

case "$1" in
start)
ifconfig lo:0 $vip netmask 255.255.255.255 broadcast $vip
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 "Real server 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 "Real server stoped"
;;

*)
echo "Usage: $0 {start|stop}"
exit 1
esac

DS master上的keepalived的配置文件:

[root@localhost ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   #notification_email {
   #   [email protected]
   #   [email protected]
   #   [email protected]
   #}
   #notification_email_from [email protected]
   #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 {
   #  [email protected]
   #  [email protected]
   #  [email protected]
   #}
   #notification_email_from [email protected]
   #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  2 16:22:04 localhost Keepalived_healthcheckers[2498]: Activating healthchecker for service [192.168.137.100]:80
Feb  2 16:22:04 localhost Keepalived_healthcheckers[2498]: Activating healthchecker for service [192.168.137.200]:80
Feb  2 16:22:04 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb  2 16:22:04 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Feb  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]:80
Feb  2 16:22:05 localhost Keepalived_vrrp[2499]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb  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.6
Feb  2 16:22:05 localhost Keepalived_healthcheckers[2498]: Netlink reflector reports IP 192.168.137.6 added
Feb  2 16:22:10 localhost Keepalived_vrrp[2499]: 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 ~]#

 

你可能感兴趣的:(keepalived)