【haproxy配置web站点的负载均衡案例】

haproxy的配置文件分为四个部分:

        全局配置:

        global:  全局配置段

        代理配置:

        default: 默认配置----->所有在backend、frontend、linsten中相同内容可以在此定义;

        frontend:前段配置----->定义前端套接字,接受客户端请求;

        backend: 后端配置----->定义后端分配规则,与后端服务器交互;

        listen:  绑定配置----->直接将指定的客户端与后端特定服务器绑定到一起;

 

其中Global参数如下:

    log global  

    mode http               #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK  

    option  httplog         #日志类别,采用httplog  

    option  dontlognull     #不记录健康检查日志信息  

    retries 2               #两次连接失败就认为是服务器不可用,也可以通过后面设置  

    option  forwardfor      #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip  

    option  httpclose       #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只能模拟这种模式的实现  

    #option redispatch      #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器,以后将不支持  

    option abortonclose     #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接  

    maxconn 4096            #默认的最大连接数  

    timeout connect  5000ms #连接超时  

    timeout client 30000ms  #客户端超时  

    timeout server 30000ms  #服务器超时  

    #timeout check 2000     #心跳检测超时  

    #timeout http-keep-alive10s   #默认持久连接超时时间  

    #timeout http-request   10s   #默认http请求超时时间  

    #timeoutqueue          1m     #默认队列超时时间  

    balance roundrobin     #设置默认负载均衡方式,轮询方式  

    #balance source        # 设置默认负载均衡方式,类似于nginx的ip_hash  

    #balnace leastconn     #设置默认负载均衡方式,最小连接数  

[root@master opt]# cd haproxy-1.5.0

[root@master haproxy-1.5.0]# cp examples/haproxy.cfg  web_haproxy.cfg 

[root@master haproxy-1.5.0]# ls

CHANGELOG  contrib  doc  ebtree  examples  haproxy  haproxy.cfg  haproxy-systemd-wrapper  include  LICENSE  Makefile  README  

 

ROADMAP  src  SUBVERS  tests  VERDATE  VERSION

[root@master haproxy-1.5.0]# cat  web_haproxy.cfg 

# this config needs haproxy-1.1.28 or haproxy-1.2.1

global

        log 127.0.0.1   local0

        log 127.0.0.1   local1 notice

        #log loghost    local0 info

        maxconn 4096

        #chroot /usr/share/haproxy

        uid 99

        gid 99

        daemon

        #debug

        #quiet

defaults

        log     global

        mode    http

        option  httplog

        option  dontlognull

        retries 3

        redispatch

        maxconn 2000

        contimeout      5000

        clitimeout      50000

        srvtimeout      50000

 

listen  appli1-rewrite 0.0.0.0:10001

        cookie  SERVERID rewrite

        balance roundrobin

        server  app1_1 192.168.1.104:8080 cookie app1inst1 check inter 2000 rise 2 fall 5

        server  app1_2 192.168.1.104:9990 cookie app1inst2 check inter 2000 rise 2 fall 5

        

       stats uri /haproxy-stats

       stats refresh 10s

       monitor-uri /haproxy_test

[root@master haproxy-1.5.0]# ps -ef |grep haproxy

502      17871     1  0 14:16 ?        00:00:00 haproxy -f web_haproxy.cfg

root     18030  3606  0 14:38 pts/0    00:00:00 grep haproxy

[root@master haproxy-1.5.0]# kill -9 502  -->502类似用户名root

[root@master haproxy-1.5.0]# 

[root@master haproxy-1.5.0]# 

[root@master haproxy-1.5.0]# 

[root@master haproxy-1.5.0]# ps -ef |grep haproxy

502      17871     1  0 14:16 ?        00:00:00 haproxy -f web_haproxy.cfg

root     18034  3606  0 14:38 pts/0    00:00:00 grep haproxy

[root@master haproxy-1.5.0]# kill -9 17871

[root@master haproxy-1.5.0]# ps -ef |grep haproxy

root     18038  3606  0 14:38 pts/0    00:00:00 grep haproxy



 

[root@master haproxy-1.5.0]# haproxy -f web_haproxy.cfg 

[WARNING] 354/143858 (18039) : parsing [web_haproxy.cfg:19]: keyword 'redispatch' is deprecated in favor of 'option redispatch', and will not be supported by future versions.

[WARNING] 354/143858 (18039) : parsing [web_haproxy.cfg:21] : the 'contimeout' directive is now deprecated in favor of 'timeout connect', and will not be supported in future versions.

[WARNING] 354/143858 (18039) : parsing [web_haproxy.cfg:22] : the 'clitimeout' directive is now deprecated in favor of 'timeout client', and will not be supported in future versions.

[WARNING] 354/143858 (18039) : parsing [web_haproxy.cfg:23] : the 'srvtimeout' directive is now deprecated in of 'timeout server', and will not be supported in future versions.

服务测试端口:

 

测试结果:

站点B的端口是9990
 站点A的端口是8080

 对外界提供的访问端口是10001:

此时访问的是B站点:

 此时访问的是A站点:



 

猜你喜欢

转载自gaojingsong.iteye.com/blog/2346406