向prometheus-operator添加新的服务指标 [英] Add new service metrics to prometheus-operator

查看:156
本文介绍了向prometheus-operator添加新的服务指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按Helm图表将Prometheus-operator部署到群集中,但是我实现了自定义服务以监视我的应用程序,我需要将服务添加到Prometheus-operator中才能查看指标数据.

I'm deploying Prometheus-operator to my cluster by Helm chart but I implement a custom service to monitor my application, I need to add my service to Prometheus-operator to see my metrics data.

我该怎么做?

推荐答案

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

At first, you need to deploy Prometheus-operator by Helm or manually:

# 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内容:

If your cluster is RBAC enabled then you need to install RBAC stuff for Prometheus object:

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 对象:

Then you need to deploy Promethues object:

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 :

Here, Prometheus object will select all ServiceMonitor that meet up the below conditions:

  • ServiceMonitor 将具有 k8s-app:prometheus 标签.
  • 将在具有 prometheus:prometheus 标签的命名空间中创建
  • ServiceMonitor .
  • ServiceMonitor will have the k8s-app: prometheus label.
  • ServiceMonitor will be created in that namespaces which have prometheus: prometheus label.

ServiceMonitor具有标签选择器,用于选择服务及其底层Endpoint对象.示例应用程序的Service对象通过具有 example-app 值的 app 标签选择Pod.Service对象还指定公开指标的端口.

The ServiceMonitor has a label selector to select Services and their underlying Endpoint objects. The Service object for the example application selects the Pods by the app label having the example-app value. The Service object also specifies the port on which the metrics are exposed.

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 .

This Service object is discovered by a ServiceMonitor, which selects in the same way. The app label must have the value 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 指定特定的任何名称空间.

Here, namespaceSelector is used to select all-namespaces where the service is created. you can specify specific any namespace using matchNames.

您还可以根据需要在任何名称空间中创建 ServiceMonitor .但是您需要在 Prometheus cr的 spec 中指定它,例如:

You can also create a ServiceMonitor in any namespace as you want. But you need to specify it in Prometheus cr's spec, like:

  serviceMonitorNamespaceSelector:
    matchLabels:
      prometheus: prometheus

上面的 serviceMonitorNamespaceSelector 用于 Prometheus 运算符,以选择带有标签 prometheus:prometheus 的名称空间.假设您有一个名称空间 demo ,并且在此 demo 名称空间中创建了一个 Prometheus ,那么您需要添加标签 prometheus:prometheus demo 名称空间中使用补丁:

The above serviceMonitorNamespaceSelector is used in Prometheus operator to select that namespace which has the label prometheus: prometheus. Suppose you have a namespace demo and in this demo namespace you have created a Prometheus then you need to add label prometheus: prometheus in demo namespace using patch:

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

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

You can find more details here:

手册: namespaceSelector: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/design.md

namespaceSelector: https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/design.md

这篇关于向prometheus-operator添加新的服务指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆