抓取配置与 relabeling
目标
从头到尾编写您自己的 Prometheus scrape 配置,看看它如何在真实集群中选择 Pod。完成后,您可以查看其他人的 prometheus.yml 并了解哪些目标幸存以及哪些指标被丢弃。
为什么它很重要?
抓取设置是普罗米修斯中唯一由您决定“看什么”的地方。所有后续查询和通知将仅涵盖在此之后幸存的数据。中继感觉困难的原因不是因为语法,而是因为这两个步骤在不同的时间运行。 relabel_configs 处理抓取之前的目标列表,metric_relabel_configs 处理抓取之后传入的指标。上一步中丢弃的目标的成本为0,因为请求本身并未发送,而后续步骤中丢弃的指标已经产生了网络和解析成本。这种差异可能意味着大集群中普罗米修斯的生死。护栏也是在同样的背景下。如果没有 sample_limit,基数爆炸的一项服务将停止监控整个服务。
步骤
1、创建/root/pca-scrape/prometheus.yml并写入global块。 scrape_interval: 15s、evaluation_interval: 30s、scrape_timeout: 10s,并在external_labels下设置cluster: homelab。
2、在scrape_configs列表的第项中创建job_name: prometheus-self,并在metrics_path: /metrics和static_configs中为targets添加一个localhost:9090。
3、在第二项中创建job_name: checkout-api,并输入scrape_interval: 30s、scrape_timeout: 20s、sample_limit: 20000、label_limit: 24、label_value_length_limit: 256。
4、在第三项中创建job_name: kubernetes-pods,在kubernetes_sd_configs中指定role: pod,然后在namespaces.names中只输入pca-scrape,以缩小监控范围。
5. 在第三个作业的relabel_configs 中插入三个规则。 (a) action: keep、source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]、regex: "true" (b) action: replace、source_labels: [__meta_kubernetes_pod_ip, __meta_kubernetes_pod_annotation_prometheus_io_port]、target_label: __address__、replacement: '$1:$2' (c) action: replace、source_labels: [__meta_kubernetes_namespace]、target_label: namespace。
6. 将规则action: labelmap和regex: __meta_kubernetes_pod_label_(.+)添加到同一个作业中,并将两条规则放入metric_relabel_configs中。 (a) action: drop、source_labels: [__name__]、regex: 'go_gc_duration_seconds.*|python_gc_.*' (b) action: replace、source_labels: [__name__]、regex: 'http_requests_total'、target_label: user_id、replacement: ''。
7. 创建命名空间 pca-scrape 并在其中创建 configmap prometheus-config。键名是prometheus.yml,值是你刚刚写入的文件的内容。
8. 在命名空间 pca-scrape 中创建 Pod checkout-api。标签app: checkout-api,注释prometheus.io/scrape: "true"、prometheus.io/port: "8080"、prometheus.io/path: "/metrics",集装箱端口命名为metrics至containerPort: 8080。
注意
-kubectl create configmap prometheus-config -n pca-scrape --from-file=prometheus.yml=/root/pca-scrape/prometheus.yml
- 通过将清单应用为
kubectl apply -f,可以更轻松地向 pod 添加注释。您可以使用任何图像。 - 常见错误1:将
source_labels写成单个字符串。它始终是一个列表。 - 常见错误2:使用注释元标签名称为
prometheus.io/scrape。点和斜杠变成下划线。 - 常见错误3:只修改文件而不重新创建configmap。步骤7 Grading读取ConfigMap中的内容。
写入全局块
创建/root/pca-scrape/prometheus.yml并写入global块。 scrape_interval: 15s、evaluation_interval: 30s、scrape_timeout: 10s,并在external_labels下设置cluster: homelab。
将 scrape_interval、evaluation_interval 和 scrape_timeout 放在 prometheus.yml 顶部的 global 下。 external_labels 是附加到 Prometheus 导出的所有时间序列的标签,用于区分联邦或远程存储中的实例。
添加 static_configs 作业
在scrape_configs列表中创建job_name: prometheus-self作为第项,并在metrics_path: /metrics和static_configs的targets中放置一个localhost:9090。
scrape_configs 是一个列表。将 job_name 和 static_configs 放在第一项中。 static_configs 是带有目标列表的项目列表,两次用方括号括起来。 metrics_path 有一个默认值,但在此处指定。
护栏吊挂作业
在第二个条目中,创建 job_name: checkout-api 并添加 scrape_interval: 30s、scrape_timeout: 20s、sample_limit: 20000、label_limit: 24 和 label_value_length_limit: 256。
Sample_limit是一个目标提供的样本数量上限,如果超过该上限,则抓取将完全失败。 label_limit 和 label_value_length_limit 也是同一类型的防线。请记住 scrape_timeout 不能大于 scrape_interval 的约束。
使用 kubernetes_sd 查找 Pod
在第三项中创建job_name: kubernetes-pods,在kubernetes_sd_configs中指定role: pod,并通过在namespaces.names中仅添加pca-scrape来缩小监视范围。
kubernetes_sd_configs 也是一个列表。 role 是 node/service/pod/endpoints/endpointslice/ingress 之一,或者 pod(如果您想直接抓取 pod 的容器端口)。将监视范围缩小到namespaces.names 可以显着减少大型集群上的SD 负载。
选择保留并重新组合地址
将三个规则插入到第三个作业的 relabel_configs 中。 (a) action: keep、source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]、regex: "true" (b) action: replace、source_labels: [__meta_kubernetes_pod_ip, __meta_kubernetes_pod_annotation_prometheus_io_port]、target_label: __address__、replacement: '$1:$2' (c) action: replace、source_labels: [__meta_kubernetes_namespace]、target_label: namespace。
keep 丢弃“不”匹配正则表达式的目标。请记住注释元标签名称将点和斜线替换为下划线的规则。如果编写两个源标签,则值在正则表达式中用分号分隔,并且替换将它们重新组装成捕获组。
labelmap 和 metric_relabel_configs
将规则 action: labelmap 和 regex: __meta_kubernetes_pod_label_(.+) 添加到同一个作业,并在 metric_relabel_configs 中放置两条规则。 (a) action: drop、source_labels: [__name__]、regex: 'go_gc_duration_seconds.*|python_gc_.*' (b) action: replace、source_labels: [__name__]、regex: 'http_requests_total'、target_label: user_id、replacement: ''。
relabel_configs 在抓取之前处理目标,metric_relabel_configs 在抓取之后处理指标。 labeldrop 仅匹配标签“name”,并将其从作业的所有指标中删除,因此,如果您只想从特定指标中删除它,请使用替换输入空值。空值标签相当于空标签。
将设置上传到 ConfigMap
创建命名空间 pca-scrape 并在其中创建配置映射 prometheus-config。键名是prometheus.yml,值是你刚刚写入的文件的内容。
kubectl create configmap 中的 --from-file 允许您以 키=경로 形式指定密钥名称。修改文件后,您必须重新创建 ConfigMap 才能反映它。在实际操作中,config-reloader sidecar 将检测到此更新并触发 Prometheus 中的重新加载。
创建一个将由 keep 规则保存的 pod
在命名空间 pca-scrape 中创建 Pod checkout-api。标签app: checkout-api,注释prometheus.io/scrape: "true"、prometheus.io/port: "8080"、prometheus.io/path: "/metrics",容器端口命名为metrics至containerPort: 8080。
设置注解值的同时考虑前面写的keep规则和__address__重组规则是否会通过这个pod。该值必须完全等于字符串 true ,并且端口注释值必须与容器端口匹配。 labelmap 规则还添加一个要移动的 Pod 标签。