将 Kubernetes 抓取目标添加到不在 Kubernetes 中的 Prometheus 实例

Pau*_*est 6 target kubernetes prometheus

我在本地运行prometheus作为 http://localhost:9090/targets

docker run --name prometheus -d -p 127.0.0.1:9090:9090 prom/prometheus
Run Code Online (Sandbox Code Playgroud)

并希望将其连接到我们拥有的多个 Kubernetes(集群)实例。看看抓取是否有效,尝试Grafana 仪表板等。

然后我将在专门用于监控的专用服务器上执行相同的操作。然而,所有谷歌搜索都为我提供了所有不同的方法来配置一个 Kubernetes 实例中已有的 Prometheus,并且无法从外部 Kubernetes 读取指标。

如何将 Kubernetes 抓取目标添加到不在 Kubernetes 中的 Prometheus 实例?


我已阅读Kubernetes 指标来自何处并检查我的(第一个)Kubernetes 集群是否具有Metrics Server

kubectl get pods --all-namespaces | grep metrics-server 
Run Code Online (Sandbox Code Playgroud)

将 Prometheus 实例添加到每个 Kubernetes(集群)实例中绝对没有意义。一个 Prometheus 必须能够从许多 Kubernetes 集群及其中的每个节点读取指标。

PS 一些老问题的答案是在每个 Kubernetes 中安装 Prometheus,然后使用联合,这与我正在寻找的相反。

PPS 这对我来说也很奇怪,为什么来自 Cloud Native Foundation 的 #1 和 #2 项目 Kubernetes 和 Prometheus 没有简单的“在 Prometheus 中添加 Kubernetes 目标”按钮或简单的步骤。

ane*_*yte 3

在我看来,在每个集群中部署一个 Prometheus 实例是比组织外部访问更简单、更干净的方式。主要问题是发现的目标kubernetes_sd_configs是集群内部 DNS 名称和 IP 地址(或者至少在我的 AWS EKS 集群中是这样)。要解决并实现这些目标,您必须位于集群内部。

\n

这个问题可以通过使用代理来解决,因此下面的配置使用 API 服务器的代理端点来到达目标。我不确定它在大型集群中的性能,但在这种情况下,部署内部 Prometheus 实例是非常值得的。

\n

通过 API 服务器代理进行外部访问

\n

您需要的东西(对于每个集群):

\n
    \n
  1. 用于 HTTPS 工作的 API 服务器 CA 证书(请参阅下文如何获取它)。
  2. \n
  3. 具有适当权限的服务帐户令牌(取决于您的需求)。
  4. \n
\n

假设您已经有了这些,下面是一个 Prometheus 配置示例:

\n
- job_name: \'kubelet-cadvisor\'\n  scheme: https\n\n  kubernetes_sd_configs:\n  - role: node\n    api_server: https://api-server.example.com\n\n    # TLS and auth settings to perform service discovery\n    authorization:\n      credentials_file: /kube/token  # the file with your service account token\n    tls_config:\n      ca_file: /kube/CA.crt  # the file with the CA certificate\n\n  # The same as above but for actual scrape request.\n  # We\'re going to send scrape requests back to the API-server\n  # so the credentials are the same.\n  bearer_token_file: /kube/token\n  tls_config:\n    ca_file: /kube/CA.crt\n\n  relabel_configs:\n  # This is just to drop this long __meta_kubernetes_node_label_ prefix\n  - action: labelmap\n    regex: __meta_kubernetes_node_label_(.+)\n\n  # By default Prometheus goes to /metrics endpoint.\n  # This relabeling changes it to /api/v1/nodes/[kubernetes_io_hostname]/proxy/metrics/cadvisor\n  - source_labels: [kubernetes_io_hostname]\n    replacement: /api/v1/nodes/$1/proxy/metrics/cadvisor\n    target_label: __metrics_path__\n\n  # This relabeling defines that Prometheus should connect to the\n  # API-server instead of the actual instance. Together with the relabeling\n  # from above this will make the scrape request proxied to the node kubelet.\n  - replacement: api-server.example.com\n    target_label: __address__\n
Run Code Online (Sandbox Code Playgroud)\n

以上是为抓取量身定做的role: node。要使其与其他角色一起使用,您必须更改__metrics_path__标签。“手动构建 apiserver 代理 URL”一文可以帮助构建路径。

\n

如何获取API服务器CA证书

\n

有多种方法可以获取它,但kubeconfig在我看来最简单的是:

\n
\xe2\x9d\xaf kubectl config view --raw\napiVersion: v1\nclusters:\n- cluster:                      # you need this \xe2\xa4\x8b long value \n    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJ...\n    server: https://api-server.example.com\n  name: default\n...\n
Run Code Online (Sandbox Code Playgroud)\n

中的证书kubeconfig是 base64 编码的,因此您必须先对其进行解码才能使用:

\n
echo LS0tLS1CRUdJTiBDRVJUSUZJ... | base64 -d > CA.crt\n
Run Code Online (Sandbox Code Playgroud)\n