Questo contenuto non è disponibile nella lingua selezionata.
Chapter 5. Enabling monitoring for user-defined projects
In OpenShift Container Platform 4.11, you can enable monitoring for user-defined projects in addition to the default platform monitoring. You can monitor your own projects in OpenShift Container Platform without the need for an additional monitoring solution. Using this feature centralizes monitoring for core platform components and user-defined projects.
Versions of Prometheus Operator installed using Operator Lifecycle Manager (OLM) are not compatible with user-defined monitoring. Therefore, custom Prometheus instances installed as a Prometheus custom resource (CR) managed by the OLM Prometheus Operator are not supported in OpenShift Container Platform.
5.1. Enabling monitoring for user-defined projects Copia collegamentoCollegamento copiato negli appunti!
Cluster administrators can enable monitoring for user-defined projects by setting the
enableUserWorkload: true
ConfigMap
In OpenShift Container Platform 4.11 you must remove any custom Prometheus instances before enabling monitoring for user-defined projects.
You must have access to the cluster as a user with the
cluster-admin
Prerequisites
-
You have access to the cluster as a user with the cluster role.
cluster-admin -
You have installed the OpenShift CLI ().
oc -
You have created the
cluster-monitoring-configobject.ConfigMap You have optionally created and configured the
user-workload-monitoring-configobject in theConfigMapproject. You can add configuration options to thisopenshift-user-workload-monitoringobject for the components that monitor user-defined projects.ConfigMapNoteEvery time you save configuration changes to the
user-workload-monitoring-configobject, the pods in theConfigMapproject are redeployed. It can sometimes take a while for these components to redeploy. You can create and configure theopenshift-user-workload-monitoringobject before you first enable monitoring for user-defined projects, to prevent having to redeploy the pods often.ConfigMap
Procedure
Edit the
cluster-monitoring-configobject:ConfigMap$ oc -n openshift-monitoring edit configmap cluster-monitoring-configAdd
underenableUserWorkload: true:data/config.yamlapiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | enableUserWorkload: true1 - 1
- When set to
true, theenableUserWorkloadparameter enables monitoring for user-defined projects in a cluster.
Save the file to apply the changes. Monitoring for user-defined projects is then enabled automatically.
WarningWhen changes are saved to the
cluster-monitoring-configobject, the pods and other resources in theConfigMapproject might be redeployed. The running monitoring processes in that project might also be restarted.openshift-monitoringCheck that the
,prometheus-operatorandprometheus-user-workloadpods are running in thethanos-ruler-user-workloadproject. It might take a short while for the pods to start:openshift-user-workload-monitoring$ oc -n openshift-user-workload-monitoring get podExample output
NAME READY STATUS RESTARTS AGE prometheus-operator-6f7b748d5b-t7nbg 2/2 Running 0 3h prometheus-user-workload-0 4/4 Running 1 3h prometheus-user-workload-1 4/4 Running 1 3h thanos-ruler-user-workload-0 3/3 Running 0 3h thanos-ruler-user-workload-1 3/3 Running 0 3h
5.2. Granting users permission to monitor user-defined projects Copia collegamentoCollegamento copiato negli appunti!
Cluster administrators can monitor all core OpenShift Container Platform and user-defined projects.
Cluster administrators can grant developers and other users permission to monitor their own projects. Privileges are granted by assigning one of the following monitoring roles:
-
The monitoring-rules-view cluster role provides read access to custom resources for a project.
PrometheusRule -
The monitoring-rules-edit cluster role grants a user permission to create, modify, and deleting custom resources for a project.
PrometheusRule -
The monitoring-edit cluster role grants the same privileges as the cluster role. Additionally, it enables a user to create new scrape targets for services or pods. With this role, you can also create, modify, and delete
monitoring-rules-editandServiceMonitorresources.PodMonitor
You can also grant users permission to configure the components that are responsible for monitoring user-defined projects:
-
The user-workload-monitoring-config-edit role in the project enables you to edit the
openshift-user-workload-monitoringuser-workload-monitoring-configobject. With this role, you can edit theConfigMapobject to configure Prometheus, Prometheus Operator, and Thanos Ruler for user-defined workload monitoring.ConfigMap
You can also grant users permission to configure alert routing for user-defined projects:
-
The alert-routing-edit cluster role grants a user permission to create, update, and delete custom resources for a project.
AlertmanagerConfig
This section provides details on how to assign these roles by using the OpenShift Container Platform web console or the CLI.
5.2.1. Granting user permissions by using the web console Copia collegamentoCollegamento copiato negli appunti!
You can grant users permissions to monitor their own projects, by using the OpenShift Container Platform web console.
Prerequisites
-
You have access to the cluster as a user with the cluster role.
cluster-admin - The user account that you are assigning the role to already exists.
Procedure
-
In the Administrator perspective within the OpenShift Container Platform web console, navigate to User Management
Role Bindings Create Binding. - In the Binding Type section, select the "Namespace Role Binding" type.
- In the Name field, enter a name for the role binding.
In the Namespace field, select the user-defined project where you want to grant the access.
ImportantThe monitoring role will be bound to the project that you apply in the Namespace field. The permissions that you grant to a user by using this procedure will apply only to the selected project.
-
Select ,
monitoring-rules-view, ormonitoring-rules-editin the Role Name list.monitoring-edit - In the Subject section, select User.
- In the Subject Name field, enter the name of the user.
- Select Create to apply the role binding.
5.2.2. Granting user permissions by using the CLI Copia collegamentoCollegamento copiato negli appunti!
You can grant users permissions to monitor their own projects, by using the OpenShift CLI (
oc
Prerequisites
-
You have access to the cluster as a user with the cluster role.
cluster-admin - The user account that you are assigning the role to already exists.
-
You have installed the OpenShift CLI ().
oc
Procedure
Assign a monitoring role to a user for a project:
$ oc policy add-role-to-user <role> <user> -n <namespace>1 - 1
- Substitute
<role>withmonitoring-rules-view,monitoring-rules-edit, ormonitoring-edit.
ImportantWhichever role you choose, you must bind it against a specific project as a cluster administrator.
As an example, substitute
with<role>,monitoring-editwith<user>, andjohnsmithwith<namespace>. This assigns the userns1permission to set up metrics collection and to create alerting rules in thejohnsmithnamespace.ns1
5.3. Granting users permission to configure monitoring for user-defined projects Copia collegamentoCollegamento copiato negli appunti!
You can grant users permission to configure monitoring for user-defined projects.
Prerequisites
-
You have access to the cluster as a user with the cluster role.
cluster-admin - The user account that you are assigning the role to already exists.
-
You have installed the OpenShift CLI ().
oc
Procedure
Assign the
role to a user in theuser-workload-monitoring-config-editproject:openshift-user-workload-monitoring$ oc -n openshift-user-workload-monitoring adm policy add-role-to-user \ user-workload-monitoring-config-edit <user> \ --role-namespace openshift-user-workload-monitoring
5.4. Accessing metrics from outside the cluster for custom applications Copia collegamentoCollegamento copiato negli appunti!
Learn how to query Prometheus statistics from the command line when monitoring your own services. You can access monitoring data from outside the cluster with the
thanos-querier
Prerequisites
- You deployed your own service, following the Enabling monitoring for user-defined projects procedure.
Procedure
Extract a token to connect to Prometheus:
$ SECRET=`oc get secret -n openshift-user-workload-monitoring | grep prometheus-user-workload-token | head -n 1 | awk '{print $1 }'`$ TOKEN=`echo $(oc get secret $SECRET -n openshift-user-workload-monitoring -o json | jq -r '.data.token') | base64 -d`Extract your route host:
$ THANOS_QUERIER_HOST=`oc get route thanos-querier -n openshift-monitoring -o json | jq -r '.spec.host'`Query the metrics of your own services in the command line. For example:
$ NAMESPACE=ns1$ curl -X GET -kG "https://$THANOS_QUERIER_HOST/api/v1/query?" --data-urlencode "query=up{namespace='$NAMESPACE'}" -H "Authorization: Bearer $TOKEN"The output will show you the duration that your application pods have been up.
Example output
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","endpoint":"web","instance":"10.129.0.46:8080","job":"prometheus-example-app","namespace":"ns1","pod":"prometheus-example-app-68d47c4fb6-jztp2","service":"prometheus-example-app"},"value":[1591881154.748,"1"]}]}}
5.5. Excluding a user-defined project from monitoring Copia collegamentoCollegamento copiato negli appunti!
Individual user-defined projects can be excluded from user workload monitoring. To do so, simply add the
openshift.io/user-monitoring
false
Procedure
Add the label to the project namespace:
$ oc label namespace my-project 'openshift.io/user-monitoring=false'To re-enable monitoring, remove the label from the namespace:
$ oc label namespace my-project 'openshift.io/user-monitoring-'NoteIf there were any active monitoring targets for the project, it may take a few minutes for Prometheus to stop scraping them after adding the label.
5.6. Disabling monitoring for user-defined projects Copia collegamentoCollegamento copiato negli appunti!
After enabling monitoring for user-defined projects, you can disable it again by setting
enableUserWorkload: false
ConfigMap
Alternatively, you can remove
enableUserWorkload: true
Procedure
Edit the
cluster-monitoring-configobject:ConfigMap$ oc -n openshift-monitoring edit configmap cluster-monitoring-configSet
toenableUserWorkload:underfalse:data/config.yamlapiVersion: v1 kind: ConfigMap metadata: name: cluster-monitoring-config namespace: openshift-monitoring data: config.yaml: | enableUserWorkload: false
- Save the file to apply the changes. Monitoring for user-defined projects is then disabled automatically.
Check that the
,prometheus-operatorandprometheus-user-workloadpods are terminated in thethanos-ruler-user-workloadproject. This might take a short while:openshift-user-workload-monitoring$ oc -n openshift-user-workload-monitoring get podExample output
No resources found in openshift-user-workload-monitoring project.
The
user-workload-monitoring-config
ConfigMap
openshift-user-workload-monitoring
ConfigMap