Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Step-by-step guide to configuring alerts in Cluster Observability Operator

December 16, 2024
Christina Zhang
Related topics:
ObservabilityOperators
Related products:
Red Hat OpenShiftRed Hat OpenShift Container Platform

Share:

    The first installment in this series provided a brief introduction to the Cluster Observability Operator (COO). This article serves as a detailed guide for configuring alerts within the COO namespace. In the following sections, I will present specific examples for two structural configurations within the COO monitoring stack.

    Case 1: One monitoring stack for multiple namespace. 

    Create a namespace to host the COO instance and utilize this monitoring stack to monitor the namespaces coo-ns2, coo-ns3, and coo-ns4 as shown in Figures 1 and 2.

    COO Monitoring Stack structure.
    Figure 1: COO Monitoring Stack structure.
    One Monitoring Stack for multiple namespace structure.
    Figure 2: One Monitoring Stack for multiple namespace structure.

    Create a project to host the COO monitoring stack. Give a label for the namespace monitoring.rhobs: coo-stack:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: coo-namespace
      labels:
        monitoring.rhobs:coo-stack

    Create a COO monitoring stack, ensuring that the namespaceSelector matches the project label monitoring.rhobs:coo-stack. The resourceSelector should be set to k8s-app: prometheus-coo-example-monitor. This label should match the ServiceMonitor label. See below:

    apiVersion: monitoring.rhobs/v1alpha1
    kind: MonitoringStack
    metadata:
      name: my-coo-monitoring-stack
      namespace: coo-namespace
    spec:
      alertmanagerConfig:
        disabled: false
      logLevel: debug
      namespaceSelector:
        matchLabels:
          monitoring.rhobs: coo-stack
      prometheusConfig:
        replicas: 2
      resourceSelector:
        matchLabels:
          k8s-app: prometheus-coo-example-monitor
      resources:
        limits:
          cpu: 500m
          memory: 512Mi
        requests:
          cpu: 100m
          memory: 256Mi
      retention: 16d

    Set the label app: prometheus-coo-example for the deployment/pod you wish to monitor:

    apiVersion: monitoring.rhobs/v1
    kind: ServiceMonitor
    metadata:
     labels:
       k8s-app: prometheus-coo-example-monitor
     name: coo-servicemonitor-forcoonamespace
     namespace: coo-namespace
    spec:
     endpoints:
     - interval: 30s
       port: web
       scheme: http
     selector:
       matchLabels:
         app: prometheus-coo-example

    Check the info below namespace coo-namespace, shown in Figure 3.

    Info below coo-namespace.
    Figure 3: Info below coo-namespace.

    Create the namespace coo-ns2, also with label monitoring.rhobs: coo-stack:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: coo-ns2
      labels:
        monitoring.rhobs: coo-stack

    Run a Deployment:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-coo-example
      name: coo-example-app2
      namespace: coo-ns2
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-coo-example
      template:
        metadata:
          labels:
            app: prometheus-coo-example
        spec:
          containers:
          - image: ghcr.io/rhobs/prometheus-example-app:0.4.2
            imagePullPolicy: IfNotPresent
            name: coo-example-app2

    Create a Service:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
       app: prometheus-coo-example
      name: coo-example-service-app2 # alter 条件是这个
      namespace: coo-ns2
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
        name: web
      selector:
        app: prometheus-coo-example
      type: ClusterIP

    Also create a Service Monitor:

    apiVersion: monitoring.rhobs/v1
    kind: ServiceMonitor
    metadata:
      labels:
        k8s-app: prometheus-coo-example-monitor
      name: prometheus-coo-example-servicemonitor2
      namespace: coo-ns2
    spec:
      endpoints:
      - interval: 30s
        port: web
        scheme: http
      selector:
        matchLabels:
          app: prometheus-coo-example

    Create a PrometheusRule and place the label k8s-app: prometheus-coo-example-monitor:

    apiVersion: monitoring.rhobs/v1
    kind: PrometheusRule
    metadata:
      name: coo-alert2
      namespace: coo-ns2
      labels:
        k8s-app: prometheus-coo-example-monitor
    spec:
      groups:
      - name: example
        rules:
        - alert: VersionAlert
          for: 1m
          expr: version{job="coo-example-service-app2"} > 0 
          labels:
            severity: warning

    Use the same method to create coo-ns3 and coo-ns4.

    To verify it the alert has been configured correctly, try oc get all. Find the svc. In my case, mine is my-coo-monitoring-stack-prometheus. See Figure 4.

    Locate my-coo-monitoring-stack-prometheus.
    Figure 4: Locate my-coo-monitoring-stack-prometheus.
    oc port-forward svc/my-coo-monitoring-stack-prometheus 9090:9090 --address 0.0.0.0 -n coo-namespace

    Try:

    localhost:9090

    You will see a Prometheus UI Page. Click Alert and you should find the alert there. See Figure 5.

    Prometheus Alerts page.
    Figure 5: Prometheus Alerts page.

    Next, let's move on and examine case 2. The structure in this example is shown in Figures 6 and 7.

    Monitoring Stack for single project structure.
    Figure 6: Monitoring Stack for single project structure.
    A COO Monitoring Stack project structure.
    Figure 7: A COO Monitoring Stack project structure.

    Case 2: Deploy the monitoring stack at the namespace you wish to monitor

    In this instance, deploy the monitoring stack below namespace coo-ns1.

    If you already had the namespace, assign a label for the namespace. If not, create a namespace, then make deployment and configure alerts for the namespace. Give a label for the namespace monitoring.rhobs: coo-stack:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: coo-ns1
      labels:
        monitoring.rhobs: coo-stack

    Next, create a monitoringStack. Make sure the namespaceSelector is monitoring.rhobs: coo-stack, the same as the namespace label. Give a label k8s-app: prometheus-coo-example-monitor to resourceSelector:

    apiVersion: monitoring.rhobs/v1alpha1
    kind: MonitoringStack
    metadata:
      name: my-coo-monitoring-stack
      namespace: coo-ns1
    spec:
      alertmanagerConfig:
        disabled: false
      logLevel: debug
      namespaceSelector:
        matchLabels:
          monitoring.rhobs: coo-stack
      prometheusConfig:
        replicas: 2
      resourceSelector:
        matchLabels:
          k8s-app: prometheus-coo-example-monitor
      resources:
        limits:
          cpu: 500m
          memory: 512Mi
        requests:
          cpu: 100m
          memory: 256Mi
      retention: 16d

    Create a ServiceMonitor. Give the label k8s-app: prometheus-coo-example-monitor. Also give a label app: prometheus-coo-example used for the deployment, service, and pod:

    apiVersion: monitoring.rhobs/v1
    kind: ServiceMonitor
    metadata:
     labels:
       k8s-app: prometheus-coo-example-monitor
     name: coo-servicemonitor-forcoonamespace
     namespace: coo-ns1
    spec:
     endpoints:
     - interval: 30s
       port: web
       scheme: http
     selector:
       matchLabels:
         app: prometheus-coo-example

    Create a Service. Use the same label app: prometheus-coo-example:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
       app: prometheus-coo-example
      name: coo-example-service-app1 
      namespace: coo-ns1
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
        name: web
      selector:
        app: prometheus-coo-example
      type: ClusterIP

    Create a Deployment. Use the same label  app: prometheus-coo-example:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: prometheus-coo-example
      name: coo-example-app1
      namespace: coo-ns1
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus-coo-example
      template:
        metadata:
          labels:
            app: prometheus-coo-example
        spec:
          containers:
          - image: ghcr.io/rhobs/prometheus-example-app:0.4.2
            imagePullPolicy: IfNotPresent
            name: coo-example-app1

    Create a PrometheusRule. It is to configure the alert you wish to fire once you trigger the condition. Use the label k8s-app: prometheus-coo-example-monitor  from the previous configuration:

    apiVersion: monitoring.rhobs/v1
    kind: PrometheusRule
    metadata:
      name: coo-alert1
      namespace: coo-ns1
      labels:
        k8s-app: prometheus-coo-example-monitor
    spec:
      groups:
      - name: example
        rules:
        - alert: VersionAlert
          for: 1m
          expr: version{job="coo-example-service-app1"} > 0 
          labels:
            severity: warning

    To verify it the alert has been configured correctly, try oc get all. Find the svc. In my case, mine is my-coo-monitoring-stack-prometheus:

    kubectl port-forward svc/my-coo-monitoring-stack-prometheus 9090:9090 --address 0.0.0.0 -n coo-ns1 

    Try:

    localhost:9090

    You will see a Prometheus UI Page. Click Alert and you should find the alert there. 

    This guide provides you with a step-by-step understanding of the procedure. You can modify the the configuration to satisfy your business requirements.

    Related Posts

    • Get started with the OpenShift Cluster Observability Operator

    • Set up an OpenShift cluster to deploy an application in odo CLI

    • How to manage a fleet of heterogeneous OpenShift clusters

    • Enable etcd backups for OpenShift clusters in hybrid cloud environments

    Recent Posts

    • Container starting and termination order in a pod

    • More Essential AI tutorials for Node.js Developers

    • How to run a fraud detection AI model on RHEL CVMs

    • How we use software provenance at Red Hat

    • Alternatives to creating bootc images from scratch

    What’s up next?

    Learn how to install the oc command-line interface (CLI) in order to work with your Red Hat OpenShift cluster from a command line.

    Start the activity
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue