Chapter 6. Enabling monitoring for user-defined projects
In OpenShift Container Platform, 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.
6.1. Enabling monitoring for user-defined projects Copy linkLink copied to clipboard!
Cluster administrators can enable monitoring for user-defined projects by setting the enableUserWorkload: true field in the cluster monitoring ConfigMap object.
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 cluster role to enable monitoring for user-defined projects in OpenShift Container Platform. Cluster administrators can then optionally grant users permission to configure the components that are responsible for monitoring user-defined projects.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role. -
You have installed the OpenShift CLI (
oc). -
You have created the
cluster-monitoring-configConfigMapobject. You have optionally created and configured the
user-workload-monitoring-configConfigMapobject in theopenshift-user-workload-monitoringproject. You can add configuration options to thisConfigMapobject for the components that monitor user-defined projects.NoteEvery time you save configuration changes to the
user-workload-monitoring-configConfigMapobject, the pods in theopenshift-user-workload-monitoringproject are redeployed. It might sometimes take a while for these components to redeploy.
Procedure
Edit the
cluster-monitoring-configConfigMapobject:oc -n openshift-monitoring edit configmap cluster-monitoring-config
$ oc -n openshift-monitoring edit configmap cluster-monitoring-configCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add
enableUserWorkload: trueunderdata/config.yaml:Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 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.
NoteIf you enable monitoring for user-defined projects, the
user-workload-monitoring-configConfigMapobject is created by default.Verify that the
prometheus-operator,prometheus-user-workload, andthanos-ruler-user-workloadpods are running in theopenshift-user-workload-monitoringproject. It might take a short while for the pods to start:oc -n openshift-user-workload-monitoring get pod
$ oc -n openshift-user-workload-monitoring get podCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.2. Granting users permission to monitor user-defined projects Copy linkLink copied to clipboard!
As a cluster administrator, you can monitor all core OpenShift Container Platform and user-defined projects.
You can also grant developers and other users different permissions:
- Monitoring user-defined projects
- Configuring the components that monitor user-defined projects
- Configuring alert routing for user-defined projects
- Managing alerts and silences for user-defined projects
You can grant the permissions by assigning one of the following monitoring roles or cluster roles:
| Role name | Description | Project |
|---|---|---|
|
|
Users with this role can edit the |
|
|
| Users with this role have read access to the user-defined Alertmanager API for all projects, if the user-defined Alertmanager is enabled. |
|
|
| Users with this role have read and write access to the user-defined Alertmanager API for all projects, if the user-defined Alertmanager is enabled. |
|
| Cluster role name | Description | Project |
|---|---|---|
|
|
Users with this cluster role have read access to |
Can be bound with |
|
|
Users with this cluster role can create, modify, and delete |
Can be bound with |
|
|
Users with this cluster role have the same privileges as users with the |
Can be bound with |
|
|
Users with this cluster role can create, update, and delete |
Can be bound with |
6.2.1. Granting user permissions by using the web console Copy linkLink copied to clipboard!
You can grant users permissions for the openshift-monitoring project or their own projects, by using the OpenShift Container Platform web console.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role. - The user account that you are assigning the role to already exists.
Procedure
-
In the Administrator perspective of the OpenShift Container Platform web console, go to User Management
RoleBindings 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 project where you want to grant the access.
ImportantThe monitoring role or cluster role permissions that you grant to a user by using this procedure apply only to the project that you select in the Namespace field.
- Select a monitoring role or cluster role from the Role Name list.
- In the Subject section, select User.
- In the Subject Name field, enter the name of the user.
- Select Create to apply the role binding.
6.2.2. Granting user permissions by using the CLI Copy linkLink copied to clipboard!
You can grant users permissions for the openshift-monitoring project or their own projects, by using the OpenShift CLI (oc).
Whichever role or cluster role you choose, you must bind it against a specific project as a cluster administrator.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role. - The user account that you are assigning the role to already exists.
-
You have installed the OpenShift CLI (
oc).
Procedure
To assign a monitoring role to a user for a project, enter the following command:
oc adm policy add-role-to-user <role> <user> -n <namespace> --role-namespace <namespace>
$ oc adm policy add-role-to-user <role> <user> -n <namespace> --role-namespace <namespace>1 Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Substitute
<role>with the wanted monitoring role,<user>with the user to whom you want to assign the role, and<namespace>with the project where you want to grant the access.
To assign a monitoring cluster role to a user for a project, enter the following command:
oc adm policy add-cluster-role-to-user <cluster-role> <user> -n <namespace>
$ oc adm policy add-cluster-role-to-user <cluster-role> <user> -n <namespace>1 Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Substitute
<cluster-role>with the wanted monitoring cluster role,<user>with the user to whom you want to assign the cluster role, and<namespace>with the project where you want to grant the access.
6.3. Granting users permission to configure monitoring for user-defined projects Copy linkLink copied to clipboard!
As a cluster administrator, you can assign the user-workload-monitoring-config-edit role to a user. This grants permission to configure and manage monitoring for user-defined projects without giving them permission to configure and manage core OpenShift Container Platform monitoring components.
Prerequisites
-
You have access to the cluster as a user with the
cluster-admincluster role. - The user account that you are assigning the role to already exists.
-
You have installed the OpenShift CLI (
oc).
Procedure
Assign the
user-workload-monitoring-config-editrole to a user in theopenshift-user-workload-monitoringproject:oc -n openshift-user-workload-monitoring adm policy add-role-to-user \ user-workload-monitoring-config-edit <user> \ --role-namespace 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-monitoringCopy to Clipboard Copied! Toggle word wrap Toggle overflow Verify that the user is correctly assigned to the
user-workload-monitoring-config-editrole by displaying the related role binding:oc describe rolebinding <role_binding_name> -n openshift-user-workload-monitoring
$ oc describe rolebinding <role_binding_name> -n openshift-user-workload-monitoringCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example command
oc describe rolebinding user-workload-monitoring-config-edit -n openshift-user-workload-monitoring
$ oc describe rolebinding user-workload-monitoring-config-edit -n openshift-user-workload-monitoringCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- In this example,
user1is assigned to theuser-workload-monitoring-config-editrole.
6.4. Accessing metrics from outside the cluster for custom applications Copy linkLink copied to clipboard!
You can query Prometheus metrics from outside the cluster when monitoring your own services with user-defined projects. Access this data from outside the cluster by using the thanos-querier route.
This access only supports using a bearer token for authentication.
Prerequisites
- You have deployed your own service, following the "Enabling monitoring for user-defined projects" procedure.
-
You are logged in to an account with the
cluster-monitoring-viewcluster role, which provides permission to access the Thanos Querier API. You are logged in to an account that has permission to get the Thanos Querier API route.
NoteIf your account does not have permission to get the Thanos Querier API route, a cluster administrator can provide the URL for the route.
Procedure
Extract an authentication token to connect to Prometheus by running the following command:
TOKEN=$(oc whoami -t)
$ TOKEN=$(oc whoami -t)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Extract the
thanos-querierAPI route URL by running the following command:HOST=$(oc -n openshift-monitoring get route thanos-querier -ojsonpath='{.status.ingress[].host}')$ HOST=$(oc -n openshift-monitoring get route thanos-querier -ojsonpath='{.status.ingress[].host}')Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set the namespace to the namespace in which your service is running by using the following command:
NAMESPACE=ns1
$ NAMESPACE=ns1Copy to Clipboard Copied! Toggle word wrap Toggle overflow Query the metrics of your own services in the command line by running the following command:
curl -H "Authorization: Bearer $TOKEN" -k "https://$HOST/api/v1/query?" --data-urlencode "query=up{namespace='$NAMESPACE'}"$ curl -H "Authorization: Bearer $TOKEN" -k "https://$HOST/api/v1/query?" --data-urlencode "query=up{namespace='$NAMESPACE'}"Copy to Clipboard Copied! Toggle word wrap Toggle overflow The output shows the status for each application pod that Prometheus is scraping:
The formatted example output
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Note-
The formatted example output uses a filtering tool, such as
jq, to provide the formatted indented JSON. See the jq Manual (jq documentation) for more information about usingjq. - The command requests an instant query endpoint of the Thanos Querier service, which evaluates selectors at one point in time.
-
The formatted example output uses a filtering tool, such as
6.5. Excluding a user-defined project from monitoring Copy linkLink copied to clipboard!
Individual user-defined projects can be excluded from user workload monitoring. To do so, add the openshift.io/user-monitoring label to the project’s namespace with a value of false.
Procedure
Add the label to the project namespace:
oc label namespace my-project 'openshift.io/user-monitoring=false'
$ oc label namespace my-project 'openshift.io/user-monitoring=false'Copy to Clipboard Copied! Toggle word wrap Toggle overflow To re-enable monitoring, remove the label from the namespace:
oc label namespace my-project 'openshift.io/user-monitoring-'
$ oc label namespace my-project 'openshift.io/user-monitoring-'Copy to Clipboard Copied! Toggle word wrap Toggle overflow 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.
6.6. Disabling monitoring for user-defined projects Copy linkLink copied to clipboard!
After enabling monitoring for user-defined projects, you can disable it again by setting enableUserWorkload: false in the cluster monitoring ConfigMap object.
Alternatively, you can remove enableUserWorkload: true to disable monitoring for user-defined projects.
Procedure
Edit the
cluster-monitoring-configConfigMapobject:oc -n openshift-monitoring edit configmap cluster-monitoring-config
$ oc -n openshift-monitoring edit configmap cluster-monitoring-configCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set
enableUserWorkload:tofalseunderdata/config.yaml:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
- Save the file to apply the changes. Monitoring for user-defined projects is then disabled automatically.
Check that the
prometheus-operator,prometheus-user-workloadandthanos-ruler-user-workloadpods are terminated in theopenshift-user-workload-monitoringproject. This might take a short while:oc -n openshift-user-workload-monitoring get pod
$ oc -n openshift-user-workload-monitoring get podCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example output
No resources found in openshift-user-workload-monitoring project.
No resources found in openshift-user-workload-monitoring project.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The user-workload-monitoring-config ConfigMap object in the openshift-user-workload-monitoring project is not automatically deleted when monitoring for user-defined projects is disabled. This is to preserve any custom configurations that you may have created in the ConfigMap object.