This documentation is for a release that is no longer maintained
See documentation for the latest supported version 3 or the latest supported version 4.Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 7. Managing metrics
You can collect metrics to monitor how cluster components and your own workloads are performing.
7.1. Understanding metrics
In OpenShift Container Platform 4.11, cluster components are monitored by scraping metrics exposed through service endpoints. You can also configure metrics collection for user-defined projects.
You can define the metrics that you want to provide for your own workloads by using Prometheus client libraries at the application level.
				In OpenShift Container Platform, metrics are exposed through an HTTP service endpoint under the /metrics canonical name. You can list all available metrics for a service by running a curl query against http://<endpoint>/metrics. For instance, you can expose a route to the prometheus-example-app example service and then run the following to view all of its available metrics:
			
curl http://<example_app_endpoint>/metrics
$ curl http://<example_app_endpoint>/metricsExample output
7.2. Setting up metrics collection for user-defined projects
				You can create a ServiceMonitor resource to scrape metrics from a service endpoint in a user-defined project. This assumes that your application uses a Prometheus client library to expose metrics to the /metrics canonical name.
			
				This section describes how to deploy a sample service in a user-defined project and then create a ServiceMonitor resource that defines how that service should be monitored.
			
7.2.1. Deploying a sample service
To test monitoring of a service in a user-defined project, you can deploy a sample service.
Procedure
- 
							Create a YAML file for the service configuration. In this example, it is called prometheus-example-app.yaml.
- Add the following deployment and service configuration details to the file: - Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - This configuration deploys a service named - prometheus-example-appin the user-defined- ns1project. This service exposes the custom- versionmetric.
- Apply the configuration to the cluster: - oc apply -f prometheus-example-app.yaml - $ oc apply -f prometheus-example-app.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - It takes some time to deploy the service. 
- You can check that the pod is running: - oc -n ns1 get pod - $ oc -n ns1 get pod- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Example output - NAME READY STATUS RESTARTS AGE prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m - NAME READY STATUS RESTARTS AGE prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow 
7.2.2. Specifying how a service is monitored
					To use the metrics exposed by your service, you must configure OpenShift Container Platform monitoring to scrape metrics from the /metrics endpoint. You can do this using a ServiceMonitor custom resource definition (CRD) that specifies how a service should be monitored, or a PodMonitor CRD that specifies how a pod should be monitored. The former requires a Service object, while the latter does not, allowing Prometheus to directly scrape metrics from the metrics endpoint exposed by a pod.
				
					This procedure shows you how to create a ServiceMonitor resource for a service in a user-defined project.
				
Prerequisites
- 
							You have access to the cluster as a user with the cluster-admincluster role or themonitoring-editcluster role.
- You have enabled monitoring for user-defined projects.
- For this example, you have deployed the - prometheus-example-appsample service in the- ns1project.Note- The - prometheus-example-appsample service does not support TLS authentication.
Procedure
- 
							Create a YAML file for the ServiceMonitorresource configuration. In this example, the file is calledexample-app-service-monitor.yaml.
- Add the following - ServiceMonitorresource configuration details:- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - This defines a - ServiceMonitorresource that scrapes the metrics exposed by the- prometheus-example-appsample service, which includes the- versionmetric.Note- A - ServiceMonitorresource in a user-defined namespace can only discover services in the same namespace. That is, the- namespaceSelectorfield of the- ServiceMonitorresource is always ignored.
- Apply the configuration to the cluster: - oc apply -f example-app-service-monitor.yaml - $ oc apply -f example-app-service-monitor.yaml- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - It takes some time to deploy the - ServiceMonitorresource.
- You can check that the - ServiceMonitorresource is running:- oc -n ns1 get servicemonitor - $ oc -n ns1 get servicemonitor- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow - Example output - NAME AGE prometheus-example-monitor 81m - NAME AGE prometheus-example-monitor 81m- Copy to Clipboard Copied! - Toggle word wrap Toggle overflow