gpt4 book ai didi

kubernetes - 使用HTTP和HTTPS端口的Prometheus刮刮kubernetes容器的指标

转载 作者:行者123 更新时间:2023-12-02 11:58:33 26 4
gpt4 key购买 nike

我们希望我们的Prometheus安装能够刮取Pod中两个容器的度量。
一个容器通过HTTPS在端口443公开度量,而另一个容器通过HTTP在端口8080公开度量。两个容器在同一路径(即/metrics)提供度量。

如果我们将prometheus.io/scheme声明为http或https,则将仅废弃一个容器。对于另一个,我们总是会收到:server returned HTTP status 400 Bad Request如果我们根本不定义prometheus.io/scheme,也会发生同样的情况。然后,Prometheus将对两个端口都使用http,而对于在端口443上公开指标的容器将失败,因为它只希望HTTPS请求。

有没有办法告诉普罗米修斯在我们的部署中如何精确地刮除各个容器?有什么可行的解决方法来获取两个容器的指标?

版本号

Kubernetes:1.10.2

普罗米修斯:2.2.1

部署摘录

apiVersion: apps/v1
kind: Deployment
metadata:
name: xxx
namespace: xxx
spec:
selector:
matchLabels:
app: xxx
template:
metadata:
labels:
app: xxx
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/metrics"
spec:
containers:
- name: container-1
image: xxx
ports:
- containerPort: 443
- name: container-2
image: xxx
ports:
- containerPort: 8080

Prometheus配置:
- job_name: kubernetes-pods
scrape_interval: 1m
scrape_timeout: 10s
metrics_path: /metrics
scheme: http
kubernetes_sd_configs:
- api_server: null
role: pod
namespaces:
names: []
relabel_configs:
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
separator: ;
regex: "true"
replacement: $1
action: keep
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
separator: ;
regex: (.+)
target_label: __metrics_path__
replacement: $1
action: replace
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
separator: ;
regex: ([^:]+)(?::\d+)?;(\d+)
target_label: __address__
replacement: $1:$2
action: replace
- separator: ;
regex: __meta_kubernetes_pod_label_(.+)
replacement: $1
action: labelmap
- source_labels: [__meta_kubernetes_namespace]
separator: ;
regex: (.*)
target_label: kubernetes_namespace
replacement: $1
action: replace
- source_labels: [__meta_kubernetes_pod_name]
separator: ;
regex: (.*)
target_label: kubernetes_pod_name
replacement: $1
action: replace

最佳答案

我发现了一个GIST代码段,如果它被命名为“metrics”,它将直接从容器中获取端口,而不是依赖于每个容器的注释。它还包含注释,以使其成为任何以“metrics”开头的端口的正则表达式。

也许您可以对其进行扩展以从端口名称中提取模式,例如“metrics-http”和“metrics-https”。

https://gist.github.com/bakins/5bf7d4e719f36c1c555d81134d8887eb

# Example scrape config for pods
#
# The relabeling allows the actual pod scrape endpoint to be configured via the
# following annotations:
#
# * `prometheus.io/scrape`: Only scrape pods that have a value of `true`
# * `prometheus.io/path`: If the metrics path is not `/metrics` override this. This
# will be the same for every container in the pod that is scraped.
# * this will scrape every container in a pod with `prometheus.io/scrape` set to true and the
port is name `metrics` in the container
# * note `prometheus.io/port` is no longer honored. You must name the port(s) to scrape `metrics`
# Also, in some of the issues I read, there was mention of a container role, but I couldn't get
# that to work - or find any more info on it.
- 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: [__meta_kubernetes_pod_container_port_name]
action: keep
regex: metrics
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace
target_label: __metrics_path__
regex: (.+)
- source_labels: [ __address__, __meta_kubernetes_pod_container_port_number]
action: replace
regex: (.+):(?:\d+);(\d+)
replacement: ${1}:${2}
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 - 使用HTTP和HTTPS端口的Prometheus刮刮kubernetes容器的指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com