Configuring Prometheus with Prometheus Operator: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
* https://coreos.com/operators/prometheus/docs/0.17.0/index.html
* https://coreos.com/operators/prometheus/docs/0.17.0/index.html
* https://github.com/prometheus-operator/prometheus-operator
* https://github.com/prometheus-operator/prometheus-operator
* https://github.com/prometheus-operator/prometheus-operator/blob/master/Documentation/api.md
=Internal=
=Internal=
* [[Prometheus_Configuration#Configuring_Prometheus_with_Prometheus_Operator|Prometheus Configuration]]
* [[Prometheus_Configuration#Configuring_Prometheus_with_Prometheus_Operator|Prometheus Configuration]]
* [[Prometheus Operator Concepts]]
=Overview=
=Overview=


=Add a Target=
=Add a Monitoring Target=
To add a monitoring [[Prometheus_Concepts#Target|target]], deploy a [[Prometheus_Operator_Concepts#ServiceMonitor_Resource|ServiceMonitor]] custom resource:
To add a monitoring [[Prometheus_Concepts#Target|target]], deploy a [[Prometheus_Operator_Concepts#ServiceMonitor_Resource|ServiceMonitor]] custom resource:
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
Line 20: Line 24:
spec:
spec:
   endpoints:
   endpoints:
   - targetPort: 9091
   - port: 'pushgateway'
     path: /metrics
     path: /metrics
     # default interval 30s
     # default interval 30s
Line 31: Line 35:
       function: pushgateway-target
       function: pushgateway-target
EOF
EOF
</syntaxhighlight>
⚠️ The endpoint "port" element must be a string, and it must be the [[Kubernetes_Service_Manifest#name|name]] of the port exposed by the corresponding Service:
<syntaxhighlight lang='yaml'>
apiVersion: v1
kind: Service
...
spec:
  ports:
  - name: 'pushgateway'
    port: 9091
    targetPort: 9091
    ...
</syntaxhighlight>
</syntaxhighlight>


Line 45: Line 62:
Also, ServiceMonitor selector must match monitored [[Kubernetes_Service_Concepts#Service_.28ClusterIP_Service.29|Services]]' labels.
Also, ServiceMonitor selector must match monitored [[Kubernetes_Service_Concepts#Service_.28ClusterIP_Service.29|Services]]' labels.


If the ServiceMonitor was configured correctly, the new monitoring target surfaces in the Prometheus server configuration after a while. The configuration can be inspected from the server's [[Prometheus_Concepts#Prometheus_Console|console]] (Status → Configuration). Also the console has a "Targets" section where the target should show up.
If the ServiceMonitor was configured correctly, the new monitoring target surfaces in the Prometheus server configuration after a while. The configuration can be inspected from the server's [[Prometheus_Concepts#Prometheus_Console|console]] (Status → Configuration). Also the console has a "Targets" section where the target should show up:
[[File: SuccessfulPrometheusTarget.png]]

Latest revision as of 00:45, 15 October 2020

External

Internal

Overview

Add a Monitoring Target

To add a monitoring target, deploy a ServiceMonitor custom resource:

namespace="prometheus"
cat <<EOF | kubectl -n ${namespace} apply -f -
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: pushgateway
  namespace: ${namespace}
  labels:
    release: prometheus
spec:
  endpoints:
  - port: 'pushgateway'
    path: /metrics
    # default interval 30s
    interval: 15s
  namespaceSelector:
    matchNames:
    - ${namespace}
  selector:
    matchLabels:
      function: pushgateway-target
EOF

⚠️ The endpoint "port" element must be a string, and it must be the name of the port exposed by the corresponding Service:

apiVersion: v1
kind: Service
...
spec:
  ports:
  - name: 'pushgateway'
    port: 9091
    targetPort: 9091
    ...

The ServiceMonitor resource must carry a label that will identify it to the Prometheus custom resource by matching the Prometheus resource "serviceMonitorSelector":

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
...
spec:
  serviceMonitorSelector:
    matchLabels:
      release: prometheus

Also, ServiceMonitor selector must match monitored Services' labels.

If the ServiceMonitor was configured correctly, the new monitoring target surfaces in the Prometheus server configuration after a while. The configuration can be inspected from the server's console (Status → Configuration). Also the console has a "Targets" section where the target should show up: SuccessfulPrometheusTarget.png