HAProxy community

Server-template and SRV with multiple local servers with different ports

Hi,

Setup

I am having two servers running locally, and I have Consul running DNS on 127.0.0.1:8600:

$ dig +short @127.0.0.1 -p 8600 -t srv _whoami-whoami-whoami._tcp.service.consul
1 1 25654 Kaspers-MBP.lan.node.dc1.consul.
1 1 21387 Kaspers-MBP.lan.node.dc1.consul.

This means that the service is running on port 21387 and 25654, if I visit them I see the following:

$ curl http://localhost:21387/
I’m 8235fc9759d6 running on linux/amd64

and:

$ curl http://localhost:25654/
I’m 0d3dc4abb184 running on linux/amd6

I then setup Haproxy to switch between them like this:

global
  maxconn 5000
  pidfile /usr/lib/haproxy/haproxy.pid
  stats socket /home/kasper/haproxy.sock

defaults
  timeout connect 5000
  timeout client  50000
  timeout server  50000
  log     global

listen stats
  bind 0.0.0.0:8999
  mode http
  stats enable
  stats uri /dashboard  # Stats URI
  stats admin if { src 127.0.0.1 }

resolvers consuldns
  nameserver consul 127.0.0.1:8600
  accepted_payload_size 8192

listen whoami
  bind *:8022
  mode http
  option httpchk GET / HTTP/1.0
  balance roundrobin
  server-template srv 2 _whoami-whoami-whoami._tcp.service.consul resolvers consuldns check resolve-opts allow-dup-ip

And when I boot Haproxy it writes the following:

[WARNING] 140/150710 (38827) : Server whoami/srv1 is DOWN, reason: Socket error, check duration: 0ms. 1 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[WARNING] 140/150711 (38827) : Server whoami/srv2 is DOWN, reason: Socket error, check duration: 0ms. 0 active and 0 backup servers left. 0 sessions active, 0 requeued, 0 remaining in queue.
[ALERT] 140/150711 (38827) : proxy ‘whoami’ has no server available!
[WARNING] 140/150711 (38827) : whoami/srv1 changed its IP from to 127.0.0.1 by consuldns/consul.
[WARNING] 140/150711 (38827) : whoami/srv2 changed its IP from to 127.0.0.1 by DNS cache.
[WARNING] 140/150714 (38827) : Server whoami/srv1 is UP, reason: Layer7 check passed, code: 200, info: “OK”, check duration: 2ms. 1 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
[WARNING] 140/150715 (38827) : Server whoami/srv2 is UP, reason: Layer7 check passed, code: 200, info: “OK”, check duration: 3ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.

When I use show servers state the server show up correctly, with the correct ports:

# be_id be_name srv_id srv_name srv_addr srv_op_state srv_admin_state srv_uweight srv_iweight srv_time_since_last_change srv_check_status srv_check_result srv_check_health srv_check_state srv_agent_state bk_f_forced_id srv_f_forced_id srv_fqdn srv_port srvrecord
3 whoami 1 srv1 127.0.0.1 2 0 1 1 153 15 3 4 6 0 0 0 Kaspers-MBP.lan.node.dc1.consul 25654 _whoami-whoami-whoami._tcp.service.consul
3 whoami 2 srv2 127.0.0.1 2 0 1 1 152 15 3 4 6 0 0 0 Kaspers-MBP.lan.node.dc1.consul 21387 _whoami-whoami-whoami._tcp.service.consul

Issue

However when I visit the service that I setup with Haproxy by going to http://localhost:8022, it doesn’t seem to loadbalance between the different servers because only one of the the names shows up:

Am I missing something?

Your browser keeps the session open, which is keep-alived towards a specific server.

Open multiple different sessions (different browser or curl output) or disable keepalive.

1 Like

Oh wow! That is right, that is what I am seeing, thanks!

How do I turn that off so that the connection between the browser and Haproxy is keepalived, but that Haproxy will use another backend?

I’ve tried doing this, but I still see the same behaviour:

listen whoami
  bind *:8022
  mode http
  option httpchk GET / HTTP/1.0
+  option http-server-close
  balance roundrobin
  server-template srv 5 _whoami-whoami-whoami._tcp.service.consul resolvers consuldns check resolve-opts allow-dup-ip

Can you provide the entire configuration as well as the output of haproxy -vv. I don’t see this behavior here, in fact, even without option http-server-close it round-robes every request for me (but that maybe a local issue here).

1 Like

I am not sure what I did wrong, sorry. It does work for me now.

Thank you so much for your help and quick answers @lukastribus.

1 Like

I’ve figured out the difference for me, if there are only 2 backend servers running then Chrome will keep hitting the same server, however, when there are 3 or more, Chrome will hit all 3 servers randomly, do you know if that is anything internally in Haproxy I’m hitting?

frontend https-all
  bind *:443
  mode tcp
  use_backend benodes

backend benodes
  mode http
  option httpchk GET / HTTP/1.0
  server-template srv 5 _whoami-whoami-whoami._tcp.service.consul resolvers consuldns check resolve-opts allow-dup-ip

I’ve solved it,

Because of round-robin the first backend server is hit with a /, and the next backend server is with a /favicon.ico and this keeps happening for every browser hit with Chrome, ensuring that the same backend will serve me every time.

1 Like