【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站点:



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
haproxy配置七层负载均衡的步骤如下: 1. 安装haproxy:使用命令"yum -y install haproxy"可以安装haproxy软件包。 2. 配置haproxyhaproxy配置文件分为五个部分,分别是全局配置、默认配置、frontend配置、backend配置listen配置。对于七层负载均衡,主要关注的是frontend和backend配置。 3. 配置frontend:在frontend配置中,需要指定监听的IP和端口,并设置使用的协议和负载均衡算法。此外,还需要定义ACL规则来根据用户请求的内容将请求转发到不同的后端服务器。 4. 配置backend:在backend配置中,需要定义后端服务器的列表和属性。可以指定服务器的IP地址和端口,并设置权重和最大连接数等属性。 5. 启动haproxy:在完成配置后,可以使用命令"systemctl start haproxy"启动haproxy服务。 需要注意的是,配置七层负载均衡还需要根据具体的需求进行调整和优化,例如设置健康检查、会话保持等功能。此外,还需要确保后端服务器已经配置好并正常工作。 总结:配置haproxy七层负载均衡主要包括安装haproxy配置haproxy文件、设置frontend和backend配置、启动haproxy服务。配置过程中还需要根据需求进行调整和优化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [haproxy七层负载均衡](https://blog.csdn.net/weixin_46018506/article/details/122257643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值