cluster-prometheus

by startxfr

Helm chartv21.3.412App 1.5.5GPL-3.0-or-laterVerified publisher

Configure the Cluster Observability Operator and the Prometheus monitoring stacks of the application workloads, in standard namespaces or in user-defined networks

About this chart

Cluster Chart : Prometheus

Configure the Cluster Observability Operator and the Prometheus monitoring stacks of the application workloads, in standard namespaces or in user-defined networks.

This chart is part of the cluster-xxx startx helm chart series that doesn't necessarily deploy pods but rather represent a cluster configuration state orchestrated by gitops tools like ArgoCD.

Custom Prometheus instances (a Prometheus resource managed by a Prometheus operator installed next to the one of OpenShift) are not supported on OpenShift. Standalone, fully configurable Prometheus instances are provided by the Red Hat Cluster Observability Operator (COO): its MonitoringStack resource deploys a highly available Prometheus (with a Thanos sidecar) and an Alertmanager that are independent from the platform monitoring and from the user workload monitoring, and that can be deployed next to them. This chart installs the operator and manages:

  • the monitoring stacks (MonitoringStack) and the RBAC they need (federation of the platform metrics, discovery limited to some namespaces)
  • the scrape configuration of the workloads (ServiceMonitor, PodMonitor, ScrapeConfig), their rules (PrometheusRule) and the routing of the alerts (AlertmanagerConfig), linked to a stack with a label
  • the global query endpoints (ThanosQuerier)
  • the network policies opening the metrics ports of the workloads, the optional routes and the secrets (from values or through an ExternalSecret)

Requirements and guidelines

Read the startx helm-repository homepage for more information on how to use these resources.

Prerequisites

  • Access to the redhat-operators catalog of the software catalog
  • cluster-admin privileges
  • A storage class when the stacks use a persistent volume (spec.prometheusConfig.persistentVolumeClaim)
  • At least two nodes matching the node selector of a stack: its Prometheus pods and its Alertmanager pods are always scheduled on distinct nodes (two replicas each by default)

The operator only supports the AllNamespaces install mode. It is installed in the openshift-cluster-observability-operator namespace, created by the operatorNamespace block with the openshift.io/cluster-monitoring label. The stacks run in the default-prometheus namespace created by the project sub-chart.

The monitoring.rhobs API group

The operator ships its own copy of the Prometheus operator, with the same resources in a different API group: monitoring.rhobs instead of monitoring.coreos.com. A stack only reads the monitoring.rhobs resources: an existing ServiceMonitor of the monitoring.coreos.com group (the one handled by the monitoring of OpenShift) is ignored and must be declared again with this chart. The specifications are identical.

How a stack selects what it monitors

Setting of the stackRoleDefault of the operator
spec.resourceSelectorLabels of the ServiceMonitor, PodMonitor, ScrapeConfig, PrometheusRule and AlertmanagerConfig resources read by the stacknot set: no scrape configuration and no rule is read
spec.namespaceSelectorLabels of the namespaces where these resources are readnot set: the namespace of the stack
spec.createClusterRoleBindingsWith a namespace selector, bind the Prometheus service account to a cluster role reading the services, endpoints and pods of every namespaceCreateClusterRoleBindings

This chart links the resources to the stacks with the monitoring.rhobs/stack label:

  • when spec.resourceSelector is not set, the chart selects monitoring.rhobs/stack: <name of the stack>
  • every serviceMonitors, podMonitors, scrapeConfigs, prometheusRules and alertmanagerConfigs item declared with stack: <name> gets this label

The namespaces are selected with their own labels (matchLabels), by name with the kubernetes.io/metadata.name label that every namespace carries (matchExpressions), or all at once with an empty selector ({}).

To avoid the cluster wide read access, set spec.createClusterRoleBindings: NoClusterRoleBindings and list the monitored namespaces in monitoredNamespaces: the chart binds the cluster role of the stack in these namespaces only.

Workloads attached to a user-defined network

A pod of a namespace attached to a primary user-defined network (UserDefinedNetwork or ClusterUserDefinedNetwork) keeps an address on the default pod network, and this is the address Kubernetes publishes in the pod status and in the endpoints of the services. Prometheus therefore discovers this address, but OVN-Kubernetes only lets the kubelet health checks reach it: without any other setting, the targets are discovered and every scrape times out. To monitor these workloads:

  1. Keep the stack in a namespace of the default pod network (the default-prometheus project). A stack running inside a user-defined network cannot scrape its own components, reach its Alertmanager or reach the discovered targets.

  2. Open the metrics port of the workload pods on the default network with the k8s.ovn.org/open-default-ports annotation, set in the pod template of the workload:

    spec:
      template:
        metadata:
          annotations:
            k8s.ovn.org/open-default-ports: |
              - protocol: tcp
                port: 8080
    
  3. Declare the ServiceMonitor or PodMonitor in the namespace of the workload, as for any other namespace, and make this namespace match the namespace selector of the stack (the cluster-cudn chart sets labels on the namespaces it creates).

Keep in mind:

  • the opened port is reachable by every pod of the default network, and the network policies of the namespace do not filter it: only open the metrics port, and protect the metrics with TLS and an authentication when they are sensitive
  • a pod of a primary user-defined network cannot reach the services of the default network (the Kubernetes API and the DNS excepted): it cannot push its metrics to a stack (remote write, OTLP), the metrics have to be scraped
  • the namespaces that only use secondary networks (Localnet, secondary user-defined network) keep the default pod network as primary network: nothing specific is required

Access to the metrics

ComponentServicePort
Prometheus<stack>-prometheus9090 (web)
Alertmanager<stack>-alertmanager9093 (web)
Thanos Querierthanos-querier-<name>10902 (http)

These services have no authentication. Inside the cluster, use them as a Prometheus data source (Perses dashboards of the operator, Grafana) and protect them with network policies of the project sub-chart. The routes block publishes a service outside of the cluster: every client able to reach the router can then read the metrics and use the administration API, restrict the clients (haproxy.router.openshift.io/ip_allowlist annotation) or prefer oc port-forward.

Deploy this helm chart on openshift

1. Connect to your Openshift cluster

oc login -t <token> <cluster-url>

2. Install the repository

helm repo add startx https://helm-repository.readthedocs.io/en/latest/repos/stable/

3. Get information about this chart

helm show chart startx/cluster-prometheus

4. Install this chart

Default values

The default values keep every feature disabled and render nothing: the stacks and the monitored workloads are specific to each cluster and must be provided in a values file.

  • No stack and no scrape configuration are created
  • The operator subscription and namespaces are managed separately via the operator and project sub-charts and the operatorNamespace block (all disabled by default)
# base configuration running default configuration
helm install cluster-prometheus startx/cluster-prometheus

Full deployment with operator and project

Helm cannot create a custom resource before its API exists: install the namespaces and the operator first, approve its InstallPlan (the subscription uses installPlanApproval: Manual), then enable a stack and the monitoring of a workload.

# 1. namespaces and operator
helm install cluster-prometheus startx/cluster-prometheus \
  --set project.enabled=true \
  --set operatorNamespace.enabled=true \
  --set operator.enabled=true
oc -n openshift-cluster-observability-operator get installplan
oc -n openshift-cluster-observability-operator patch installplan <name> --type merge -p '{"spec":{"approved":true}}'

# 2. stack, scrape configuration and alert of the my-app workload, once the operator is running
cat > my-prometheus.yaml <<EOF
monitoringStacks:
  enabled: true
  list:
    - name: apps
      spec:
        retention: 7d
        namespaceSelector:
          matchExpressions:
            - key: kubernetes.io/metadata.name
              operator: In
              values:
                - default-prometheus
                - my-app
        prometheusConfig:
          persistentVolumeClaim:
            resources:
              requests:
                storage: 20Gi
serviceMonitors:
  enabled: true
  list:
    - name: my-app
      namespace: my-app
      stack: apps
      spec:
        selector:
          matchLabels:
            app.kubernetes.io/name: my-app
        endpoints:
          - port: metrics
            interval: 30s
prometheusRules:
  enabled: true
  list:
    - name: my-app
      namespace: my-app
      stack: apps
      spec:
        groups:
          - name: my-app
            rules:
              - alert: MyAppDown
                expr: up{job="my-app"} == 0
                for: 5m
                labels:
                  severity: critical
EOF
helm upgrade cluster-prometheus startx/cluster-prometheus -f my-prometheus.yaml \
  --set project.enabled=true \
  --set operatorNamespace.enabled=true \
  --set operator.enabled=true

Check the targets of the stack with oc -n default-prometheus port-forward svc/apps-prometheus 9090 and http://localhost:9090/targets.

Workload of a user-defined network

The values are the same as for a standard namespace. Only the workload changes: its pod template carries the k8s.ovn.org/open-default-ports annotation (see Workloads attached to a user-defined network). A PodMonitor scrapes the pods directly:

podMonitors:
  enabled: true
  list:
    - name: my-udn-app
      namespace: my-udn-app
      stack: apps
      spec:
        selector:
          matchLabels:
            app.kubernetes.io/name: my-udn-app
        podMetricsEndpoints:
          - port: metrics
            interval: 30s

Metrics of the platform monitoring

The CPU, memory and state metrics of the pods (container_*, kube_*) are collected by the platform monitoring of OpenShift. A stack reads the ones it needs from the federation endpoint of the platform Prometheus: clusterMonitoringView: true grants the access, and a ServiceMonitor selecting the Prometheus service of the stack itself is relabeled to the platform Prometheus:

monitoringStacks:
  enabled: true
  list:
    - name: apps
      clusterMonitoringView: true
serviceMonitors:
  enabled: true
  list:
    - name: federate-platform
      stack: apps
      spec:
        selector:
          matchLabels:
            app.kubernetes.io/managed-by: observability-operator
            app.kubernetes.io/name: apps-prometheus
        endpoints:
          - port: web
            scheme: https
            path: /federate
            interval: 30s
            honorLabels: true
            params:
              'match[]':
                - '{__name__=~"container_cpu_usage_seconds_total|container_memory_working_set_bytes", namespace="my-app"}'
                - '{__name__=~"kube_pod_.*", namespace="my-app"}'
            relabelings:
              - action: replace
                targetLabel: __address__
                replacement: prometheus-k8s.openshift-monitoring.svc:9091
              - action: labeldrop
                regex: pod|namespace|service|endpoint|container
            bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
            tlsConfig:
              serverName: prometheus-k8s.openshift-monitoring.svc
              ca:
                configMap:
                  name: openshift-service-ca.crt
                  key: service-ca.crt

Only federate the metrics you need: the request is evaluated by the platform Prometheus at every scrape.

Other available values

  • startx : one startx stack on the infra nodes (two Prometheus replicas with a 20Gi volume, seven days of retention, Alertmanager) monitoring the namespaces labelled `monitoring.rhobs/stack:

Deploy cluster-prometheus with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has cluster-prometheus on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartcluster-prometheus is pinned at v21.3.412 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply