Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an HTTP healthcheck for a TCP server in HA Proxy

Tags:

haproxy

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?

like image 983
Michael Avatar asked Mar 19 '14 07:03

Michael


People also ask

Does HAProxy support TCP?

HAProxy can run in two different modes: TCP or HTTP.

How do I monitor traffic on HAProxy?

The best way to ensure proper HAProxy performance and operation is by monitoring its key metrics in three broad areas: Frontend metrics such as client connections and requests. Backend metrics such as availability and health of backend servers. Health metrics that reflect the state of your HAProxy setup.

What is a TCP Healthcheck?

TCP is the default, specified as a TCP: port pair, for example "TCP:5000". In this case, a health check simply attempts to open a TCP connection to the instance on the specified port. Failure to connect within the configured timeout is considered unhealthy.


2 Answers

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
like image 52
chrskly Avatar answered Oct 16 '22 12:10

chrskly


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
like image 39
Kasper Grubbe Avatar answered Oct 16 '22 13:10

Kasper Grubbe