Chapter 2. Configuring the Cluster Observability Operator to monitor a service
You can monitor metrics for a service by configuring monitoring stacks managed by the Cluster Observability Operator (COO).
To test monitoring a service, follow these steps:
- Deploy a sample service that defines a service endpoint.
-
Create a
ServiceMonitorobject that specifies how the service is to be monitored by the COO. -
Create a
MonitoringStackobject to discover theServiceMonitorobject.
2.1. Deploying a sample service for Cluster Observability Operator Copy linkLink copied to clipboard!
This configuration deploys a sample service named prometheus-coo-example-app in the user-defined ns1-coo project. The service exposes the custom version metric.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role or as a user with administrative permissions for the namespace.
Procedure
Create a YAML file named
prometheus-coo-example-app.yamlthat contains the following configuration details for a namespace, deployment, and service:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Save the file.
Apply the configuration to the cluster by running the following command:
oc apply -f prometheus-coo-example-app.yaml
$ oc apply -f prometheus-coo-example-app.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the pod is running by running the following command and observing the output:
oc -n ns1-coo get pod
$ oc -n ns1-coo get podCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME READY STATUS RESTARTS AGE prometheus-coo-example-app-0927545cb7-anskj 1/1 Running 0 81m
NAME READY STATUS RESTARTS AGE prometheus-coo-example-app-0927545cb7-anskj 1/1 Running 0 81mCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.2. Specifying how a service is monitored by Cluster Observability Operator Copy linkLink copied to clipboard!
To use the metrics exposed by the sample service you created in the "Deploying a sample service for Cluster Observability Operator" section, you must configure monitoring components to scrape metrics from the /metrics endpoint.
You can create this configuration by using a ServiceMonitor object that specifies how the service is to be monitored, or a PodMonitor object that specifies how a pod is to be monitored. The ServiceMonitor object requires a Service object. The PodMonitor object does not, which enables the MonitoringStack object to scrape metrics directly from the metrics endpoint exposed by a pod.
This procedure shows how to create a ServiceMonitor object for a sample service named prometheus-coo-example-app in the ns1-coo namespace.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role or as a user with administrative permissions for the namespace. - You have installed the Cluster Observability Operator.
You have deployed the
prometheus-coo-example-appsample service in thens1-coonamespace.NoteThe
prometheus-coo-example-appsample service does not support TLS authentication.
Procedure
Create a YAML file named
example-coo-app-service-monitor.yamlthat contains the followingServiceMonitorobject configuration details:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This configuration defines a
ServiceMonitorobject that theMonitoringStackobject will reference to scrape the metrics data exposed by theprometheus-coo-example-appsample service.Apply the configuration to the cluster by running the following command:
oc apply -f example-coo-app-service-monitor.yaml
$ oc apply -f example-coo-app-service-monitor.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the
ServiceMonitorresource is created by running the following command and observing the output:oc -n ns1-coo get servicemonitors.monitoring.rhobs
$ oc -n ns1-coo get servicemonitors.monitoring.rhobsCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME AGE prometheus-coo-example-monitor 81m
NAME AGE prometheus-coo-example-monitor 81mCopy to Clipboard Copied! Toggle word wrap Toggle overflow
2.3. Creating a MonitoringStack object for the Cluster Observability Operator Copy linkLink copied to clipboard!
To scrape the metrics data exposed by the target prometheus-coo-example-app service, create a MonitoringStack object that references the ServiceMonitor object you created in the "Specifying how a service is monitored for Cluster Observability Operator" section. This MonitoringStack object can then discover the service and scrape the exposed metrics data from it.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role or as a user with administrative permissions for the namespace. - You have installed the Cluster Observability Operator.
-
You have deployed the
prometheus-coo-example-appsample service in thens1-coonamespace. -
You have created a
ServiceMonitorobject namedprometheus-coo-example-monitorin thens1-coonamespace.
Procedure
-
Create a YAML file for the
MonitoringStackobject configuration. For this example, name the fileexample-coo-monitoring-stack.yaml. Add the following
MonitoringStackobject configuration details:Example
MonitoringStackobjectCopy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
MonitoringStackobject by running the following command:oc apply -f example-coo-monitoring-stack.yaml
$ oc apply -f example-coo-monitoring-stack.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the
MonitoringStackobject is available by running the following command and inspecting the output:oc -n ns1-coo get monitoringstack
$ oc -n ns1-coo get monitoringstackCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME AGE example-coo-monitoring-stack 81m
NAME AGE example-coo-monitoring-stack 81mCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run the following comand to retrieve information about the active targets from Prometheus and filter the output to list only targets labeled with
app=prometheus-coo-example-app. This verifies which targets are discovered and actively monitored by Prometheus with this specific label.oc -n ns1-coo exec -c prometheus prometheus-example-coo-monitoring-stack-0 -- curl -s 'http://localhost:9090/api/v1/targets' | jq '.data.activeTargets[].discoveredLabels | select(.__meta_kubernetes_endpoints_label_app=="prometheus-coo-example-app")'
$ oc -n ns1-coo exec -c prometheus prometheus-example-coo-monitoring-stack-0 -- curl -s 'http://localhost:9090/api/v1/targets' | jq '.data.activeTargets[].discoveredLabels | select(.__meta_kubernetes_endpoints_label_app=="prometheus-coo-example-app")'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThe above example uses
jqcommand-line JSON processor to format the output for convenience.
2.4. Validating the monitoring stack Copy linkLink copied to clipboard!
To validate that the monitoring stack is working correctly, access the example service and then view the gathered metrics.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role or as a user with administrative permissions for the namespace. - You have installed the Cluster Observability Operator.
-
You have deployed the
prometheus-coo-example-appsample service in thens1-coonamespace. -
You have created a
ServiceMonitorobject namedprometheus-coo-example-monitorin thens1-coonamespace. -
You have created a
MonitoringStackobject namedexample-coo-monitoring-stackin thens1-coonamespace.
Procedure
Create a route to expose the example
prometheus-coo-example-appservice. From your terminal, run the command:oc expose svc prometheus-coo-example-app -n ns1-coo
$ oc expose svc prometheus-coo-example-app -n ns1-cooCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Access the route from your browser, or command line, to generate metrics.
Execute a query on the Prometheus pod to return the total HTTP requests metric:
oc -n ns1-coo exec -c prometheus prometheus-example-coo-monitoring-stack-0 -- curl -s 'http://localhost:9090/api/v1/query?query=http_requests_total'
$ oc -n ns1-coo exec -c prometheus prometheus-example-coo-monitoring-stack-0 -- curl -s 'http://localhost:9090/api/v1/query?query=http_requests_total'Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output (formatted using
jqfor convenience)Copy to Clipboard Copied! Toggle word wrap Toggle overflow
2.5. Scrape targets in multiple namespaces Copy linkLink copied to clipboard!
To scrape targets in multiple namespaces, set the namespace and resource selector in the MonitoringStack object.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role or as a user with administrative permissions for the namespace. - You have installed the Cluster Observability Operator.
Procedure
Deploy the following namespace object and
MonitoringStackYAML file:Example
MonitoringStackCopy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy a sample application in the namespace
ns1-coo, with an alert that is always firing:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Deploy the same example application in another namespace labeled with
monitoring.rhobs/stack: multi-ns:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the Prometheus instance adds new targets and that the alert are firing. Use a port-forward command to expose the Prometheus or the Alertmanager user interface that has been deployed by the
Monitoringstackinstance.Prometheus
oc port-forward -n ns1-coo pod/prometheus-example-coo-monitoring-stack-0 9090
$ oc port-forward -n ns1-coo pod/prometheus-example-coo-monitoring-stack-0 9090Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alertmanager
oc port-forward -n ns1-coo pod/alertmanager-example-coo-monitoring-stack-0 9093
$ oc port-forward -n ns1-coo pod/alertmanager-example-coo-monitoring-stack-0 9093Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
Verify that the targets are being scraped and that the alerts are firing by browsing to
http://localhost:9090/targetsorhttp://localhost:9093/#/alerts.