Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 4. 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
ServiceMonitor
object that specifies how the service is to be monitored by the COO. -
Create a
MonitoringStack
object to discover theServiceMonitor
object.
4.1. Deploying a sample service for Cluster Observability Operator Copier lienLien copié sur presse-papiers!
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-admin
cluster role or as a user with administrative permissions for the namespace.
Procedure
Create a YAML file named
prometheus-coo-example-app.yaml
that 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.yaml
Copy 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 pod
Copy 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 81m
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.2. Specifying how a service is monitored by Cluster Observability Operator Copier lienLien copié sur presse-papiers!
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-admin
cluster 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-app
sample service in thens1-coo
namespace.NoteThe
prometheus-coo-example-app
sample service does not support TLS authentication.
Procedure
Create a YAML file named
example-coo-app-service-monitor.yaml
that contains the followingServiceMonitor
object configuration details:Copy to Clipboard Copied! Toggle word wrap Toggle overflow This configuration defines a
ServiceMonitor
object that theMonitoringStack
object will reference to scrape the metrics data exposed by theprometheus-coo-example-app
sample 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.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the
ServiceMonitor
resource 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.rhobs
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME AGE prometheus-coo-example-monitor 81m
NAME AGE prometheus-coo-example-monitor 81m
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.3. Creating a MonitoringStack object for the Cluster Observability Operator Copier lienLien copié sur presse-papiers!
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-admin
cluster 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-app
sample service in thens1-coo
namespace. -
You have created a
ServiceMonitor
object namedprometheus-coo-example-monitor
in thens1-coo
namespace.
Procedure
-
Create a YAML file for the
MonitoringStack
object configuration. For this example, name the fileexample-coo-monitoring-stack.yaml
. Add the following
MonitoringStack
object configuration details:Example
MonitoringStack
objectCopy to Clipboard Copied! Toggle word wrap Toggle overflow Apply the
MonitoringStack
object by running the following command:oc apply -f example-coo-monitoring-stack.yaml
$ oc apply -f example-coo-monitoring-stack.yaml
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the
MonitoringStack
object is available by running the following command and inspecting the output:oc -n ns1-coo get monitoringstack
$ oc -n ns1-coo get monitoringstack
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
NAME AGE example-coo-monitoring-stack 81m
NAME AGE example-coo-monitoring-stack 81m
Copy 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
jq
command-line JSON processor to format the output for convenience.
4.4. Validating the monitoring stack Copier lienLien copié sur presse-papiers!
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-admin
cluster 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-app
sample service in thens1-coo
namespace. -
You have created a
ServiceMonitor
object namedprometheus-coo-example-monitor
in thens1-coo
namespace. -
You have created a
MonitoringStack
object namedexample-coo-monitoring-stack
in thens1-coo
namespace.
Procedure
Create a route to expose the example
prometheus-coo-example-app
service. 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-coo
Copy 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
jq
for convenience)Copy to Clipboard Copied! Toggle word wrap Toggle overflow
4.5. Scrape targets in multiple namespaces Copier lienLien copié sur presse-papiers!
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-admin
cluster 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
MonitoringStack
YAML file:Example
MonitoringStack
Copy 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
Monitoringstack
instance.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 9090
Copy 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 9093
Copy 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/targets
orhttp://localhost:9093/#/alerts
.