Using an HTTP healthcheck for a TCP server in HA Proxy
Asked Answered
I

2

6

Suppose I have a tcp server running on localhost:9000 and an HTTP server running on localhost:8000. The HTTP server exposes a URL "/healthz" that returns a 200 if the tcp server is healthy and a 500 if the tcp server is unhealthy. That is, performing:

curl localhost:9000/healthz

will return a status 200 or status 500 depending on health of the tcp server.

I want HAProxy to use localhost:8000/healthz for the healthcheck of the tcp server. Is that possible?

Indo answered 19/3, 2014 at 7:6 Comment(0)
P
4

Answering this in case anyone comes here looking for an answer.

I would have thought this wouldn't work, as you're mixing HTTP and TCP, but it appears to be possible. I've just tested with v1.4.24. The trick is to specify a different port to check on the server line in the backend. Here's a snippet of config that works for me.

frontend httpfrontend
  bind *:8080
  mode http
  option httplog
  default_backend http-backend

backend http-backend
  balance roundrobin
  mode http
  option httplog
  option httpclose
  reqadd X-Forwarded-Proto:\ http
  server server1 localhost:8000

frontend tcpfrontend
  bind *:1080
  mode tcp
  option tcplog
  default_backend tcp-backend

backend tcp-backend
  mode tcp
  option tcplog
  # specify the format of the health check to run on the backend
  option httpchk GET /healthz HTTP/1.0\r\nUser-agent:\ LB-Check\ TCP
  # check -> turn on checks for this server
  # port 8000 -> send the checks to port 8000 on the backend server (rather than 9000)
  # inter 60000 -> check every 60s
  server server1 localhost:9000 check port 8000 inter 60000
Paddy answered 17/9, 2014 at 12:51 Comment(0)
T
3

This works for me on version 1.4.24 (same as @chrskly), without any tricks:

listen service 0.0.0.0:80
  mode tcp
  balance roundrobin

  option tcplog

  option httpchk GET http://service.com/healthcheck

  server server1.service.com 192.168.0.101:80 check inter 2000
  server server2.service.com 192.168.0.102:80 check inter 2000
Thievery answered 5/3, 2015 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.