首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向prometheus-operator添加新的服务度量

向prometheus-operator添加新的服务度量
EN

Stack Overflow用户
提问于 2020-08-26 22:02:04
回答 1查看 1.4K关注 0票数 1

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

我怎么能这么做?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-27 05:16:49

首先,您需要通过Helm或手动部署Prometheus操作符:

代码语言:javascript
复制
# 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组件:

代码语言:javascript
复制
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对象:

代码语言:javascript
复制
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将在具有prometheus: prometheus标签的命名空间中创建k8s-app: prometheus label.
  • ServiceMonitor

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

代码语言:javascript
复制
kind: Service
apiVersion: v1
metadata:
  name: example-app
  labels:
    app: example-app
spec:
  selector:
    app: example-app
  ports:
  - name: web
    port: 8080

这个服务对象是由一个ServiceMonitor发现的,它以相同的方式进行选择。app标签必须具有example-app值。

代码语言:javascript
复制
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。但是您需要在Prometheus cr的spec中指定它,例如:

代码语言:javascript
复制
  serviceMonitorNamespaceSelector:
    matchLabels:
      prometheus: prometheus

上面的serviceMonitorNamespaceSelectorPrometheus运算符中用于选择带有标签prometheus: prometheus的命名空间。假设您有一个名称空间demo,并且在这个demo名称空间中创建了一个Prometheus,那么您需要使用修补程序在demo名称空间中添加标签prometheus: prometheus

代码语言:javascript
复制
$ kubectl patch namespace demo -p '{"metadata":{"labels": {"prometheus":"prometheus"}}}'

您可以在这里找到更多详细信息:

https://github.com/helm/charts/tree/master/stable/prometheus-operator

  • Helm

https://github.com/prometheus-operator/prometheus-operator/blob/release-0.41/Documentation/user-guides/getting-started.md

  • 手册

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63606347

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档