【问题标题】:How to make prometheus work with grafana in Docker?如何使 prometheus 在 Docker 中与 grafana 一起工作?
【发布时间】:2018-12-19 21:16:35
【问题描述】:

主要目标是在grafana中链接prometheus作为后端,但是在grafana中输入http://localhost:9090作为url返回HTTP Error Bad Gateway

我启动了一个 prometheus docker 映像,但它没有在 IPv4 上的端口 9090 上侦听。

netstat -ntulp

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      15895/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3190/sshd           
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      24970/postmaster    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3148/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      15895/nginx: master 
tcp6       0      0 :::9100                 :::*                    LISTEN      16652/node_exporter 
tcp6       0      0 :::80                   :::*                    LISTEN      15895/nginx: master 
tcp6       0      0 :::22                   :::*                    LISTEN      3190/sshd           
tcp6       0      0 :::3000                 :::*                    LISTEN      28436/docker-proxy  
tcp6       0      0 ::1:5432                :::*                    LISTEN      24970/postmaster    
tcp6       0      0 ::1:25                  :::*                    LISTEN      3148/master         
tcp6       0      0 :::9090                 :::*                    LISTEN      31648/docker-proxy  
udp        0      0 0.0.0.0:68              0.0.0.0:*                           2806/dhclient       
udp        0      0 127.0.0.1:323           0.0.0.0:*                           1639/chronyd        
udp6       0      0 ::1:323                 :::*                                1639/chronyd  

这是我的 docker 命令:

docker run -d -p 9090:9090 --name prometheus -v /etc/prometheus.yml:/etc/prometheus/prometheus.yml -v /mnt/vol-0001/prometheus_data/:/etc/prometheus/data prom/prometheus --log.level=debug

我使用-p 9090:9090-p 0.0.0.0:9090 得到相同的结果

docker logs prometheus 返回:

level=info ts=2018-12-19T21:07:59.332452641Z caller=main.go:243 msg="Starting Prometheus" version="(version=2.6.0, branch=HEAD, revision=dbd1d58c894775c0788470944b818cc724f550fb)"
level=info ts=2018-12-19T21:07:59.332554622Z caller=main.go:244 build_context="(go=go1.11.3, user=root@bf5760470f13, date=20181217-15:14:46)"
level=info ts=2018-12-19T21:07:59.332584047Z caller=main.go:245 host_details="(Linux 3.10.0-957.1.3.el7.x86_64 #1 SMP Thu Nov 29 14:49:43 UTC 2018 x86_64 9dd3a9318064 (none))"
level=info ts=2018-12-19T21:07:59.332610547Z caller=main.go:246 fd_limits="(soft=65536, hard=65536)"
level=info ts=2018-12-19T21:07:59.332631287Z caller=main.go:247 vm_limits="(soft=unlimited, hard=unlimited)"
level=info ts=2018-12-19T21:07:59.334232116Z caller=main.go:561 msg="Starting TSDB ..."
level=info ts=2018-12-19T21:07:59.334671887Z caller=repair.go:48 component=tsdb msg="found healthy block" mint=1545204931123 maxt=1545220800000 ulid=01CZ3PHTVQQTW7Q122X7Y15WV4
level=info ts=2018-12-19T21:07:59.334756938Z caller=repair.go:48 component=tsdb msg="found healthy block" mint=1545242400000 maxt=1545249600000 ulid=01CZ44997810VTYP3GV0KJXXN1
level=info ts=2018-12-19T21:07:59.334819198Z caller=repair.go:48 component=tsdb msg="found healthy block" mint=1545220800000 maxt=1545242400000 ulid=01CZ4499ASP4RG8BPR8PE5WAKY
level=info ts=2018-12-19T21:07:59.346244745Z caller=web.go:429 component=web msg="Start listening for connections" address=0.0.0.0:9090
level=info ts=2018-12-19T21:07:59.461554488Z caller=main.go:571 msg="TSDB started"
level=info ts=2018-12-19T21:07:59.461625871Z caller=main.go:631 msg="Loading configuration file" filename=prometheus.yml
level=debug ts=2018-12-19T21:07:59.462558422Z caller=manager.go:213 component="discovery manager scrape" msg="Starting provider" provider=string/0 subs=[prometheus]
level=info ts=2018-12-19T21:07:59.462601563Z caller=main.go:657 msg="Completed loading of configuration file" filename=prometheus.yml
level=info ts=2018-12-19T21:07:59.462615458Z caller=main.go:530 msg="Server is ready to receive web requests."
level=debug ts=2018-12-19T21:07:59.462669264Z caller=manager.go:231 component="discovery manager scrape" msg="discoverer channel closed" provider=string/0

我还尝试禁用防火墙以确保它不是造成这种头痛的原因。

我不是 docker/kubernetes 专家,感谢您的帮助。

【问题讨论】:

    标签: docker grafana prometheus


    【解决方案1】:

    您在 Grafana 数据源输入中引用的 localhost 它是 Grafana 容器本身,因为 Grafana 内部将 localhost 解析为 127.0.0.1:可能因为您使用的是 GUI,所以您期望查询是通过 AJAX 发出的/frontend 调用但不,这一切都由后端支持。

    让甚至使用 Docker Compose 与使用 Networks 连接容器的服务来编排容器:

    # docker-compose.yaml
    version: "3"
    
    services:
      grafana:
        image: grafana/grafana:5.4.1
        ports:
        - 3000:3000
      prometheus:
        image: prom/prometheus:v2.5.0
    

    docker-compose up -d 之后,您可以在端口:3000 访问您的Docker 机器IP(或localhost,如果运行Docker for Mac),然后将Prometheus 数据源URL 设置为http://prometheus:9090,它将工作!

    【讨论】:

    • 谢谢。我希望有一种方法可以避免 docker-compose,因为我还不知道如何扩展它。但至少现在它正在工作:D
    • prometheus.yml 文件作为 prometheus 的配置文件呢?
    • @prometherion 很高兴它对你有用,但对我不起作用。 Prometheus 在 localhost 上根本不可用,更不用说尝试在 grafana 中映射数据源了
    • 忽略所有 docker compose 废话,只需在 Grafana docker 容器(本例中为 localhost:3000)上将“访问”设置为“浏览器”,似乎可以检测到 prometheus 数据源。我尝试使用 server 选项,使用 prometheus docker 容器的内部 IP 以及使用 docker compose 的这种方法,但这些都不起作用。
    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 2020-11-15
    • 2018-11-03
    • 1970-01-01
    相关资源
    最近更新 更多