Search code examples
dockerkubernetesprometheusprometheus-operator

Create ServiceMonitor in Prometheus Operator


I am new to Kubernetes. I want to create Service Monitor in Prometheus Operator. I've installed Prometheus Operator and Grafana. I have the running pods as shown below: enter image description here

The documentation of Prometheus Operator provides code below to create Service Monitor.

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

I know this is a yaml file, but I am confused how to run this file? In other words, where should I put this piece of code to? I am learning to create Prometheus monitors. Can I get some help?

Thanks!


Solution

  • You can deploy it like any other manifest in Kubernetes cluster, for example by running kubectl apply -f servicemonitor.yaml.

    You can check if it was deployed by simply running kubectl get prometheus:

    $ kubectl get prometheus
    NAME         VERSION   REPLICAS   AGE
    prometheus                        5s
    

    When you enabled RBAC authorization there is another yaml that you should use:

    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