Using ID in URL for load balancing with HAProxy

时光怂恿深爱的人放手 提交于 2020-06-01 07:53:44

问题


I know it is possible to make connections sticky based on url a parameter: https://serverfault.com/questions/495049/using-url-parameters-for-load-balancing-with-haproxy?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

Is it also possible to do it based on an ID in the url path?

If my url is: /objects/:objectId

Can I somehow use that :objectId to make the connection sticky?

EDIT

I was able to load balance making the request sticky on the url path using the configuration below:

global
    #daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    balance roundrobin
    stick-table type string size 200k expire 30m
    stick on path
    server server1 127.0.0.1:8000
    server server2 127.0.0.1:8001

listen stats
    bind 127.0.0.1:9000
    mode            http
    log             global

    maxconn 10

    stats enable
    stats hide-version
    stats refresh 5s
    stats show-node
    stats auth admin:password
    stats uri  /haproxy?stats

The problem now is that if one of the servers go down the stick-table is not updated. How can I make it so that if one of the servers is not reachable the entries in the stick-table that point to that server are deleted?

Final Answer

Ok, I was able to figure that out. The configuration below makes the requests stick on the url path and HAProxy will make an HTTP GET to /health at every 250ms and if it doesn't returns 200 it will consider the server to be down and that will remove all entries from the stick-table.

global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers


backend servers
    balance roundrobin
    stick-table type string size 200k expire 30m
    option httpchk GET /health
    http-check expect status 200
    stick on path,word(2,/)  if { path_beg /objects/ }
    server server1 127.0.0.1:8000 check inter 250
    server server2 127.0.0.1:8001 check inter 250

listen stats
    bind 127.0.0.1:9000
    mode            http
    log             global

    maxconn 10

    stats enable
    stats hide-version
    stats refresh 5s
    stats show-node
    stats auth admin:password
    stats uri  /haproxy?stats

回答1:


Use this:

stick on path,word(2,/)  if { path_beg /objects/ }


来源:https://stackoverflow.com/questions/50648689/using-id-in-url-for-load-balancing-with-haproxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!