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 Link kopierenLink in die Zwischenablage kopiert!
In OpenShift Container Platform 4.12, 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
curl
http://<endpoint>/metrics
prometheus-example-app
$ curl http://<example_app_endpoint>/metrics
Example output
# HELP http_requests_total Count of all HTTP requests
# TYPE http_requests_total counter
http_requests_total{code="200",method="get"} 4
http_requests_total{code="404",method="get"} 2
# HELP version Version information about this binary
# TYPE version gauge
version{version="v0.1.0"} 1
7.2. Setting up metrics collection for user-defined projects Link kopierenLink in die Zwischenablage kopiert!
You can create a
ServiceMonitor
/metrics
This section describes how to deploy a sample service in a user-defined project and then create a
ServiceMonitor
7.2.1. Deploying a sample service Link kopierenLink in die Zwischenablage kopiert!
To test monitoring of a service in a user-defined project, you can deploy a sample service.
Prerequisites
-
You have access to the cluster as a user with the cluster role or as a user with administrative permissions for the namespace.
cluster-admin
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:
apiVersion: v1 kind: Namespace metadata: name: ns1 --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: prometheus-example-app name: prometheus-example-app namespace: ns1 spec: replicas: 1 selector: matchLabels: app: prometheus-example-app template: metadata: labels: app: prometheus-example-app spec: containers: - image: ghcr.io/rhobs/prometheus-example-app:0.4.2 imagePullPolicy: IfNotPresent name: prometheus-example-app --- apiVersion: v1 kind: Service metadata: labels: app: prometheus-example-app name: prometheus-example-app namespace: ns1 spec: ports: - port: 8080 protocol: TCP targetPort: 8080 name: web selector: app: prometheus-example-app type: ClusterIPThis configuration deploys a service named
in the user-definedprometheus-example-appproject. This service exposes the customns1metric.versionApply the configuration to the cluster:
$ oc apply -f prometheus-example-app.yamlIt takes some time to deploy the service.
You can check that the pod is running:
$ oc -n ns1 get podExample output
NAME READY STATUS RESTARTS AGE prometheus-example-app-7857545cb7-sbgwq 1/1 Running 0 81m
7.2.2. Specifying how a service is monitored Link kopierenLink in die Zwischenablage kopiert!
To use the metrics exposed by your service, you must configure OpenShift Container Platform monitoring to scrape metrics from the
/metrics
ServiceMonitor
PodMonitor
Service
This procedure shows you how to create a
ServiceMonitor
Prerequisites
-
You have access to the cluster as a user with the cluster role or the
cluster-admincluster role.monitoring-edit - You have enabled monitoring for user-defined projects.
For this example, you have deployed the
sample service in theprometheus-example-appproject.ns1NoteThe
sample service does not support TLS authentication.prometheus-example-app
Procedure
-
Create a new YAML configuration file named .
example-app-service-monitor.yaml Add a
resource to the YAML file. The following example creates a service monitor namedServiceMonitorto scrape metrics exposed by theprometheus-example-monitorservice in theprometheus-example-appnamespace:ns1apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: prometheus-example-monitor namespace: ns11 spec: endpoints: - interval: 30s port: web2 scheme: http selector:3 matchLabels: app: prometheus-example-appNoteA
resource in a user-defined namespace can only discover services in the same namespace. That is, theServiceMonitorfield of thenamespaceSelectorresource is always ignored.ServiceMonitorApply the configuration to the cluster:
$ oc apply -f example-app-service-monitor.yamlIt takes some time to deploy the
resource.ServiceMonitorVerify that the
resource is running:ServiceMonitor$ oc -n <namespace> get servicemonitorExample output
NAME AGE prometheus-example-monitor 81m
7.2.3. Example service endpoint authentication settings Link kopierenLink in die Zwischenablage kopiert!
You can configure authentication for service endpoints for user-defined project monitoring by using
ServiceMonitor
PodMonitor
The following samples show different authentication settings for a
ServiceMonitor
Secret
7.2.3.1. Sample YAML authentication with a bearer token Link kopierenLink in die Zwischenablage kopiert!
The following sample shows bearer token settings for a
Secret
example-bearer-auth
ns1
Example bearer token secret
apiVersion: v1
kind: Secret
metadata:
name: example-bearer-auth
namespace: ns1
stringData:
token: <authentication_token>
- 1
- Specify an authentication token.
The following sample shows bearer token authentication settings for a
ServiceMonitor
Secret
example-bearer-auth
Example bearer token authentication settings
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-example-monitor
namespace: ns1
spec:
endpoints:
- authorization:
credentials:
key: token
name: example-bearer-auth
port: web
selector:
matchLabels:
app: prometheus-example-app
Do not use
bearerTokenFile
bearerTokenFile
ServiceMonitor
7.2.3.2. Sample YAML for Basic authentication Link kopierenLink in die Zwischenablage kopiert!
The following sample shows Basic authentication settings for a
Secret
example-basic-auth
ns1
Example Basic authentication secret
apiVersion: v1
kind: Secret
metadata:
name: example-basic-auth
namespace: ns1
stringData:
user: <basic_username>
password: <basic_password>
The following sample shows Basic authentication settings for a
ServiceMonitor
Secret
example-basic-auth
Example Basic authentication settings
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-example-monitor
namespace: ns1
spec:
endpoints:
- basicAuth:
username:
key: user
name: example-basic-auth
password:
key: password
name: example-basic-auth
port: web
selector:
matchLabels:
app: prometheus-example-app
7.2.3.3. Sample YAML authentication with OAuth 2.0 Link kopierenLink in die Zwischenablage kopiert!
The following sample shows OAuth 2.0 settings for a
Secret
example-oauth2
ns1
Example OAuth 2.0 secret
apiVersion: v1
kind: Secret
metadata:
name: example-oauth2
namespace: ns1
stringData:
id: <oauth2_id>
secret: <oauth2_secret>
The following sample shows OAuth 2.0 authentication settings for a
ServiceMonitor
Secret
example-oauth2
Example OAuth 2.0 authentication settings
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: prometheus-example-monitor
namespace: ns1
spec:
endpoints:
- oauth2:
clientId:
secret:
key: id
name: example-oauth2
clientSecret:
key: secret
name: example-oauth2
tokenUrl: https://example.com/oauth2/token
port: web
selector:
matchLabels:
app: prometheus-example-app
- 1
- The key that contains the OAuth 2.0 ID in the specified
Secretobject. - 2 4
- The name of the
Secretobject that contains the OAuth 2.0 credentials. - 3
- The key that contains the OAuth 2.0 secret in the specified
Secretobject. - 5
- The URL used to fetch a token with the specified
clientIdandclientSecret.
7.3. Viewing a list of available metrics Link kopierenLink in die Zwischenablage kopiert!
As a cluster administrator or as a user with view permissions for all projects, you can view a list of metrics available in a cluster and output the list in JSON format.
Prerequisites
-
You are a cluster administrator, or you have access to the cluster as a user with the cluster role.
cluster-monitoring-view -
You have installed the OpenShift Container Platform CLI ().
oc - You have obtained the OpenShift Container Platform API route for Thanos Querier.
You are able to get a bearer token by using the
command.oc whoami -tImportantYou can only use bearer token authentication to access the Thanos Querier API route.
Procedure
If you have not obtained the OpenShift Container Platform API route for Thanos Querier, run the following command:
$ oc get routes -n openshift-monitoring thanos-querier -o jsonpath='{.status.ingress[0].host}'Retrieve a list of metrics in JSON format from the Thanos Querier API route by running the following command. This command uses
to authenticate with a bearer token.oc$ curl -k -H "Authorization: Bearer $(oc whoami -t)" https://<thanos_querier_route>/api/v1/metadata1 - 1
- Replace
<thanos_querier_route>with the OpenShift Container Platform API route for Thanos Querier.