无法从 pods 抓取指标
Unable to scrape metrics from pods
我能够使用此 Prometheus 作业配置从 Kubernetes 服务中抓取 Prometheus 指标:
- job_name: 'prometheus-potapi'
static_configs:
- targets: ['potapi-service.potapi:1234']
它使用 Kubernetes DNS,它为我提供了我用于服务的三个 pods 中的任何一个的指标。
我想查看每个 pod 的结果。
我可以使用此配置查看我想要的数据:
- job_name: 'prometheus-potapi-pod'
static_configs:
- targets: ['10.1.0.126:1234']
我已经使用 Prometheus 中提供的服务发现机制进行了搜索和试验。不幸的是,我不明白应该如何设置。如果您不知道它是如何工作的,service discovery reference 就没有多大帮助。
我正在寻找一个例子,其中使用 IP 号的作业被一些服务发现机制所取代。指定 IP 足以让我看到我要查找的数据已公开。
pods 我想从同一命名空间 potapi
中的所有活动中抓取指标。
指标始终通过同一端口公开,1234
。
最后,都这样命名:
potapi-deployment-754d96f855-lkh4x
potapi-deployment-754d96f855-pslgg
potapi-deployment-754d96f855-z2zj2
当我做
kubectl describe pod potapi-deployment-754d96f855-pslgg -n potapi
我得到这个描述:
Name: potapi-deployment-754d96f855-pslgg
Namespace: potapi
Node: docker-for-desktop/192.168.65.3
Start Time: Tue, 07 Aug 2018 14:18:55 +0200
Labels: app=potapi
pod-template-hash=3108529411
Annotations: <none>
Status: Running
IP: 10.1.0.127
Controlled By: ReplicaSet/potapi-deployment-754d96f855
Containers:
potapi:
Container ID: docker://72a0bafbda9b82ddfc580d79488a8e3c480d76a6d17c43d7f7d7ab18458c56ee
Image: potapi-service
Image ID: docker://sha256:d64e94c2dda43c40f641008c122e6664845d73cab109768efa0c3619cb0836bb
Ports: 4567/TCP, 4568/TCP, 1234/TCP
Host Ports: 0/TCP, 0/TCP, 0/TCP
State: Running
Started: Tue, 07 Aug 2018 14:18:57 +0200
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4fttn (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-4fttn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4fttn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
如果有这些先决条件,您将如何重写工作定义?
如果不是 /metrics
,他们在这里使用 example.io/scrape=true
(and similar annotations for specifying the scrape port and the scrape path),这就是实现 "autodiscovery" 部分的方式。
如果您将该注释以及 Prom 配置中的相关配置片段应用到 Service
,那么 Prom 将抓取 Service
上的端口和路径,这意味着您将有 Service
本身的统计数据,而不是它背后的各个端点。同样,如果您标记 Pod
s,您将收集 Pod
s 的指标,但需要汇总它们以获得事态的交叉 Pod
视图。可以自动发现多种不同的资源类型,包括 node and ingress。他们的行为都相似。
除非您对 Prom 实例有严重的 CPU 或存储问题,否则我绝对不会像那样在配置中枚举抓取目标:我会使用抓取注释,这意味着您可以更改谁是抓取,什么端口等,无需每次都重新配置 Prom。
请注意,如果您想按原样使用他们的示例,并且想从 kubernetes 资源 YAML 中应用这些注释,请确保引用 : 'true'
值,否则 YAML 会提升该值为布尔字面量,kubernetes注解只能为字符串值。
从命令行应用注释就可以了:
kubectl annotate pod -l app=potapi example.io/scrape=true
(顺便说一句,他们在示例中使用 example.io/
,但是该字符串没有什么特别之处,只是它为 scrape
部分命名空间以防止它与其他名为 [=21= 的东西发生冲突]。所以,如果你想避免在你的集群中有一些奇怪的名字 example.io/
,请随意使用你的组织的命名空间)
我最终得到了这个解决方案:
...
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__address__]
action: replace
regex: ([^:]+)(?::\d+)?
replacement: :1234
target_label: __address__
...
有两部分。
检查值为 'true'
的注释 prometheus.io/scrape
。它是在第一个 source_labels
中完成的。
prometheus_io_scrape
转换为 prometheus.io/scrape
可能不是不言而喻的
获取地址并将所需的端口添加到其中。它是在第二个 source_labels
上完成的。 __address__
源将被查询主机名或 ip 号。在本例中,使用神秘的正则表达式 ([^:]+)(?::\d+)?
找到了一个 ip 号码。我要使用的端口是“1234”,所以我将其硬编码为 replacement:
结果是
__address__
现在将包含带有端口 1234
的 pod 的 ip,格式为 10.1.0.172:1234
,其中 10.1.0.172
是找到的 ip 编号。
在 Prometheus 中使用此配置,我应该能够找到带有正确注释的 pods。
那么注解应该加在哪里呢?我最终将它添加到我的 Kubernetes 部署模板描述中。
完整的部署描述如下所示:
apiVersion: apps/v1
kind: Deployment
metadata:
name: potapi-deployment
namespace: potapi
labels:
app: potapi
spec:
replicas: 3
selector:
matchLabels:
app: potapi
template:
metadata:
annotations:
prometheus.io/scrape: 'true'
labels:
app: potapi
spec:
containers:
- name: potapi
image: potapi-service
imagePullPolicy: IfNotPresent
ports:
- containerPort: 4567
name: service
- containerPort: 1234
name: metrics
在template
部分添加了有趣的注释
我能够使用此 Prometheus 作业配置从 Kubernetes 服务中抓取 Prometheus 指标:
- job_name: 'prometheus-potapi'
static_configs:
- targets: ['potapi-service.potapi:1234']
它使用 Kubernetes DNS,它为我提供了我用于服务的三个 pods 中的任何一个的指标。
我想查看每个 pod 的结果。
我可以使用此配置查看我想要的数据:
- job_name: 'prometheus-potapi-pod'
static_configs:
- targets: ['10.1.0.126:1234']
我已经使用 Prometheus 中提供的服务发现机制进行了搜索和试验。不幸的是,我不明白应该如何设置。如果您不知道它是如何工作的,service discovery reference 就没有多大帮助。
我正在寻找一个例子,其中使用 IP 号的作业被一些服务发现机制所取代。指定 IP 足以让我看到我要查找的数据已公开。
pods 我想从同一命名空间 potapi
中的所有活动中抓取指标。
指标始终通过同一端口公开,1234
。
最后,都这样命名:
potapi-deployment-754d96f855-lkh4x
potapi-deployment-754d96f855-pslgg
potapi-deployment-754d96f855-z2zj2
当我做
kubectl describe pod potapi-deployment-754d96f855-pslgg -n potapi
我得到这个描述:
Name: potapi-deployment-754d96f855-pslgg
Namespace: potapi
Node: docker-for-desktop/192.168.65.3
Start Time: Tue, 07 Aug 2018 14:18:55 +0200
Labels: app=potapi
pod-template-hash=3108529411
Annotations: <none>
Status: Running
IP: 10.1.0.127
Controlled By: ReplicaSet/potapi-deployment-754d96f855
Containers:
potapi:
Container ID: docker://72a0bafbda9b82ddfc580d79488a8e3c480d76a6d17c43d7f7d7ab18458c56ee
Image: potapi-service
Image ID: docker://sha256:d64e94c2dda43c40f641008c122e6664845d73cab109768efa0c3619cb0836bb
Ports: 4567/TCP, 4568/TCP, 1234/TCP
Host Ports: 0/TCP, 0/TCP, 0/TCP
State: Running
Started: Tue, 07 Aug 2018 14:18:57 +0200
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4fttn (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-4fttn:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4fttn
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
如果有这些先决条件,您将如何重写工作定义?
如果不是 /metrics
,他们在这里使用 example.io/scrape=true
(and similar annotations for specifying the scrape port and the scrape path),这就是实现 "autodiscovery" 部分的方式。
如果您将该注释以及 Prom 配置中的相关配置片段应用到 Service
,那么 Prom 将抓取 Service
上的端口和路径,这意味着您将有 Service
本身的统计数据,而不是它背后的各个端点。同样,如果您标记 Pod
s,您将收集 Pod
s 的指标,但需要汇总它们以获得事态的交叉 Pod
视图。可以自动发现多种不同的资源类型,包括 node and ingress。他们的行为都相似。
除非您对 Prom 实例有严重的 CPU 或存储问题,否则我绝对不会像那样在配置中枚举抓取目标:我会使用抓取注释,这意味着您可以更改谁是抓取,什么端口等,无需每次都重新配置 Prom。
请注意,如果您想按原样使用他们的示例,并且想从 kubernetes 资源 YAML 中应用这些注释,请确保引用 : 'true'
值,否则 YAML 会提升该值为布尔字面量,kubernetes注解只能为字符串值。
从命令行应用注释就可以了:
kubectl annotate pod -l app=potapi example.io/scrape=true
(顺便说一句,他们在示例中使用 example.io/
,但是该字符串没有什么特别之处,只是它为 scrape
部分命名空间以防止它与其他名为 [=21= 的东西发生冲突]。所以,如果你想避免在你的集群中有一些奇怪的名字 example.io/
,请随意使用你的组织的命名空间)
我最终得到了这个解决方案:
...
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__address__]
action: replace
regex: ([^:]+)(?::\d+)?
replacement: :1234
target_label: __address__
...
有两部分。
检查值为
'true'
的注释prometheus.io/scrape
。它是在第一个source_labels
中完成的。prometheus_io_scrape
转换为prometheus.io/scrape
可能不是不言而喻的
获取地址并将所需的端口添加到其中。它是在第二个
source_labels
上完成的。__address__
源将被查询主机名或 ip 号。在本例中,使用神秘的正则表达式([^:]+)(?::\d+)?
找到了一个 ip 号码。我要使用的端口是“1234”,所以我将其硬编码为replacement:
结果是__address__
现在将包含带有端口1234
的 pod 的 ip,格式为10.1.0.172:1234
,其中10.1.0.172
是找到的 ip 编号。
在 Prometheus 中使用此配置,我应该能够找到带有正确注释的 pods。
那么注解应该加在哪里呢?我最终将它添加到我的 Kubernetes 部署模板描述中。
完整的部署描述如下所示:
apiVersion: apps/v1
kind: Deployment
metadata:
name: potapi-deployment
namespace: potapi
labels:
app: potapi
spec:
replicas: 3
selector:
matchLabels:
app: potapi
template:
metadata:
annotations:
prometheus.io/scrape: 'true'
labels:
app: potapi
spec:
containers:
- name: potapi
image: potapi-service
imagePullPolicy: IfNotPresent
ports:
- containerPort: 4567
name: service
- containerPort: 1234
name: metrics
在template
部分添加了有趣的注释