<服务>Keepalived+LVS

时间:Jan. 19, 2017 分类:

目录:

keepalived+lvs配置

[root@why-1 keepalived]# cat keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {
   49000448@qq.com
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 10.0.0.1
   smtp_connect_timeout 30
   router_id LVS_1
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.204/24
    }
}

virtual_server 192.168.0.204 80 {   #VIP和端口
    delay_loop 6
    lb_algo wrr                     #算法
    lb_kind DR                      #模式
    nat_mask 255.255.255.0          #子网掩码
    persistence_timeout 300         #会话保持时间
    protocol TCP                    #协议
    #以上这一段就相当于ipvsadm -A -t 192.168.0.204:80 -s wrr -p 300
    real_server 192.168.0.202 80 {  #RIP和端口
        weight 1                    #权重
        TCP_CHECK {                 #健康检查
            connect_timeout 8       #超时时间
            nb_get_retry 3          #重试次数
            delay_before_retry 3    #延迟重试的次数
            connect_port 80         #检查端口
        }
    }
    real_server 192.168.0.203 80 {  #RIP和端口
        weight 1                    #权重
        TCP_CHECK {                 #健康检查
            connect_timeout 8       #超时时间
            nb_get_retry 3          #重试次数
            delay_before_retry 3    #延迟重试的次数
            connect_port 80         #检查端口
        }
    }    
}

当然也有一些别的CHECK方式,例如HTTP_GET等,可以通过man keepalived.conf查看

keepalived+lvs检验

192.168.0.201端

[root@why-1 keepalived]# /etc/init.d/keepalived start
正在启动 keepalived:                                      [确定]
[root@why-1 keepalived]# ip add | grep 192.168.0.204
    inet 192.168.0.204/24 scope global secondary eth0
[root@why-1 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.204:80 wrr persistent 300
  -> 192.168.0.202:80             Route   1      0          0         
  -> 192.168.0.203:80             Route   1      0          0         

192.168.0.130端

[root@why keepalived]# /etc/init.d/keepalived start
正在启动 keepalived:                                      [确定]
[root@why keepalived]# ip add | grep 192.168.0.204
[root@why keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.204:80 wrr persistent 300
  -> 192.168.0.202:80             Route   1      0          0         
  -> 192.168.0.203:80             Route   1      0          0      

可以看到这的为201机器的LVS提供负载均衡服务

[root@why-1 keepalived]# ipvsadm -Lnc
IPVS connection entries
pro expire state       source             virtual            destination
TCP 04:56  NONE        192.168.0.103:0    192.168.0.204:80   192.168.0.203:80
TCP 00:56  SYN_RECV    192.168.0.103:50263 192.168.0.204:80   192.168.0.203:80

可以看到我通过主机103访问,到达204的LB上转发到203上。

当停止一端的keepalived的时候,另一端启用lvs前会清空一下ARP的缓存,再提供服务的。

企业使用

一般我们都是采用keepalived+lvs的DR模式进行高可用负载均衡,当然也有用nginx或者haproxy的。