prometheus / haproxy_exporter

Simple server that scrapes HAProxy stats and exports them via HTTP for Prometheus consumption
Apache License 2.0
617 stars 219 forks source link

dial tcp 127.0.0.1:8181: connect: connection refused #242

Closed ZiyamSanthosh closed 2 years ago

ZiyamSanthosh commented 2 years ago

I have configured HAProxy locally in my machine and I have tried to read the metrics of it using haproxy_exporter. I am using the docker image of the haproxy_exporter and used the below-given command to execute it.

docker run -p 9101:9101 prom/haproxy-exporter:latest --haproxy.scrape-uri="http://localhost:8181/stats;csv"

If I try to reach the metric endpoint, I am getting this error saying that the connection is refused. What am I doing wrong?

ts=2022-05-06T10:03:49.462Z caller=haproxy_exporter.go:584 level=info msg="Starting haproxy_exporter" version="(version=0.13.0, branch=HEAD, revision=c5c72aa059b69c18ab38fd63777653c13eddaa7f)"
ts=2022-05-06T10:03:49.462Z caller=haproxy_exporter.go:585 level=info msg="Build context" context="(go=go1.17.3, user=root@77b4a325967c, date=20211126-09:54:41)"
ts=2022-05-06T10:03:49.462Z caller=haproxy_exporter.go:603 level=info msg="Listening on address" address=:9101
ts=2022-05-06T10:03:49.464Z caller=tls_config.go:195 level=info msg="TLS is disabled." http2=false
ts=2022-05-06T10:03:56.366Z caller=haproxy_exporter.go:399 level=error msg="Can't scrape HAProxy" err="Get \"http://localhost:8181/stats;csv\": dial tcp 127.0.0.1:8181: connect: connection refused"

Below given is the configuration I used for HAProxy

defaults
    mode http
    timeout server 5s
    timeout connect 5s
    timeout client 5s

frontend http80
    bind *:80
    default_backend allservers

listen stats
    bind *:8181
    stats enable
    stats uri /stats
    stats refresh 3s

backend allservers
    server server1 33.112.207.203
aminvakil commented 2 years ago

localhost in your container is itself, not host which is running docker and haproxy.

aminvakil commented 2 years ago

Citing from https://stackoverflow.com/a/62431165/3835210:

For linux systems, you can – starting from major version 20.04 of the docker engine – now also communicate with the host via host.docker.internal. This won't work automatically, but you need to provide the following run flag: --add-host=host.docker.internal:host-gateway

ZiyamSanthosh commented 2 years ago

Citing from https://stackoverflow.com/a/62431165/3835210:

For linux systems, you can – starting from major version 20.04 of the docker engine – now also communicate with the host via host.docker.internal. This won't work automatically, but you need to provide the following run flag: --add-host=host.docker.internal:host-gateway

Thank you @aminvakil