keepalived+haproxy 高可用均衡负载

0x01 keepalived

keepalived 是基于VRRP协议+lvs的一个高可用负载均衡软件。

其拥有三种工作方式,DR,IPtun,nat。第一种方式使用改写mac地址的方式实现请求的转发。

该方式要求Director转发器必须与服务器处在同一个物理网络环境下。

当请求到达Director时,首先Director会根据后端真实服务器的运行情况选出应答本次请求的服务器,然后修改

来源请求包的目标mac地址将目标mac从Director的mac修改为真实realserver的mac,


在realserver端处理请求后,根据来源ip 返回处理结果。该过程中只是对mac地址的改写,并不涉及对请求ip包的处理,工作在layer2。

在请求的返回过程中,不使用Director作为返回的路由,故优点是该方法能够达到很高的转发效率,缺点为Director与realserver必须在同一物理环境下。


第二中方式为IPtun,原理上是使用iptunnling (IP encapsulation )方式,该方式要求真实服务器主机支持IP encapsulation。

在请求包到达Director时,Director同样根据后端realserver的负载情况选出应答本次请求的服务器,然后原ip包的基础上进行IP encapsulation。

realserver 接收到请求后,将IP encapsulation 过的请求包还原成正常的ip包然后处理。处理结束后与DR方式同样不使用DR最为返回路由通过其他路由方式将

应答包返回。该方式对比与DR方式优点在于不必须Director与realserver在同一物理环境,只需要能互相通讯即可。缺点在于Director工作量增加,所能处理的最大请求数量

不如DR方式。


第三种方式为NAT方式,该方使用类似路由的方式,仅仅改变请求ip包中的目的ip地址,将ip包进行转发。待realserver接收到请求包并处理后,按原路返回实际Director起到

的功能就是路由。该方法优点在于实现简单,不要求具体的网络环境与主机类型,缺点在于转发效率不高,因为同时需要处理来源与请求的返回。


lvs支持十种均衡算法,分别为:

  • Round-Robin Scheduling (rr)
  • Weighted Round-Robin Scheduling (wrr)
  • Least-Connection Scheduling (lc)
  • Weighted Least-Connection Scheduling (wlc)
  • Locality-Based Least-Connection Scheduling (lblc)
  • Locality-Based Least-Connection with Replication Scheduling (lblcwr)
  • Destination Hashing Scheduling (dh)
  • Source Hashing Scheduling (sh)
  • Shortest Expected Delay Scheduling (sed)
  • Never Queue Scheduling (nq)

具体均衡算法的介绍在官方Document中均有介绍

0x02 keepalived

keeplaived 集成了vrrp(虚拟路由冗余协议)与lvs。

简单来说其功能分为两部分,lvs部分与vrrp部分。

对于lvs不再赘述。vrrp 支持多个虚拟路由之间的通讯与设置,百度百科doc

可以设置master 与backup,在非故障的情况下,由master 完成对请求的处理工作,一旦master出现故障造成无法提供服务。

backup会通过vrrp协议请求不到master的应答,自动设置为master并替代master进行提供服务。

keepalived还提供很多高级配置属性,可查看官网文档。


0x03 haproxy

haproxy提供对TCP以及HTTP的代理支持4层和7层的工作模式。使用事件驱动模型异步IO,可以支持非常多的的并发连接。

其本身对高可用支持不如keepalived,但是其提供了几项keepalived不支持的功能,所以使用二者同时工作达到均衡负载和高可用的目的。


0x04 环境介绍

使用vmware esxi 虚拟机虚拟安装,一共使用5台虚拟机,均使用centOS 6.5 。

其中一台作为网关gate,两台为vrouter ,两台为realserver。

两台vrouter 安装haproxy与keepalived ,两台realserver 均安装apache。

用户请求首先由gate接受,使用iptables 做DNAT 。由vrouter 均衡负载将请求交到realserver 后将处理结果返回给用户端。


0x05 部署过程

vrouter1:

不使用编译安装的方式直接使用yum 进行安装。

配置网关,路由。



安装haproxy,keepalived

yum install haproxy keepalived

编辑keepalived 配置文件 /etc/keepalived/keepalived.conf

</pre><pre name="code" class="plain">! Configuration File for keepalived

global_defs {
	router_id zvrouter1
}

vrrp_sync_group bl_group{
group {
	vroute1
}
}

vrrp_instance vroute1 {
    state MASTER
    interface eth2
    lvs_sync_daemon_interface eth3
    virtual_router_id 51
    priority 150
    advert_int 2
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
	192.168.2.103
}
}


其中 状态为MASTER,使用eth3 作为状态同步网卡,vrouterid为51,优先级 150,虚拟路由地址为192.168.2.103

然后配置haproxy

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
stats uri /hadmin
	timeout connect 5000
	timeout client 50000
	timeout server 50000
stats auth niem:niem
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend http-in
	bind *:80
	mode http
	option httplog
	log global
	default_backend htmlpool

backend htmlpool
	balance roundrobin
	server web1 192.168.2.101:80 cookie 1 check inter 2000 rise 2 fall 3
	server web2 192.168.2.102:80 cookie 2 check inter 2000 rise 2 fall 3
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
#backend static
#    balance     roundrobin
#    server      static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------

其中日志保存在本地,使用http方式,控制台网址为/hadmin ,认证方式为niem:niem.

配置后台网站为web1与web2后面为相关的检查以及失败重试次数。


以上配置完成后使用

service keepalived start
service haproxy start
启动keepalived和haproxy


vroute2:

配置ip,路由





同样使用yum 安装keepalived,haproxy。

haproxy 配置文件与vrouter1 完全相同。

keepalived 配置文件:

global_defs {
	router_id zvrouter1
}

vrrp_sync_group bl_group{
group {<pre name="code" class="plain">

vroute2}}vrrp_instance vroute2 { state BACKUP interface eth0 lvs_sync_daemon_interface eth1 virtual_router_id 51 priority 100 advert_int 2 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress {192.168.2.103}}

 

配置完成后启动keepalived和haproxy 服务。

配置realserver:

realserver1和realserver2的配置是完全相同的,故以下只说明realserver1的配置方式。

安装apache

yum install httpd
修改配置文件并启动httpd服务,使用本地浏览器或者使用wget 验证httpd可以正常提供网页服务。

使用ifconfig 用iptunl 虚拟网卡。

ifconfig tunl0 192.168.2.103 netmask 255.255.255.255 broadcast 192.168.2.103 up


设置路由

其中需要注意的是在路由表中添加虚拟网卡的路由,让通过虚拟网卡过来的流量使用tunl0。(其实不配置这条也是好用的)

route add -host 192.168.2.103 dev tunl0
下一步需要配置网卡属性,让tunl0 成为nonarp网卡。

echo "1" >  /proc/sys/net/ipv4/conf/all/arp_ignore 
echo "2" >  /proc/sys/net/ipv4/conf/all/arp_announce
echo "1" >  /proc/sys/net/ipv4/conf/tunl0/arp_ignore 
echo "2" >  /proc/sys/net/ipv4/conf/tunl0/arp_announce

以上就是realserver的配置过程。完成后可以使用该网内任意客户端访问192.168.2.103观察访问结果。(需要在realserver1和realserver2分别放置不同的网页)。

在lvs,keepalived,haproxy原理上大体相同,都是由网关的请求经过均衡后转发请求到对应的realserver中。请求由网关进入,网关使用arp查找192.168.2.103的网卡,由于

realserver 们都配置了nonarp 所以不会应答只有vrouter会应答。待realserver处理后,经过路由不回到vrouter中,直接返回。

通过抓包可以查看网络流量的流传过程。


0x06 总结

在试验的时候碰到一些情况,可以使用

tail -f /var/log/message 
实时跟踪日志的记录进行排错

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值