在Prometheus Operator中创建ServiceMonitor

时间:2020-10-29 20:03:45

标签: docker kubernetes prometheus prometheus-operator

我是Kubernetes的新手。我想在Prometheus Operator中创建服务监视器。我已经安装了Prometheus Operator和Grafana。我有正在运行的吊舱,如下所示: enter image description here

documentation of Prometheus Operator在下面提供了用于创建服务监视器的代码。

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
spec:
  serviceAccountName: prometheus
  serviceMonitorSelector:
    matchLabels:
      team: frontend
  resources:
    requests:
      memory: 400Mi
  enableAdminAPI: false

我知道这是一个Yaml文件,但我对如何运行此文件感到困惑?换句话说,我应该将这段代码放在哪里?我正在学习创建Prometheus监视器。我可以帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

您可以像运行Kubernetes集群中的任何其他清单一样部署它,例如,通过运行kubectl apply -f servicemonitor.yaml

您可以通过简单运行kubectl get prometheus来检查它是否已部署:

$ kubectl get prometheus
NAME         VERSION   REPLICAS   AGE
prometheus                        5s

启用RBAC授权后,您还应该使用另一个Yaml:

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  name: prometheus
  labels:
    prometheus: prometheus
spec:
  replicas: 2
  serviceAccountName: prometheus
  serviceMonitorSelector:
    matchLabels:
      team: frontend
  alerting:
    alertmanagers:
    - namespace: default
      name: alertmanager
      port: web