【问题标题】:How do you add scrape targets to a Prometheus server that was installed with Kubernetes-Helm?如何将抓取目标添加到使用 Kubernetes-Helm 安装的 Prometheus 服务器?
【发布时间】:2018-01-18 16:47:54
【问题描述】:

背景

我已经使用Helm chart for Prometheus 在我的 Kubernetes 集群(托管在 Google Container Engineer 上)上安装了 Prometheus。

问题

我不知道如何将抓取目标添加到 Prometheus 服务器。 prometheus.io 站点描述了我如何将 prometheus.yml 文件(其中包含抓取目标列表)安装到 Prometheus Docker 容器——我已经在本地完成了这个并且它可以工作。但是,我不知道如何为通过 Kubernetes-Helm 安装的 Prometheus 设置指定抓取目标。我是否需要向包含抓取目标的 Prometheus 服务器 pod 添加一个卷,从而更新 Helm 生成的 YAML 文件??

我也不清楚如何在 Kubernetes Pod 中公开指标——我需要转发特定端口吗?

【问题讨论】:

    标签: kubernetes google-kubernetes-engine prometheus kubernetes-helm


    【解决方案1】:

    您需要为要监控的服务添加注解。

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        prometheus.io/scrape: 'true'
    

    来自图表中的prometheus.yml:

    • prometheus.io/scrape:只抓取值为true的服务
    • prometheus.io/scheme:http 或 https
    • prometheus.io/path:如果指标路径不是/metrics,则覆盖
    • prometheus.io/port:如果指标暴露在不同的端口上

    是的,您需要向服务公开带有指标的端口,以便 Prometheus 可以访问它

    【讨论】:

    • 请原谅我的无知,但是您在哪里找到 prometheus.yml 文件?我在 GitHub 页面上找不到图表的任何内容。另外,我可以将容器端口转发到服务,但是是否有特定的端口要公开?以及如何将 Prometheus 指向该端口?
    • 它在图表值文件中 - github.com/kubernetes/charts/blob/master/stable/prometheus/… 关于端口。如果您的服务公开指标,它要么使用单独的端口(sidecar 模式),要么与您的应用程序相同。你到底想监控什么?
    • 啊,我明白了。如果我只是在与服务本身相同的端口上公开指标,似乎我不必指定端口。
    • 就是这样吗? prometheus 什么时候应该识别新服务?我将注释添加到我的服务中,但 prometheus 没有注册它。
    • 使用默认 helm chart 部署 prometheus 就足够了。服务应该出现在your-prometheus-url/targets
    【解决方案2】:

    首先,你需要创建一个Service Monitor,这是一个自定义的K8s资源。只需在 manifests 文件夹中创建一个servicemonitor.yaml

    由于在 K8s 上部署时,我们无法访问 Prometheus.yaml 文件来提及目标,因此我们创建了 servicemonitor,它反过来将目标添加到 Prometheus.yaml 文件中的 scrap_config 中。您可以从here 了解更多信息。

    这是一个示例 servicemonitor.yaml 文件,用于在 Prometheus 中公开 Flask App 指标。

    apiVersion: monitoring.coreos.com/v1 
    kind: ServiceMonitor 
    metadata:
      name: flask-metrics
      namespace: prometheus # namespace where prometheus is running
      labels:
        app: flask-app
        release: prom  # name of the release 
        # ( VERY IMPORTANT: You need to know the correct release name by viewing 
        # the servicemonitor of Prometheus itself: Without the correct name, 
        #  Prometheus cannot identify the metrics of the Flask app as the target.)
    spec:
      selector:
        matchLabels:
          # Target app service
          app: flask-app # same as above
          release: prom # same as above
      endpoints:
      - interval: 15s # scrape interval
        path: /metrics # path to scrape
        port: http # named port in target app
      namespaceSelector:
        matchNames:
        - flask # namespace where the app is running
    
    

    还要将此发布标签添加到服务和部署文件的元数据和规范部分。

    如果您遇到 Prometheus 显示目标而不显示端点的情况,请查看:https://github.com/prometheus-operator/prometheus-operator/issues/3053

    一些有用的链接:

    【讨论】:

      猜你喜欢
      • 2021-06-26
      • 2022-01-24
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 2018-04-23
      • 2021-02-04
      • 2019-10-31
      • 1970-01-01
      相关资源
      最近更新 更多