This is a cache of https://docs.openshift.com/container-platform/4.9/monitoring/enabling-monitoring-for-user-defined-projects.html. It is a snapshot of the page at 2024-11-29T18:48:30.116+0000.
Enabling monitoring for user-defined projects | Monitoring | OpenShift Container Platform 4.9
×

In OpenShift Container Platform 4.9, you can enable monitoring for user-defined projects in addition to the default platform monitoring. You can now monitor your own projects in OpenShift Container Platform without the need for an additional monitoring solution. Using this new 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.

Enabling monitoring for user-defined projects

Cluster administrators can enable monitoring for user-defined projects by setting the enableUserWorkload: true field in the cluster monitoring ConfigMap object.

In OpenShift Container Platform 4.9 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 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-admin role.

  • You have installed the OpenShift CLI (oc).

  • You have created the cluster-monitoring-config ConfigMap object.

  • You have optionally created and configured the user-workload-monitoring-config ConfigMap object in the openshift-user-workload-monitoring project. You can add configuration options to this ConfigMap object for the components that monitor user-defined projects.

    Every time you save configuration changes to the user-workload-monitoring-config ConfigMap object, the pods in the openshift-user-workload-monitoring project are redeployed. It can sometimes take a while for these components to redeploy. You can create and configure the ConfigMap object before you first enable monitoring for user-defined projects, to prevent having to redeploy the pods often.

Procedure
  1. Edit the cluster-monitoring-config ConfigMap object:

    $ oc -n openshift-monitoring edit configmap cluster-monitoring-config
  2. Add enableUserWorkload: true under data/config.yaml:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cluster-monitoring-config
      namespace: openshift-monitoring
    data:
      config.yaml: |
        enableUserWorkload: true (1)
    1 When set to true, the enableUserWorkload parameter enables monitoring for user-defined projects in a cluster.
  3. Save the file to apply the changes. Monitoring for user-defined projects is then enabled automatically.

    When changes are saved to the cluster-monitoring-config ConfigMap object, the pods and other resources in the openshift-monitoring project might be redeployed. The running monitoring processes in that project might also be restarted.

  4. Check that the prometheus-operator, prometheus-user-workload and thanos-ruler-user-workload pods are running in the openshift-user-workload-monitoring project. It might take a short while for the pods to start:

    $ oc -n openshift-user-workload-monitoring get pod
    Example 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

Granting users permission to monitor user-defined projects

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 role provides read access to PrometheusRule custom resources for a project.

  • The monitoring-rules-edit role grants a user permission to create, modify, and deleting PrometheusRule custom resources for a project.

  • The monitoring-edit role grants the same privileges as the monitoring-rules-edit 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 ServiceMonitor and PodMonitor resources.

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 openshift-user-workload-monitoring project enables you to edit the user-workload-monitoring-config ConfigMap object. With this role, you can edit the ConfigMap object to configure Prometheus, Prometheus Operator and Thanos Ruler for user-defined workload monitoring.

This section provides details on how to assign these roles by using the OpenShift Container Platform web console or the CLI.

Granting user permissions by using the web console

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-admin role.

  • The user account that you are assigning the role to already exists.

Procedure
  1. In the Administrator perspective within the OpenShift Container Platform web console, navigate to User ManagementRole BindingsCreate Binding.

  2. In the Binding Type section, select the "Namespace Role Binding" type.

  3. In the Name field, enter a name for the role binding.

  4. In the Namespace field, select the user-defined project where you want to grant the access.

    The 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.

  5. Select monitoring-rules-view, monitoring-rules-edit, or monitoring-edit in the Role Name list.

  6. In the Subject section, select User.

  7. In the Subject Name field, enter the name of the user.

  8. Select Create to apply the role binding.

Granting user permissions by using the CLI

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-admin role.

  • 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> with monitoring-rules-view, monitoring-rules-edit, or monitoring-edit.

    Whichever role you choose, you must bind it against a specific project as a cluster administrator.

    As an example, substitute <role> with monitoring-edit, <user> with johnsmith, and <namespace> with ns1. This assigns the user johnsmith permission to set up metrics collection and to create alerting rules in the ns1 namespace.

Granting users permission to configure monitoring for user-defined projects

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-admin 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-edit role to a user in the openshift-user-workload-monitoring project:

    $ oc -n openshift-user-workload-monitoring adm policy add-role-to-user \
      user-workload-monitoring-config-edit <user> \
      --role-namespace openshift-user-workload-monitoring

Accessing metrics from outside the cluster for custom applications

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 route.

Prerequisites
  • You deployed your own service, following the Enabling monitoring for user-defined projects procedure.

Procedure
  1. 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`
  2. Extract your route host:

    $ THANOS_QUERIER_HOST=`oc get route thanos-querier -n openshift-monitoring -o json | jq -r '.spec.host'`
  3. 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"]}]}}

Excluding a user-defined project from monitoring

Individual user-defined projects can be excluded from user workload monitoring. To do so, simply add the openshift.io/user-monitoring label to the project’s namespace with a value of false.

Procedure
  1. Add the label to the project namespace:

    $ oc label namespace my-project 'openshift.io/user-monitoring=false'
  2. To re-enable monitoring, remove the label from the namespace:

    $ oc label namespace my-project 'openshift.io/user-monitoring-'

    If 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.

Disabling monitoring for user-defined projects

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
  1. Edit the cluster-monitoring-config ConfigMap object:

    $ oc -n openshift-monitoring edit configmap cluster-monitoring-config
    1. Set enableUserWorkload: to false under data/config.yaml:

      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: cluster-monitoring-config
        namespace: openshift-monitoring
      data:
        config.yaml: |
          enableUserWorkload: false
  2. Save the file to apply the changes. Monitoring for user-defined projects is then disabled automatically.

  3. Check that the prometheus-operator, prometheus-user-workload and thanos-ruler-user-workload pods are terminated in the openshift-user-workload-monitoring project. This might take a short while:

    $ oc -n openshift-user-workload-monitoring get pod
    Example output
    No resources found in openshift-user-workload-monitoring project.

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.

Next steps