Bird
Raised Fist0
Kubernetesdevops~5 mins

Grafana for visualization 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
Grafana helps you see and understand data from your applications and servers. It turns numbers and logs into clear graphs and dashboards so you can spot problems or trends quickly.
When you want to monitor how your app is performing in real time with easy-to-read graphs.
When you need to combine data from different sources like databases and servers into one dashboard.
When you want to get alerts if something goes wrong, like if your server is too slow or down.
When you want to share visual reports with your team to help fix issues faster.
When you want to track usage or errors over time to improve your app.
Config File - grafana-deployment.yaml
grafana-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  labels:
    app: grafana
spec:
  replicas: 1
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
      - name: grafana
        image: grafana/grafana:9.5.2
        ports:
        - containerPort: 3000
        volumeMounts:
        - name: grafana-storage
          mountPath: /var/lib/grafana
      volumes:
      - name: grafana-storage
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: grafana-service
spec:
  type: NodePort
  selector:
    app: grafana
  ports:
  - protocol: TCP
    port: 3000
    targetPort: 3000
    nodePort: 32000

This file creates a Grafana deployment with one replica running the official Grafana image version 9.5.2. It exposes port 3000 where Grafana listens. A service of type NodePort exposes Grafana on port 32000 on the Kubernetes nodes so you can access it from outside the cluster. The emptyDir volume stores Grafana data temporarily while the pod runs.

Commands
This command creates the Grafana deployment and service in your Kubernetes cluster so Grafana starts running.
Terminal
kubectl apply -f grafana-deployment.yaml
Expected OutputExpected
deployment.apps/grafana created service/grafana-service created
This command checks if the Grafana pod is running by listing pods with the label app=grafana.
Terminal
kubectl get pods -l app=grafana
Expected OutputExpected
NAME READY STATUS RESTARTS AGE grafana-6d4f7b7f6b-abcde 1/1 Running 0 30s
-l - Filter pods by label
This command shows the details of the Grafana service, including the NodePort to access Grafana from outside the cluster.
Terminal
kubectl get svc grafana-service
Expected OutputExpected
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE grafana-service NodePort 10.96.123.45 <none> 3000:32000/TCP 1m
Key Concept

If you remember nothing else from this pattern, remember: Grafana runs as a pod in Kubernetes and you expose it with a service so you can open its dashboard in your browser.

Common Mistakes
Not exposing the Grafana service with a NodePort or LoadBalancer.
Without exposing the service, you cannot access the Grafana web interface from outside the cluster.
Always create a service with type NodePort or LoadBalancer to access Grafana.
Using an incorrect port number when accessing Grafana in the browser.
Grafana listens on port 3000 inside the pod, but the NodePort might be different, so the browser must use the NodePort number.
Check the NodePort assigned by the service and use that port in your browser URL.
Not waiting for the Grafana pod to be in Running status before accessing it.
If the pod is not ready, Grafana will not respond and the connection will fail.
Use kubectl get pods to confirm the pod is Running before trying to open Grafana.
Summary
Create a Kubernetes deployment and service to run Grafana and expose it on a NodePort.
Check the pod status to ensure Grafana is running before accessing it.
Use the service's NodePort to open Grafana's web interface in your browser.

Practice

(1/5)
1. What is the main purpose of Grafana in a Kubernetes environment?
easy
A. To visualize and monitor data from Kubernetes clusters
B. To deploy applications automatically
C. To manage Kubernetes user permissions
D. To store container images

Solution

  1. Step 1: Understand Grafana's role

    Grafana is a tool designed to create visual dashboards from data sources.
  2. Step 2: Connect Grafana to Kubernetes data

    In Kubernetes, Grafana connects to metrics sources to visualize cluster health and performance.
  3. Final Answer:

    To visualize and monitor data from Kubernetes clusters -> Option A
  4. Quick Check:

    Grafana = Visualization and Monitoring [OK]
Hint: Grafana = Visualize data, not deploy or store [OK]
Common Mistakes:
  • Confusing Grafana with deployment tools
  • Thinking Grafana manages permissions
  • Assuming Grafana stores images
2. Which Kubernetes resource is commonly used to deploy Grafana?
easy
A. Pod
B. Deployment
C. ConfigMap
D. ServiceAccount

Solution

  1. Step 1: Identify deployment method

    Grafana runs as an application that needs to be managed and scaled.
  2. Step 2: Choose Kubernetes resource for managing apps

    Deployments manage pods and allow updates and scaling.
  3. Final Answer:

    Deployment -> Option B
  4. Quick Check:

    Deployments = Manage app lifecycle [OK]
Hint: Use Deployment to run and scale Grafana pods [OK]
Common Mistakes:
  • Using Pod directly without Deployment
  • Confusing ConfigMap with deployment
  • Thinking ServiceAccount deploys apps
3. Given this snippet of a Grafana dashboard JSON, what type of visualization will it create?
{
  "panels": [
    {
      "type": "graph",
      "title": "CPU Usage"
    }
  ]
}
medium
A. A graph chart displaying CPU usage over time
B. A table showing CPU usage data
C. A text panel with CPU usage summary
D. A heatmap of CPU usage

Solution

  1. Step 1: Identify panel type in JSON

    The panel type is "graph", which means a line or bar chart.
  2. Step 2: Match visualization to type

    Graph panels show data trends over time, suitable for CPU usage.
  3. Final Answer:

    A graph chart displaying CPU usage over time -> Option A
  4. Quick Check:

    Panel type 'graph' = Chart visualization [OK]
Hint: Panel type 'graph' means line/bar chart [OK]
Common Mistakes:
  • Confusing 'graph' with 'table'
  • Assuming 'graph' means text
  • Mixing heatmap with graph
4. You deployed Grafana on Kubernetes but the dashboard shows no data. Which fix is most likely correct?
medium
A. Increase the CPU limits of the Grafana pod
B. Restart the Kubernetes cluster
C. Check if the data source is configured and connected properly
D. Delete the Grafana deployment and recreate it

Solution

  1. Step 1: Identify cause of no data in Grafana

    No data usually means Grafana cannot read from its data source.
  2. Step 2: Verify data source configuration

    Ensure the data source (like Prometheus) is added and reachable in Grafana settings.
  3. Final Answer:

    Check if the data source is configured and connected properly -> Option C
  4. Quick Check:

    No data = Check data source connection [OK]
Hint: No data? Verify data source setup first [OK]
Common Mistakes:
  • Restarting cluster unnecessarily
  • Deleting deployment without checking config
  • Changing CPU limits unrelated to data
5. You want to create a Grafana dashboard that shows CPU and memory usage side by side for multiple Kubernetes nodes. Which approach is best?
hard
A. Use a text panel describing CPU and memory usage
B. Use a single panel with combined CPU and memory metrics in one graph
C. Create separate dashboards for CPU and memory usage
D. Create a dashboard JSON with two panels: one for CPU and one for memory, each querying node metrics

Solution

  1. Step 1: Understand dashboard layout needs

    Side by side means multiple panels on one dashboard.
  2. Step 2: Design panels for each metric

    Create one panel for CPU and another for memory, each querying node metrics separately.
  3. Step 3: Avoid combining unrelated metrics in one graph

    Separate panels improve clarity and comparison.
  4. Final Answer:

    Create a dashboard JSON with two panels: one for CPU and one for memory, each querying node metrics -> Option D
  5. Quick Check:

    Separate panels = Clear side-by-side view [OK]
Hint: Use separate panels for different metrics side by side [OK]
Common Mistakes:
  • Combining CPU and memory in one confusing graph
  • Making separate dashboards instead of one
  • Using text panels instead of graphs