使用prometheus对kubernetes容器的HTTP和HTTPS端口的刮取度量
我们希望我们的普罗米修斯装置能够在一个吊舱内刮取两个容器的指标。 一个容器通过端口443的HTTPS公开度量,而另一个容器通过端口8080的HTTP公开度量。两个容器在同一路径上提供度量,即使用prometheus对kubernetes容器的HTTP和HTTPS端口的刮取度量,kubernetes,prometheus,Kubernetes,Prometheus,我们希望我们的普罗米修斯装置能够在一个吊舱内刮取两个容器的指标。 一个容器通过端口443的HTTPS公开度量,而另一个容器通过端口8080的HTTP公开度量。两个容器在同一路径上提供度量,即/metrics 如果我们将prometheus.io/scheme声明为http或https,则只会刮取一个容器。对于另一个,我们总是收到:服务器返回HTTP状态400错误请求 如果我们根本不定义prometheus.io/scheme,也会发生同样的情况。然后,普罗米修斯将对这两个端口使用http,而在端
/metrics
如果我们将prometheus.io/scheme声明为http或https,则只会刮取一个容器。对于另一个,我们总是收到:服务器返回HTTP状态400错误请求
如果我们根本不定义prometheus.io/scheme,也会发生同样的情况。然后,普罗米修斯将对这两个端口使用http,而在端口443公开度量的容器将失败,因为它只希望HTTPS请求
有没有办法告诉普罗米修斯,在我们的部署中,它应该如何准确地刮取各个容器?获取两个容器的指标的可行解决方法是什么?
版本
库伯内特斯:1.10.2
普罗米修斯:2.2.1
部署摘录
普罗米修斯构型:
我发现了一个GIST片段,如果端口名为“metrics”,则直接从容器中获取该端口,而不是依赖于每个pod的注释。它还包含一条注释,使其成为任何以“metrics”开头的端口的正则表达式 也许您可以对其进行扩展,以从端口名中提取模式,如“metrics http”和“metrics https”
我发现了一个GIST片段,如果端口名为“metrics”,则直接从容器中获取该端口,而不是依赖于每个pod的注释。它还包含一条注释,使其成为任何以“metrics”开头的端口的正则表达式 也许您可以对其进行扩展,以从端口名中提取模式,如“metrics http”和“metrics https”
您不仅限于在部署清单中添加prometheus注释。事实上,我将它们添加到kubernetes服务清单中
这意味着您可以添加两个服务,一个用于container-1,另一个用于container-2。比思将被普罗米修斯删除您不仅限于在部署清单中添加普罗米修斯注释。事实上,我将它们添加到kubernetes服务清单中
这意味着您可以添加两个服务,一个用于container-1,另一个用于container-2。比思将被普罗米修斯刮伤您是否考虑过使用侧车容器(如
haproxy
)来消除两个当前容器之间的协议不平衡?我认为你可以命名侧车容器container-1
,将当前的container-1
更改为其他名称,然后(从普罗米修斯的PoV)度量将以正确的名称出现,只有你知道这个诡计。我在中没有看到任何允许您所描述的细粒度控制的内容。我们可以这样做,事实上,在我们的大多数场景中,两个容器中的一个已经是卸载TLS流量的侧车。或多或少,我们已经有了一个解决方案,可以完全做到这一点,并从443中删除所有指标。不过,我们还有其他星座,希望避免只部署另一个侧车。您是否考虑过使用侧车容器(如haproxy
)来消除当前两个容器之间的协议不平衡?我认为你可以命名侧车容器container-1
,将当前的container-1
更改为其他名称,然后(从普罗米修斯的PoV)度量将以正确的名称出现,只有你知道这个诡计。我在中没有看到任何允许您所描述的细粒度控制的内容。我们可以这样做,事实上,在我们的大多数场景中,两个容器中的一个已经是卸载TLS流量的侧车。或多或少,我们已经有了一个解决方案,可以完全做到这一点,并从443中删除所有指标。我们仍然有其他星座,我们希望避免部署另一个侧车
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
- 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
# 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