带有 Dockerfile 的普罗米修斯
Prometheus with Dockerfile
我有以下 Dockerfile:
FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/
和prometheus.yml:
global:
scrape_interval: 15s
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'prometheus'
metrics_path: /metrics
scrape_interval: 15s
static_configs:
- targets: ['localhost:9090']
- job_name: 'auth-service'
scrape_interval: 15s
metrics_path: /actuator/prometheus
static_configs:
- targets: ['localhost:8080']
并且 运行 使用以下命令:
docker build -t prometheus .
docker run -d -p 9090:9090 --rm prometheus
prometheus has status up
auth-service has status down (Get "http://localhost:8080/actuator/prometheus": dial tcp 127.0.0.1:8080: connect: connection refused)
我如何解决 auth-service 的问题,因为我可以从本地计算机从这个地址 http://localhost:8080/actuator/prometheus:
v.balun@macbook-vbalun Trainter-Prometheus % curl -X GET
http://localhost:8080/actuator/prometheus
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the
Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="heap",id="G1 Survivor Space",} 4194304.0
jvm_memory_committed_bytes{area="heap",id="G1 Old Gen",} 3.145728E7
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 3.0982144E7
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 2555904.0
jvm_memory_committed_bytes{area="heap",id="G1 Eden Space",} 2.7262976E7
jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 4325376.0
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 6291456.0
您遇到的问题似乎与 prometheus 无关,似乎是在 docker 网络级别。
在您的 prometheus
容器中,您是这样说的:
static_configs:
- targets: ['localhost:8080']
但请记住 localhost
现在不是您的物理主机(当您 运行 它位于 Docker 本地时,它现在是在容器内,在同一个容器内,很可能您没有服务 运行ning...
根据所提供的信息,我建议您执行以下操作:
- 先尝试
localhost
使用您的真实 IP,这取决于您用于容器的网络配置,这就足够了...
- 你可以用
localhost
你auth-service
的IP地址,这是docker给的,你可以运行一个docker inspect...
得到它。
- 如果#1 和#2 不起作用,并且如果
auth-service
在同一物理主机内的另一个容器中 运行ning,那么您可以使用桥接网络进行通信可能的容器,更多细节在这里:https://docs.docker.com/network/bridge/
一旦两个容器 运行ning 在同一个网络中,您可以使用容器名称来引用它而不是 localhost
,例如:
static_configs:
- targets: ['auth-service:8080']
我有以下 Dockerfile:
FROM prom/prometheus
ADD prometheus.yml /etc/prometheus/
和prometheus.yml:
global:
scrape_interval: 15s
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'prometheus'
metrics_path: /metrics
scrape_interval: 15s
static_configs:
- targets: ['localhost:9090']
- job_name: 'auth-service'
scrape_interval: 15s
metrics_path: /actuator/prometheus
static_configs:
- targets: ['localhost:8080']
并且 运行 使用以下命令:
docker build -t prometheus .
docker run -d -p 9090:9090 --rm prometheus
prometheus has status up
auth-service has status down (Get "http://localhost:8080/actuator/prometheus": dial tcp 127.0.0.1:8080: connect: connection refused)
我如何解决 auth-service 的问题,因为我可以从本地计算机从这个地址 http://localhost:8080/actuator/prometheus:
v.balun@macbook-vbalun Trainter-Prometheus % curl -X GET
http://localhost:8080/actuator/prometheus
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the
Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="heap",id="G1 Survivor Space",} 4194304.0
jvm_memory_committed_bytes{area="heap",id="G1 Old Gen",} 3.145728E7
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 3.0982144E7
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-nmethods'",} 2555904.0
jvm_memory_committed_bytes{area="heap",id="G1 Eden Space",} 2.7262976E7
jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 4325376.0
jvm_memory_committed_bytes{area="nonheap",id="CodeHeap 'non-profiled nmethods'",} 6291456.0
您遇到的问题似乎与 prometheus 无关,似乎是在 docker 网络级别。
在您的 prometheus
容器中,您是这样说的:
static_configs:
- targets: ['localhost:8080']
但请记住 localhost
现在不是您的物理主机(当您 运行 它位于 Docker 本地时,它现在是在容器内,在同一个容器内,很可能您没有服务 运行ning...
根据所提供的信息,我建议您执行以下操作:
- 先尝试
localhost
使用您的真实 IP,这取决于您用于容器的网络配置,这就足够了... - 你可以用
localhost
你auth-service
的IP地址,这是docker给的,你可以运行一个docker inspect...
得到它。 - 如果#1 和#2 不起作用,并且如果
auth-service
在同一物理主机内的另一个容器中 运行ning,那么您可以使用桥接网络进行通信可能的容器,更多细节在这里:https://docs.docker.com/network/bridge/ 一旦两个容器 运行ning 在同一个网络中,您可以使用容器名称来引用它而不是localhost
,例如:
static_configs:
- targets: ['auth-service:8080']