gpt4 book ai didi

kubernetes - 向prometheus-operator添加新的服务指标

转载 作者:行者123 更新时间:2023-12-02 12:00:01 24 4
gpt4 key购买 nike

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

最佳答案

首先,您需要通过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标签的 namespace 中创建。

  • ServiceMonitor具有标签选择器,可以选择服务及其底层Endpoint对象。示例应用程序的Service对象通过具有 app值的 example-app标签选择Pod。服务对象还指定公开度量标准的端口。
    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用于选择创建服务的所有 namespace 。您可以使用 matchNames指定特定的任何 namespace 。
    您还可以根据需要在任何 namespace 中创建 ServiceMonitor。但是您需要在 Prometheus cr的 spec中指定它,例如:
      serviceMonitorNamespaceSelector:
    matchLabels:
    prometheus: prometheus
    上面的 serviceMonitorNamespaceSelectorPrometheus运算符中用于选择带有标签 prometheus: prometheus的 namespace 。假设您有一个命名空间 demo,并且在此 demo命名空间中创建了 Prometheus,然后需要使用patch在 prometheus: prometheus命名空间中添加标签 demo:
    $ kubectl patch namespace demo -p '{"metadata":{"labels": {"prometheus":"prometheus"}}}'
    您可以在此处找到更多详细信息:
  • Helm :https://github.com/helm/charts/tree/master/stable/prometheus-operator
  • 手册:https://github.com/prometheus-operator/prometheus-operator/blob/release-0.41/Documentation/user-guides/getting-started.md
  • namespace 选择器:https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/design.md
  • 关于kubernetes - 向prometheus-operator添加新的服务指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63606347/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com