【问题标题】:haproxy heartbeat with backend based on http post基于http post的后端haproxy heartbeat
【发布时间】:2017-05-12 07:20:23
【问题描述】:

我想创建一个配置,使 haproxy 和后端之间的心跳基于 HTTP POST。

有人对此有任何想法吗? 我尝试了以下配置,但它只将 http HEAD 发送到后端服务器(我想要 HTTP POST):

backend mlp
    mode http
    balance     roundrobin
    server  mlp1 192.168.12.165:9210 check
    server  mlp2 192.168.12.166:9210 check

感谢您的帮助。

@Mohsin, 太感谢了。我确实工作。 但是我想指定请求消息,似乎我的配置不起作用。如果您也能提供帮助,我将不胜感激。

[root@LB_vAPP_1 tmp]# more /var/www/index.txt

POST / HTTP/1.1\r\nHost: 176.16.0.8:2234\r\nContent-Length: 653\r\n\r\n<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<svc_init ver=\"3.2.0\">\r\n<hdr ver=\"3.2.0\">\r\n<client>\r\n<id>915948</id>\r\n<pwd>915948</pwd>\r\n<serviceid></serviceid>\r\n</client>\r\n<requestor><id>13969041845</id></requestor>\r\n</hdr>\r\n<slir ver=\"3.2.0\" res_type=\"SYNC\">\r\n<msids><msid enc=\"ASC\" type=\"MSISDN\">00000000000</msid></msids>\r\n<eqop>\r\n<resp_req type=\"LOW_DELAY\"/>\r\n<hor_acc>200</hor_acc>\r\n</eqop>\r\n<geo_info>\r\n<CoordinateReferenceSystem>\r\n<Identifier
>\r\n<code>4326</code>\r\n<codeSpace>EPSG</codeSpace>\r\n<edition>6.1</edition>\r\n</Identifier\r\n</CoordinateReferenceSystem>\r\n</geo_info>\r\n<loc_type type=\"CURRENT_OR_LAST\"/>\r\n<prio type=\"HIGH\"/>\r\n</slir>\r\n</svc_init>\r\n\r\n\r\n\r\n

我的 haproxy.conf 文件如下:

#---------------------------------------------------------------------
# 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 local7

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    ulimit-n 65536
    daemon
    nbproc 1
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
defaults
    mode                    tcp
    retries                 3
    log                     global
    option                  redispatch
#    option                 abortonclose
    retries                 3
    timeout queue           28s
    timeout connect         28s
    timeout client          28s
    timeout server          28s
    timeout check           1s
    maxconn                 32000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  mlp
    mode                tcp
    option                      persist
#    bind                       10.68.97.42:9211 ssl crt /etc/ssl/server.pem
   #bind                        10.68.97.42:9211
    bind                        10.68.97.42:9210
    default_backend             mlp

frontend  supl
    mode                        tcp
    option                      persist
    bind                        10.68.97.42:7275
    default_backend             supl
#-------------
# option1 http check
#------------
backend mlp
    mode http
    balance     roundrobin
 option                      httpchk POST / HTTP/1.1\r\nHost: 176.16.0.8:2234\r\nContent-Length: 653\r\n\r\n{<?xml version=\"1.0\" encoding=\"gb2312\"?>\r\n<svc_init ver=\"3.2.0\">\r\n<hdr ver=\"3.2.0\">\r\n<client>\r\n<id>915948</id>\r\n<pwd>915948</pwd>\r\n<serviceid></serviceid>\r\n</client>\r\n<requestor><id>13969041845</id></requestor>\r\n</hdr>\r\n<slir ver=\"3.2.0\" res_type=\"SYNC\">\r\n<msids><msid enc=\"ASC\" type=\"MSISDN\">00000000000</msid></msids>\r\n<eqop>\r\n<resp_req type=\"LOW_DELAY\"/>\r\n<hor_acc>200</hor_acc>\r\n</eqop>\r\n<geo_info>\r\n<CoordinateReferenceSystem>\r\n<Identifier>\r\n<code>4326</code>\r\n<codeSpace>EPSG</codeSpace>\r\n<edition>6.1</edition>\r\n</Identifier>\r\n</CoordinateReferenceSystem>\r\n</geo_info>\r\n<loc_type type=\"CURRENT_OR_LAST\"/>\r\n<prio type=\"HIGH\"/>\r\n</slir>\r\n</svc_init>\r\n\r\n\r\n\r\n}
    http-check                  expect  rstring <result resid=\"4\">UNKNOWN SUBSCRIBER</result>
    server  mlp1 192.168.12.165:9210 check
    server  mlp2 192.168.12.166:9210 check
    #server  mlp2 192.168.12.166:9210 check 


backend supl
    mode tcp
        source  0.0.0.0 usesrc clientip
    balance     roundrobin
    server  supl1 192.168.12.165:7275 check
    server  supl2 192.168.12.166:7275 check
   #server  supl2 192.168.12.166:7275 check

【问题讨论】:

    标签: haproxy heartbeat


    【解决方案1】:

    @Mohsin, 感谢您的回答,它为我提供了解决此问题的关键线索。

    但是,我的消息如下,现在它可以按我的意愿工作(发送指定的请求并检查指定的响应)。我发布它,希望它也可以帮助其他人。一点是,内容长度非常重要。

    后端 mlp 模式http 平衡循环 选项 httpchk POST / HTTP/1.1\r\nUser-Agent:HAProxy\r\nHost:176.16.0.8:2234\r\nContent-Type:\ text/xml\r\nContent-Length:516\r\n\r \n91594891594813969041845000000000003200

    http-check                  expect  rstring <result resid=\"4\">UNKNOWN SUBSCRIBER</result>
    server  mlp1 192.168.12.165:9210 check
    server  mlp2 192.168.12.166:9210 check
    

    【讨论】:

      【解决方案2】:

      经过一番试验,我能够得到这个工作。

      这是我的设置

      HAProxy -> NGINX -> Backend
      

      我在 NGINX 阶段用 tcpdump 嗅探请求,看看实际发生了什么。

      为了更改健康检查请求,我们必须按照文档中描述的hack 更改 HTTP 版本并发送标头:

      可以通过使用 rn 和反斜杠空格连接它们来在字符串之后发送 HTTP 标头。这对于在探测虚拟主机时发送 Host 标头很有用

      这是我要发送的原始 http 检查:

      POST ${ENDPOINT} HTTP/1.0
      Content-Type: application/json
      
      {"body": "json"}
      

      这里最大的问题是 HAProxy 自己添加了一个新的标头:Connection: close,所以这就是 NGINX 得到的:

      POST ${ENDPOINT} HTTP/1.0
      Content-Type: application/json
      
      {"body": "json"}
      Connection: close
      

      至少在我的情况下,由于请求格式错误,这会导致错误 400。

      解决方法是添加一个Content-Length 标头:

      POST ${ENDPOINT} HTTP/1.0
      Content-Type: application/json
      Content-Length: 16
      
      {"body": "json"}
      Connection: close
      

      由于Content-Length 应该优先于实际长度,这会强制忽略最后一个标头。这是 NGINX 传递给后端的内容:

      POST ${ENDPOINT} HTTP/1.0
      Host: ~^(.+)$
      X-Real-IP: ${IP}
      X-Forwarded-For: ${IP}
      Connection: close
      Content-Length: 16
      Content-Type: application/json
      
      {"body": "json"}
      

      这是我最后的检查:

      option httpchk POST ${ENDPOINT} HTTP/1.0\r\nContent-Type:\ application/json\r\nContent-Length:\ 16\r\n\r\n{\"body\":\"json\"}
      

      如果只是 JSON,您应该可以复制并粘贴它并调整内容长度。 但是,我建议您遵循相同的程序并嗅探实际的运行状况检查,因为必须在配置文件中转义字符,正确创建请求可能会很棘手。

      【讨论】:

        【解决方案3】:

        打开 haproxy/conf/haproxy.conf 文件。转到页面末尾,您会看到有一行'option httpchk GET /',将GET更改为POST,您就完成了。

        如果您遇到任何问题,请告诉我。

        【讨论】:

        • 您好 Mohsin,感谢您的回答。但是请您给我更具体的建议。 haproxy.conf 中的 GET 应该替换为 POST。是如下配置:后端 mlp 模式 http balance roundrobin 选项 httpchk HEAD /index.txt HTTP/1.1\r\n http-check expect rstring UNKNOWN SUBSCRIBER server mlp1 192.168。 12.165:9210 检查服务器 mlp2 192.168.12.166:9210 检查
        • 非常感谢,我更新了我的问题,如果可能的话,你能帮我弄清楚吗?我想通过路径中的文件指定我的请求: /var/www/index.txt 。但它不起作用
        • index.txt 无效。您必须指定服务器的 URL,该 URL 应返回类型 2 的响应状态,例如 200
        • 我添加了如下所示的 URL,但仍然无法正常工作。选项 httpchk POST / HTTP/1.1\r\n主机:176.16.0.8:2234\r\n内容长度:653\r\n\r\n{******\r\n\r\n\r \n\r\n}
        • 请粘贴您的 haproxy.conf 文件的完整内容。
        猜你喜欢
        • 1970-01-01
        • 2020-02-28
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 1970-01-01
        • 2013-03-09
        • 1970-01-01
        相关资源
        最近更新 更多