【问题标题】:Add new service metrics to prometheus-operator向 prometheus-operator 添加新的服务指标
【发布时间】:2020-08-26 22:02:04
【问题描述】:

我正在通过 Helm 图表将 Prometheus-operator 部署到我的集群,但我实现了一个自定义服务来监控我的应用程序,我需要将我的服务添加到 Prometheus-operator 以查看我的指标数据。

我该怎么做?

【问题讨论】:

    标签: kubernetes microservices prometheus kubernetes-helm prometheus-operator


    【解决方案1】:

    首先需要通过 Helm 或手动部署 Prometheus-operator:

    # By Helm:
    $ helm install stable/prometheus-operator --generate-name
     
    # By manual: for release `release-0.41`
    kubectl apply -f  https://raw.githubusercontent.com/prometheus-operator/prometheus-operator/release-0.41/bundle.yaml
    

    如果您的集群启用了 RBAC,那么您需要为 Prometheus 对象安装 RBAC 东西:

    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRole
    metadata:
      name: prometheus
    rules:
    - apiGroups: [""]
      resources:
      - nodes
      - nodes/metrics
      - services
      - endpoints
      - pods
      verbs: ["get", "list", "watch"]
    - apiGroups: [""]
      resources:
      - configmaps
      verbs: ["get"]
    - nonResourceURLs: ["/metrics"]
      verbs: ["get"]
    ---
    apiVersion: v1
    kind: ServiceAccount
    metadata:
      name: prometheus
      namespace: default
    ---
    apiVersion: rbac.authorization.k8s.io/v1beta1
    kind: ClusterRoleBinding
    metadata:
      name: prometheus
    roleRef:
      apiGroup: rbac.authorization.k8s.io
      kind: ClusterRole
      name: prometheus
    subjects:
    - kind: ServiceAccount
      name: prometheus
      namespace: default
    

    那么你需要部署Promethues对象:

    apiVersion: monitoring.coreos.com/v1
    kind: Prometheus
    metadata:
      name: prometheus
      labels:
        prometheus: prometheus
    spec:
      replicas: 1
      serviceAccountName: prometheus
      serviceMonitorSelector:
        matchLabels:
          k8s-app: prometheus
      serviceMonitorNamespaceSelector:
        matchLabels:
          prometheus: prometheus
      resources:
        requests:
          memory: 400Mi
    

    这里,Prometheus 对象将选择所有满足以下条件的ServiceMonitor

    • ServiceMonitor 将具有 k8s-app: prometheus 标签。
    • ServiceMonitor 将在具有 prometheus: prometheus 标签的命名空间中创建。

    ServiceMonitor 有一个标签选择器来选择服务及其底层端点对象。示例应用程序的 Service 对象通过具有 example-app 值的 app 标签选择 Pod。 Service 对象还指定了公开指标的端口。

    kind: Service
    apiVersion: v1
    metadata:
      name: example-app
      labels:
        app: example-app
    spec:
      selector:
        app: example-app
      ports:
      - name: web
        port: 8080
    

    这个 Service 对象是由一个 ServiceMonitor 发现的,它以同样的方式选择。 app 标签的值必须为 example-app

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      name: example-app
      labels:
        k8s-app: prometheus
    spec:
      selector:
        matchLabels:
          app: example-app
      namespaceSelector:
        # matchNames:
        # - demo
        any: true
      endpoints:
      - port: web
    

    这里,namespaceSelector 用于选择创建服务的所有命名空间。您可以使用 matchNames 指定特定的任何命名空间。

    您还可以根据需要在任何命名空间中创建ServiceMonitor。但是需要在Prometheuscr的spec中指定,比如:

      serviceMonitorNamespaceSelector:
        matchLabels:
          prometheus: prometheus
    

    上面的serviceMonitorNamespaceSelector 用于Prometheus 操作符来选择标签为prometheus: prometheus 的命名空间。假设您有一个命名空间demo,并且在这个demo 命名空间中创建了一个Prometheus,那么您需要使用补丁在demo 命名空间中添加标签prometheus: prometheus

    $ kubectl patch namespace demo -p '{"metadata":{"labels": {"prometheus":"prometheus"}}}'
    

    您可以在此处找到更多详细信息:

    【讨论】:

    • 谢谢,如果我的 Prometheus 位于不同的命名空间,而我的服务位于默认命名空间,我会做些什么更改?
    • 我已经更新了我的答案并添加了一个新的参考。你也许会从上面的答案中找到你的答案。
    • 谢谢,但现在在应用您的解决方案 Prometheus Service Discovery get >>>monitoring/sg-prometheus-rabbitmq-exporter/0 (0/16 活动目标) - 这是我的服务和数据不显示在普罗米修斯上
    • 我的服务是 Selector:app=prometheus-rabbitmq-exporter,release=sg-prometheus-rabbitmq-exporter 类型:ClusterIP IP:10.3.144.40 端口:rabbitmq-exporter 9419/TCP TargetPort:publish/ TCP 端点:10.2.0.47:9419 会话关联性:无
    • + 我的 ServiceMonitor 是 apiVersion:monitoring.coreos.com/v1 种类:ServiceMonitor 元数据:名称:sg-prometheus-rabbitmq-exporter 标签:k8s-app:prometheus 发布:sg-prometheus-operator spec :端点:- 路径:/metrics 端口:网络选择器:matchLabels:应用程序:prometheus-rabbitmq-exporter 发布:sg-prometheus-rabbitmq-exporter
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2020-02-20
    • 2019-06-19
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多