Prometheus在Docker容器(版本18.09.2,内部版本6247962 ,下面的docker-compose.xml
中运行)上,并且抓取目标位于由Python 3创建的localhost:8000
上脚本。
针对失败的抓取目标(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
使用
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())
答案 0 :(得分:3)
虽然不是很常见的用例,但是您确实可以从容器连接到主机。
来自https://docs.docker.com/docker-for-mac/networking/
我想从容器连接到主机上的服务
主机的IP地址正在更改(如果您没有网络,则没有IP地址) 访问)。从18.03开始,我们的建议是连接到 特殊的DNS名称 host.docker.internal ,解析为内部 主机使用的IP地址。这是出于发展目的,将 在Docker Desktop以外的生产环境中无法运行 Mac。
答案 1 :(得分:0)
供可能通过搜索找到此问题的人参考,现在从 Docker 20.10 及更高版本开始支持。见以下链接:
How to access host port from docker container
和:
https://github.com/docker/for-linux/issues/264#issuecomment-823528103