4

I am cross posting this from stack overflow since it was suggested I might get a better answer here ...

I was wondering if anyone has used HAProxy for sharding. Specifically, I would like to be able use a cookie I define w/a identifier and have any client that presents that cookie/identifier combination be directed to the same server. It seems possible and my preliminary tests seem to work but I am not a sure.

My listen configuration looks like:

listen  appli1-rewrite 0.0.0.0:80
cookie  mysession= prefix
balance roundrobin
server  app1_1 192.168.0.1:80 cookie server01
server  app1_2 192.168.0.2:80 cookie server02
option  httpclose

However, when I look at the request headers using Live Http Headers I don't see the mysession cookie indicating which server it will be going to, which is what I expected based on the prefix operation. I am quite new to HAProxy so any help/guidance would be great!

5 Answers 5

3

first, remove the equal sign after your cookie name, otherwise it will never match. Second, please ensure that your server correctly sets the "mysession" cookie, as the cookie prefix mode relies on the server to set the cookie. If it does not, use the insert mode instead. Third, please add "option httpclose" in your config, because if there is any sort of keepalive, haproxy won't see the cookie (or will not be able to mangle it).

Hoping this helps,
Willy

2

Your config is OK. You can check in the logs that the cookie presented by the client was recognized and used. It will put the letter 'V' at the first position in the 4-character status flags (eg: VN--). First requests without a valid cookie will have a 'N' instead. Please check the doc for more info, you'll have all that in section 8.5. You can also enable the stats page and check them while you browse. You should see that the "LbTot" column only increases once per new client and is not incremented anymore for a same client.

1

You can try the capture cookie feature (all depends of the served application) in your listen section, see :http://haproxy.1wt.eu/download/1.3/doc/configuration.txt:

Only the first cookie is captured. Both the "cookie" request headers and the "set-cookie" response headers are monitored. This is particularly useful to check for application bugs causing session crossing or stealing between users, because generally the user's cookies can only change on a login page.

Example: capture cookie ASPSESSION len 32


By the way it seems like your config is ok.

1
listen corporate_web_live
  bind 1.2.3.4:80  # site1.com
  bind 1.2.3.5:80  # site2.com
    option httpchk HEAD /server.txt HTTP/1.0
    cookie HAPSRV insert postonly indirect
    server webapp-corp-1 10.0.0.1:80 weight 50 maxconn 150 slowstart 30s cookie WAC1 check
    server webapp-corp-2 10.0.0.2:80 weight 50 maxconn 150 slowstart 30s cookie WAC2 check
    server webapp-corp-3 10.0.0.3:80 weight 50 maxconn 150 slowstart 30s cookie WAC3 check
    server webapp-corp-4 10.0.0.4:80 weight 50 maxconn 150 slowstart 30s cookie WAC4 check
    server webapp-corp-5 10.0.0.5:80 weight 50 maxconn 150 slowstart 30s cookie WAC5 check
    server webapp-corp-6 10.0.0.6:80 weight 50 maxconn 150 slowstart 30s cookie WAC6 check

Note the use of 'insert', 'postonly' and 'indirect' in the example above - this causes HAproxy to chuck in a cookie as required to the streams which traverse it.

0

I tried cookie capture and it did not show any difference from what live http headers was showing me ... When I do change the config such that haproxy will insert its own cookie I can see it in the http headers ... I am starting to believe that my original tests may have been faulty.

You must log in to answer this question.