普罗米修斯只刮一个豆荚
Prometheus only scrapes one pod
我正在使用 Prometheus 从我的 pods 中抓取指标。我感兴趣的应用程序通过一项提供访问的服务被复制了几次。 Prometheus 使用此服务来抓取指标。在我的应用程序中,指标设置如下:
import * as Prometheus from 'prom-client';
const httpRequestDurationMicroseconds = new Prometheus.Histogram({
name: 'transaction_amounts',
help: 'Amount',
labelNames: ['amount'],
buckets: [0, 5, 15, 50, 100, 200, 300, 400, 500, 10000],
});
const totalPayments = new Prometheus.Counter('transaction_totals', 'Total payments');
我正在使用 helm 安装 Prometheus,抓取配置如下所示:
prometheus.yml:
rule_files:
- /etc/config/rules
- /etc/config/alerts
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
- job_name: transactions
scrape_interval: 1s
static_configs:
- targets:
- transaction-metrics-service:3001
我可以看到 prometheus 内部的指标,但它似乎只来自一个 pod。例如,在 Prometheus 中,当我查询 transaction_totals
时,它给出:
我认为 instance
标签不能唯一标识我的 pods。我应该怎么做才能查询所有pods?
为您的服务添加 prometheus 注释,因为 prom 只会抓取满足以下条件的服务:
- 公开导出端口
- 有
prometheus.io/scrape: "true"
注释
- 有
prometheus.io/port: "<exporter_port_here>"
注释
这是一个official example
刮掉的 pod 可能就是 prometheus 本身
与其使用仅抓取一台主机的 static_config
,不如尝试使用 Prometheus 提供的 kubernetes_sd_configs
Kubernetes 服务发现。
您的配置文件看起来像这样:
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
# only scrape when annotation prometheus.io/scrape: 'true' is set
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: (.+):(?:\d+);(\d+)
replacement: :
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: kubernetes_pod_name
然后将注释添加到您的 Kubernetes 部署 yaml 配置中,如下所示:
kind: Deployment
...
spec:
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "<< PORT OF YOUR CONTAINER >>"
你可以看到一个full working example here.
我正在使用 Prometheus 从我的 pods 中抓取指标。我感兴趣的应用程序通过一项提供访问的服务被复制了几次。 Prometheus 使用此服务来抓取指标。在我的应用程序中,指标设置如下:
import * as Prometheus from 'prom-client';
const httpRequestDurationMicroseconds = new Prometheus.Histogram({
name: 'transaction_amounts',
help: 'Amount',
labelNames: ['amount'],
buckets: [0, 5, 15, 50, 100, 200, 300, 400, 500, 10000],
});
const totalPayments = new Prometheus.Counter('transaction_totals', 'Total payments');
我正在使用 helm 安装 Prometheus,抓取配置如下所示:
prometheus.yml:
rule_files:
- /etc/config/rules
- /etc/config/alerts
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
- job_name: transactions
scrape_interval: 1s
static_configs:
- targets:
- transaction-metrics-service:3001
我可以看到 prometheus 内部的指标,但它似乎只来自一个 pod。例如,在 Prometheus 中,当我查询 transaction_totals
时,它给出:
我认为 instance
标签不能唯一标识我的 pods。我应该怎么做才能查询所有pods?
为您的服务添加 prometheus 注释,因为 prom 只会抓取满足以下条件的服务:
- 公开导出端口
- 有
prometheus.io/scrape: "true"
注释 - 有
prometheus.io/port: "<exporter_port_here>"
注释
这是一个official example
刮掉的 pod 可能就是 prometheus 本身
与其使用仅抓取一台主机的 static_config
,不如尝试使用 Prometheus 提供的 kubernetes_sd_configs
Kubernetes 服务发现。
您的配置文件看起来像这样:
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
# only scrape when annotation prometheus.io/scrape: 'true' is set
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
regex: (.+):(?:\d+);(\d+)
replacement: :
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)
- source_labels: [__meta_kubernetes_namespace]
action: replace
target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace
target_label: kubernetes_pod_name
然后将注释添加到您的 Kubernetes 部署 yaml 配置中,如下所示:
kind: Deployment
...
spec:
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "<< PORT OF YOUR CONTAINER >>"
你可以看到一个full working example here.