码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-02 17:58:51      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

相关架构设置:

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 {
   #   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  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+LVS搭建高可用负载均衡系统

标签:

原文地址:http://www.cnblogs.com/digdeep/p/4268063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!