Prometheus(在 Docker 容器中)无法在主机上抓取目标

Nyx*_*nyx 13 docker docker-compose prometheus

Prometheus 在docker容器(版本 18.09.2,构建 6247962,docker-compose.xml如下)中运行,并且刮取目标是localhost:8000由 Python 3 脚本创建的。

为失败的抓取目标 ( localhost:9090/targets)获得的错误是

获取http://127.0.0.1:8000/metrics:拨号 tcp 127.0.0.1:8000:getsockopt:连接被拒绝

问题:为什么 docker 容器中的 Prometheus 无法抓取在主机(Mac OS X)上运行的目标?我们如何让在 docker 容器中运行的 Prometheus 能够抓取在主机上运行的目标?

失败的尝试:尝试替换docker-compose.yml

networks: 
  - back-tier
  - front-tier
Run Code Online (Sandbox Code Playgroud)

和

network_mode: "host"
Run Code Online (Sandbox Code Playgroud)

但随后我们无法访问 Prometheus 管理页面localhost:9090。

无法从类似问题中找到解决方案

docker-compose.yml

version: '3.3'

networks:
  front-tier:
  back-tier:

services:

  prometheus:
    image: prom/prometheus:v2.1.0
    volumes:
      - ./prometheus/prometheus:/etc/prometheus/
      - ./prometheus/prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    ports:
      - 9090:9090
    networks:
      - back-tier
    restart: always


  grafana:
    image: grafana/grafana
    user: "104"
    depends_on:
      - prometheus
    ports:
      - 3000:3000
    volumes:
      - ./grafana/grafana_data:/var/lib/grafana
      - ./grafana/provisioning/:/etc/grafana/provisioning/
    env_file:
      - ./grafana/config.monitoring
    networks:
      - back-tier
      - front-tier
    restart: always
Run Code Online (Sandbox Code Playgroud)

普罗米修斯.yml

global:
scrape_interval:     15s 
evaluation_interval: 15s 

external_labels:
    monitor: 'my-project'

- job_name: 'prometheus'

    scrape_interval: 5s

    static_configs:
        - targets: ['localhost:9090']


- job_name: 'rigs-portal'

    scrape_interval: 5s

    static_configs:
        - targets: ['127.0.0.1:8000']
Run Code Online (Sandbox Code Playgroud)

输出在 http://localhost:8000/metrics

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 65.0
python_gc_objects_collected_total{generation="1"} 281.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable object found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 37.0
python_gc_collections_total{generation="1"} 3.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="7",patchlevel="3",version="3.7.3"} 1.0
# HELP request_processing_seconds Time spend processing request
# TYPE request_processing_seconds summary
request_processing_seconds_count 2545.0
request_processing_seconds_sum 1290.4869346540017
# TYPE request_processing_seconds_created gauge
request_processing_seconds_created 1.562364777766845e+09
# HELP my_inprorgress_requests CPU Load
# TYPE my_inprorgress_requests gauge
my_inprorgress_requests 65.0
Run Code Online (Sandbox Code Playgroud)

Python3 脚本

from prometheus_client import start_http_server, Summary, Gauge
import random
import time

# Create a metric to track time spent and requests made
REQUEST_TIME = Summary("request_processing_seconds", 'Time spend processing request')

@REQUEST_TIME.time()
def process_request(t):
    time.sleep(t)

if __name__ == "__main__":
    start_http_server(8000)
    g = Gauge('my_inprorgress_requests', 'CPU Load')
    g.set(65)

    while True:
        process_request(random.random())
Run Code Online (Sandbox Code Playgroud)

rbe*_*nto 16

下面是在 macOS 的 Docker 上运行 Prometheus 的示例,这会导致 Prometheus 抓取运行在 上的简单 Spring Boot 应用程序localhost:8080:

重击

docker run --rm --name prometheus -p 9090:9090 -v /Users/YourName/conf/prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus
Run Code Online (Sandbox Code Playgroud)

/Users/你的名字/conf/prometheus.yml

global:
  scrape_interval:     15s 
  evaluation_interval: 15s 

scrape_configs:
  - job_name: 'spring-boot'
    metrics_path: '/actuator/prometheus'
    scrape_interval: 5s
    static_configs:
      - targets: ['host.docker.internal:8080']
Run Code Online (Sandbox Code Playgroud)

在这种情况下,当配置文件映射到 Prometheus 容器时,是使用特殊域host.docker.internal而不是导致从 macOS 上的容器解析主机。localhost

环境

  • Macbook Pro、苹果 M1 Pro
  • Docker 版本 20.10.17,内部版本 100c701
  • 普罗米修斯2.38


Ale*_*uma 14

虽然不是一个非常常见的用例......你确实可以从你的容器连接到你的主机。

来自https://docs.docker.com/docker-for-mac/networking/

我想从容器连接到主机上的服务

主机有一个不断变化的 IP 地址(如果您没有网络访问权限,则没有)。从 18.03 开始​​,我们的建议是连接到特殊的 DNS 名称host.docker.internal,该名称解析为主机使用的内部 IP 地址。这是出于开发目的,不适用于 Docker Desktop for Mac 之外的生产环境。

  • 这是我使用 docker 版本 v19.03 的情况的解决方案,谢谢。 (2认同)

小智 6

供可能通过搜索找到此问题的人参考,从 Docker 20.10 及更高版本开始支持此功能。请参阅以下链接:

如何从docker容器访问主机端口

和:

https://github.com/docker/for-linux/issues/264#issuecomment-823528103