基于http帖子的后端haproxy心跳

时间:2015-08-03 03:16:34

标签: haproxy heartbeat

我想创建一个配置,使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
&#13;
&#13;
&#13;

我的haproxy.conf文件如下:

&#13;
&#13;
#---------------------------------------------------------------------
# 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
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

@Mohsin,     谢谢你的回答,它给了我解决这个问题的关键线索。

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

后端mlp     模式http     平衡圆形 选项httpchk POST / HTTP / 1.1 \ r \ nUser-Agent:HAProxy \ r \ n \ nNost:176.16.0.8:2234 \ r \ nContent-Type:\ text / xml \ r \ nContent-Length:516 \ r \ n \ r \ n \ 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

答案 1 :(得分:1)

经过一些实验后,我才能使这个工作。

这是我的设置

HAProxy -> NGINX -> Backend

我正在使用tcpdump在NGINX阶段嗅探请求以查看实际发生的情况。

要更改运行状况检查请求,我们必须按照文档中描述的hack更改HTTP版本并发送标头:

  

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

这是我要发送的原始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你应该可以复制并粘贴它并调整内容长度。 但是,我建议您遵循相同的过程并嗅探实际的运行状况检查,因为,如果必须在配置文件中转义字符,正确创建请求可能会非常棘手。

答案 2 :(得分:0)

打开haproxy / conf / haproxy.conf文件。转到页面的末尾,您会看到有一行&#39;选项httpchk GET /&#39;,将GET更改为POST,您就完成了。

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