【发布时间】:2019-11-16 11:47:57
【问题描述】:
Prometheus 在 docker 容器内运行(版本 18.09.2,内部版本 6247962,docker-compose.xml 下面),并且抓取目标位于由 Python 3 脚本创建的 localhost:8000。 p>
为失败的抓取目标 (localhost:9090/targets) 获得的错误是
获取http://127.0.0.1:8000/metrics:拨打tcp 127.0.0.1:8000:getsockopt:连接被拒绝
问题: 为什么 docker 容器中的 Prometheus 无法抓取主机(Mac OS X)上运行的目标?我们如何让 Prometheus 在 docker 容器中运行能够抓取主机上运行的目标?
尝试失败:尝试替换docker-compose.yml
networks:
- back-tier
- front-tier
与
network_mode: "host"
但随后我们无法访问位于localhost:9090 的 Prometheus 管理页面。
无法从类似问题中找到解决方案
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
prometheus.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']
在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
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())
【问题讨论】:
-
您是否尝试连接到
host.docker.internal:8000? -
另外,请发布您的 docker 版本。
-
@AlexandreJuma 更新了版本 18.09.2,内部版本 6247962 的帖子。我尝试在浏览器中访问
host.docker.internal:8000,但它显示错误 ERR_NAME_NOT_RESOLVED -
我在问你是否试图从 prometheus 容器中抓取
host.docker.internal:8000。 -
@AlexandreJuma 是的,你是对的,从 pormetheus 容器中抓取
host.docker.internal:8000! prometheus 能否访问外部 IP 地址如http://google.com:8000/metrics进行抓取?
标签: docker docker-compose prometheus