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
-
Open a terminal and authenticate to your OpenShift cluster:
oc login <your-cluster-url>
-
Port-forward to the Prometheus service:
oc port-forward -n openshift-monitoring svc/prometheus-k8s 9090:9090
-
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.
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]+.*"}
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-.*"}
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.
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.