Exercises

Exercise 1: Basic Label Filtering

In this exercise, you will practice writing queries with label selectors to filter metrics in your OpenShift cluster.

Step 1: Access Prometheus

  1. Open a terminal and authenticate to your OpenShift cluster:

oc login <your-cluster-url>
  1. Port-forward to the Prometheus service:

oc port-forward -n openshift-monitoring svc/prometheus-k8s 9090:9090
  1. Open your browser and navigate to http://localhost:9090

Step 2: Query Container Memory Usage

Write a query to find all container memory usage metrics in the default namespace:

container_memory_usage_bytes{namespace="default"}

Execute this query in the Prometheus UI and observe the results.

Step 3: Filter by Pod Pattern

Modify the query to only show metrics for pods whose names start with "web-":

container_memory_usage_bytes{namespace="default", pod=~"^web-.*"}

Step 4: Exclude System Namespaces

Write a query to show container CPU usage for all namespaces except system namespaces:

container_cpu_usage_seconds_total{namespace!~"kube-.*|openshift-.*"}

Exercise 2: Regular Expression Patterns

Practice using regular expressions to match patterns in label values.

Step 1: Match Pod Names with Version Numbers

Write a query to find all pods with names containing a version number pattern (e.g., "v1", "v2"):

kube_pod_info{pod=~".*-v[0-9]+.*"}

Step 2: Match Specific Deployment Pattern

Find all pods that belong to deployments with names matching the pattern "app-*-prod":

kube_pod_info{deployment=~"^app-.*-prod$"}

Step 3: Exclude Test and Dev Environments

Write a query to show container memory usage excluding test and development namespaces:

container_memory_usage_bytes{namespace!~".*-test|.*-dev"}

Exercise 3: Combining Multiple Filters

Practice combining multiple label selectors and operators.

Step 1: Filter by Multiple Conditions

Write a query to find container memory usage in the "production" namespace for containers with names starting with "app-":

container_memory_usage_bytes{namespace="production", container=~"^app-.*"}

Step 2: Use Comparison Operators

Find all containers using more than 500MB of memory:

container_memory_usage_bytes > 524288000

Step 3: Combine Filters and Comparisons

Find containers in the "production" namespace using more than 1GB of memory:

container_memory_usage_bytes{namespace="production"} > 1073741824

Exercise 4: OpenShift-Specific Queries

Practice writing queries using OpenShift-specific labels and metrics.

Step 1: Query Pod Information

List all pods in a specific namespace with their node assignments:

kube_pod_info{namespace="your-namespace"}

Replace "your-namespace" with an actual namespace in your cluster.

Step 2: Filter by Node

Find all pods running on a specific node:

kube_pod_info{node="<node-name>"}

Replace <node-name> with an actual node name from your cluster. You can find node names using:

oc get nodes

Step 3: Query Build Information

Query OpenShift build information for builds in a specific namespace:

openshift_build_info{namespace="your-namespace"}

Verification

After completing these exercises, verify your understanding:

  • Can you write a query that filters metrics by multiple labels?

  • Can you use regular expressions to match patterns?

  • Can you exclude specific values using negative operators?

  • Can you combine filters with comparison operators?

If you can answer yes to all these questions, you are ready to proceed to the next module on aggregation functions.