Bird
Raised Fist0
Kubernetesdevops~5 mins

Prometheus for metrics collection in Kubernetes - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Prometheus helps you collect and store data about how your applications and servers are performing. It solves the problem of knowing what is happening inside your system by gathering metrics you can watch and analyze.
When you want to monitor the health of your Kubernetes applications in real time.
When you need to track how much CPU or memory your services are using.
When you want to set alerts to notify you if something goes wrong in your system.
When you want to visualize performance trends over time with graphs.
When you need a reliable way to collect metrics from multiple servers and containers.
Config File - prometheus-deployment.yaml
prometheus-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  labels:
    app: prometheus
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      containers:
      - name: prometheus
        image: prom/prometheus:v2.44.0
        args:
        - --config.file=/etc/prometheus/prometheus.yml
        ports:
        - containerPort: 9090
        volumeMounts:
        - name: config-volume
          mountPath: /etc/prometheus/
      volumes:
      - name: config-volume
        configMap:
          name: prometheus-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
    scrape_configs:
      - job_name: 'kubernetes'
        static_configs:
          - targets: ['localhost:9090']

This file creates a Prometheus deployment in Kubernetes with one replica.

The ConfigMap holds the Prometheus configuration, telling it to scrape metrics every 15 seconds from itself (localhost:9090).

The deployment mounts this config so Prometheus knows where to get its settings.

Commands
This command creates the Prometheus deployment and its configuration in your Kubernetes cluster.
Terminal
kubectl apply -f prometheus-deployment.yaml
Expected OutputExpected
deployment.apps/prometheus created configmap/prometheus-config created
This command checks if the Prometheus pod is running by listing pods with the label 'app=prometheus'.
Terminal
kubectl get pods -l app=prometheus
Expected OutputExpected
NAME READY STATUS RESTARTS AGE prometheus-6d4b7f7f7f-abcde 1/1 Running 0 30s
-l - Filter pods by label
This command forwards your local machine's port 9090 to the Prometheus pod's port 9090 so you can access the Prometheus web interface in your browser at http://localhost:9090.
Terminal
kubectl port-forward deployment/prometheus 9090:9090
Expected OutputExpected
Forwarding from 127.0.0.1:9090 -> 9090 Forwarding from [::1]:9090 -> 9090
Key Concept

If you remember nothing else from this pattern, remember: Prometheus collects metrics by regularly scraping configured targets and stores them for monitoring and alerting.

Common Mistakes
Not creating the ConfigMap before deploying Prometheus.
Prometheus needs its configuration file to know what to monitor; without it, it won't start properly.
Always apply the ConfigMap with the Prometheus configuration before or together with the deployment.
Trying to access Prometheus without port-forwarding or a service.
Prometheus runs inside the cluster and is not accessible from outside by default.
Use kubectl port-forward or create a Kubernetes service to expose Prometheus.
Setting scrape targets incorrectly in prometheus.yml.
If targets are wrong or unreachable, Prometheus will not collect any metrics.
Verify targets are correct and reachable from the Prometheus pod.
Summary
Create a ConfigMap with Prometheus settings to tell it what to monitor.
Deploy Prometheus in Kubernetes using a Deployment that mounts the ConfigMap.
Use kubectl commands to apply the configuration, check pod status, and access the Prometheus UI.

Practice

(1/5)
1. What is the main purpose of Prometheus in a Kubernetes environment?
easy
A. To deploy applications automatically
B. To collect and store metrics data for monitoring
C. To manage Kubernetes cluster nodes
D. To provide a user interface for Kubernetes

Solution

  1. Step 1: Understand Prometheus role

    Prometheus is designed to collect numerical data called metrics from applications and systems.
  2. Step 2: Identify its main function in Kubernetes

    In Kubernetes, Prometheus collects metrics to monitor app health and performance.
  3. Final Answer:

    To collect and store metrics data for monitoring -> Option B
  4. Quick Check:

    Prometheus collects metrics = A [OK]
Hint: Prometheus = metrics collection tool [OK]
Common Mistakes:
  • Confusing Prometheus with deployment tools
  • Thinking Prometheus manages nodes
  • Assuming Prometheus is a UI tool
2. Which Kubernetes resource is used to tell Prometheus which services to monitor?
easy
A. ServiceMonitor
B. PodMonitor
C. ConfigMap
D. Ingress

Solution

  1. Step 1: Identify Prometheus monitoring resources

    Prometheus uses special Kubernetes custom resources to know what to watch.
  2. Step 2: Recognize ServiceMonitor's role

    ServiceMonitor tells Prometheus which Kubernetes services to scrape metrics from.
  3. Final Answer:

    ServiceMonitor -> Option A
  4. Quick Check:

    ServiceMonitor selects services for Prometheus [OK]
Hint: ServiceMonitor = tells Prometheus what to watch [OK]
Common Mistakes:
  • Confusing PodMonitor with ServiceMonitor
  • Using ConfigMap for monitoring targets
  • Thinking Ingress controls Prometheus scraping
3. Given this snippet of a ServiceMonitor YAML, what is the scrape interval Prometheus will use?
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: example-monitor
spec:
  endpoints:
  - port: web
    interval: 15s
  selector:
    matchLabels:
      app: example
medium
A. 5 seconds
B. 30 seconds
C. 15 seconds
D. 1 minute

Solution

  1. Step 1: Locate the interval field in YAML

    The interval is set under endpoints as 'interval: 15s'.
  2. Step 2: Understand interval meaning

    This means Prometheus scrapes metrics every 15 seconds from the specified port.
  3. Final Answer:

    15 seconds -> Option C
  4. Quick Check:

    interval: 15s means 15 seconds [OK]
Hint: Check 'interval' value under endpoints [OK]
Common Mistakes:
  • Ignoring the interval field and guessing default
  • Confusing seconds with minutes
  • Assuming interval is global, not per endpoint
4. You created a ServiceMonitor but Prometheus is not scraping metrics from your service. Which of these is a likely cause?
medium
A. The ServiceMonitor selector labels do not match the service labels
B. The Prometheus server is not running on the cluster
C. The service port is not exposed in the ServiceMonitor endpoints
D. All of the above

Solution

  1. Step 1: Check label matching

    If ServiceMonitor selector labels don't match service labels, Prometheus won't find the service.
  2. Step 2: Verify Prometheus server status and endpoint config

    Prometheus must be running and the service port must be correctly specified in endpoints to scrape metrics.
  3. Final Answer:

    All of the above -> Option D
  4. Quick Check:

    Any mismatch or missing config stops scraping [OK]
Hint: Check labels, server status, and endpoints all match [OK]
Common Mistakes:
  • Only checking one cause and ignoring others
  • Assuming Prometheus always runs by default
  • Forgetting to expose correct port in ServiceMonitor
5. You want Prometheus to scrape metrics from multiple services with different scrape intervals. How should you configure this in Kubernetes?
hard
A. Create separate ServiceMonitor resources for each service with their specific intervals
B. Set a global scrape interval in Prometheus config and ignore ServiceMonitor intervals
C. Create one ServiceMonitor with multiple endpoints, each having its own interval
D. Use a ConfigMap to list all services and intervals for Prometheus

Solution

  1. Step 1: Understand ServiceMonitor scope

    Each ServiceMonitor targets services with specific scrape configs; intervals are per endpoint.
  2. Step 2: Manage different intervals

    To have different intervals per service, create separate ServiceMonitors with their own intervals.
  3. Step 3: Why not other options?

    One ServiceMonitor with multiple endpoints cannot set different intervals per service easily; global config overrides intervals; ConfigMap does not control scraping targets.
  4. Final Answer:

    Create separate ServiceMonitor resources for each service with their specific intervals -> Option A
  5. Quick Check:

    Separate ServiceMonitors allow different intervals [OK]
Hint: Use separate ServiceMonitors for different intervals [OK]
Common Mistakes:
  • Trying to set different intervals in one ServiceMonitor
  • Ignoring ServiceMonitor intervals in favor of global config
  • Using ConfigMap incorrectly for scraping targets