Haproxy with consul service discovery

I am following this tutorial.

I have a service deployed and when I dig consul I get back

 dig @127.0.0.1 -p 53 brad1.service.consul SRV

; <<>> DiG 9.16.1-Ubuntu <<>> @127.0.0.1 -p 53 brad1.service.consul SRV
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 5506
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 3

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;brad1.service.consul.          IN      SRV

;; ANSWER SECTION:
brad1.service.consul.   0       IN      SRV     1 1 30000 0a016006.addr.nyc.consul.

;; ADDITIONAL SECTION:
0a016006.addr.nyc.consul. 0     IN      A       10.1.96.6
app1.node.nyc.consul.   0       IN      TXT     "consul-network-segment="

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Sun Aug 01 15:54:09 PDT 2021
;; MSG SIZE  rcvd: 155

So to me this is working correctly. Now I know you are not haproxy, and if I need to ask there I will, but I thought I would start here first.

haproxy cfg.

#---------------------------------------------------------------------
# consul dns for server resolution
#---------------------------------------------------------------------
resolvers consul
  nameserver consul 127.0.0.1:8600
  accepted_payload_size 8192
  hold valid 5s

backend default
    http-request set-var(txn.username) req.hdr(Host),field(1,.),lower
    server-template fvtt 10 _%[var(txn.username)]._tcp.service.nyc.consul resolvers consul resolve-prefer ipv4

In this case, the subdomain, is the username of the client. So in this query example the username/hostname is brad1. I have confirmed in the logs… that haproxy is getting the right username/subdomain name.

Aug  1 15:49:34 loadbalancer1 hapee-lb[15033]: 24.69.39.80:5636 [01/Aug/2021:15:49:34.724] http_in~ host_not_found/<NOSRV> 0/-1/-1/-1/0 503 222 - - SCNN 2/2/0/0/0 0/0 {brad1.domain.net|brad1} "GET / HTTP/1.1"

this log entry shows that haproxy can’t find a server "host_not_found/ " I have curled from the cli to make sure it is running and available on the right ip and port. I don’t know how to trouble shoot from here. Any help or thoughts would be greatly appreciated.

Brad

This seems to be a better way to do the integratin, I will post the complete config when I get it working.

Brad

1 Like

ok, so this haproxy dataplaneapi method for service discovery works very well with consul. Very easy and painless way to get your backend services (servers) listed in the haproxy configs. However, if you have a dynamic front end, lets say the clients username is also the subdomain name, then at the moment there is no real easy way to connect the front and backends in haproxy using consul.

I have abandoned the idea, in favor of the dataplaneapi ‘add backend’ endpoint. So my code queries the consul dns service SRV record and gets the ip and port based on the username that is being provisioned. Then I call the haproxy dataplaneapi and give it that information, it in turns configures the backend in haproxy for the new client. For the front end I use:

http-request set-var(txn.username) req.hdr(Host),field(1,.),lower
use_backend %[var(txn.username)]

What this does is create a way for a dynamic front end. I could configure both the front and back for each client, but there is no need as this works just fine.

I hope this helps someone else. I have mentioned this to the haproxy people and hopefully they will consider the feature request to have consul configure both front and back and for a complete dynamic service discovery from end to end.

Brad