Nextgen Blog
HomeLearnAIQuantumRoboticsSecuritySpaceAbout
Nextgen Blog

By Anubhav โ€” full stack engineer, IIT Mandi CSE. Deep dives into AI, software engineering, and the technologies shaping the next generation of tech.

Navigate

  • Home
  • Learn
  • Blog
  • Archive
  • About

Topics

  • Artificial Intelligence
  • Quantum Computing
  • Robotics
  • Cybersecurity
  • Space Tech

Connect

Built with โค๏ธ & By - Anubhav

ยฉ 2026 Nextgen Blog by Anubhav. All rights reserved.

Privacy PolicyTerms of Service
Phase 04 ยท Operations & SecurityChapter 23 of 33
22.
Phase 04Read time ~18 minDifficulty intermediate

Kubernetes Monitoring ๐Ÿ“Š

Warrior, in Chapter 21 we learned Logging.

In this chapter
  • 01What is Monitoring?
  • 02Why Logging Alone Isn't Enough
  • 03The Three Pillars
  • 04Log
  • 05Metric
  • 06Trace
  • 07Kubernetes Has Built-In Metrics?
  • 08Metrics Server
  • 09kubectl top
  • 10Monitor Pods
  • 11Monitor a Namespace
  • 12Monitoring vs Requests/Limits
  • 13Why Requests Matter for HPA
  • 14Metrics Server vs Prometheus
  • 15Metrics Server
  • 16Prometheus
  • 17What is Prometheus?
  • 18Prometheus Architecture
  • 19Prometheus Pull Model
  • 20/metrics
  • 21Node.js + Prometheus
  • 22What are Metrics?
  • 23Request count
  • 24Error count
  • 25Request duration
  • 26Active requests
  • 27Database connection count
  • 28Four Important Prometheus Metric Types
  • 29Counter
  • 30Gauge
  • 31Histogram
  • 32Percentiles: P50, P95, P99
  • 33P50
  • 34P95
  • 35P99
  • 36Why Average Latency Is Dangerous
  • 37Prometheus Labels
  • 38Don't Create High-Cardinality Labels
  • 39Prometheus Service Discovery in Kubernetes
  • 40ServiceMonitor
  • 41Prometheus + Kubernetes
  • 42What is Grafana?
  • 43Monitoring Kubernetes Nodes
  • 44Exporter Concept
  • 45Kubernetes Monitoring Architecture
  • 46kube-state-metrics
  • 47Alerting
  • 48Prometheus Alert Rule
  • 49Alerting Strategy
  • 50Four Golden Signals
  • 51Latency
  • 52Traffic
  • 53Errors
  • 54Saturation
  • 55RED Method
  • 56USE Method
  • 57Monitoring Your Node.js API
  • 58API
  • 59Node.js
  • 60PostgreSQL
  • 61Redis
  • 62Kubernetes
  • 63Monitoring HPA
  • 64HPA + Metrics Server vs Prometheus
  • 65Monitoring vs Logging
  • 66A Real Incident
  • 67Step 1 โ€” Grafana
  • 68Step 2 โ€” Traffic
  • 69Step 3 โ€” CPU
  • 70Step 4 โ€” HPA
  • 71Step 5 โ€” Logs
  • 72Step 6 โ€” Database metrics
  • 73Monitoring Architecture for Your Final Kubernetes Project
  • 74Practical Monitoring Commands
  • 75Nodes
  • 76Pods
  • 77Namespace
  • 78All namespaces
  • 79Pod status
  • 80Deployment status
  • 81HPA
  • 82Detailed HPA
  • 83Important Commands for Monitoring Investigation
  • 84Interview Questions ๐ŸŽฏ
  • 85Q1. What is Metrics Server?
  • 86Q2. Metrics Server vs Prometheus?
  • 87Q3. What is Prometheus?
  • 88Q4. Does Prometheus normally push or pull metrics?
  • 89Q5. What endpoint is commonly exposed by applications?
  • 90Q6. What is Grafana?
  • 91Q7. What is a Counter?
  • 92Q8. What is a Gauge?
  • 93Q9. What is a Histogram?
  • 94Q10. What is P95?
  • 95Q11. Why is P95 useful?
  • 96Q12. What is high cardinality?
  • 97Q13. What is Node Exporter?
  • 98Q14. What is kube-state-metrics?
  • 99Q15. What are the Four Golden Signals?
  • 100Chapter 22 Cheat Sheet ๐Ÿง 
  • 101Remember:
  • 102Next โ†’ Chapter 23: Troubleshooting ๐Ÿ”ง
In this chapter (102 sections)
01What is Monitoring?02Why Logging Alone Isn't Enough03The Three Pillars04Log05Metric06Trace07Kubernetes Has Built-In Metrics?08Metrics Server09kubectl top10Monitor Pods11Monitor a Namespace12Monitoring vs Requests/Limits13Why Requests Matter for HPA14Metrics Server vs Prometheus15Metrics Server16Prometheus17What is Prometheus?18Prometheus Architecture19Prometheus Pull Model20/metrics21Node.js + Prometheus22What are Metrics?23Request count24Error count25Request duration26Active requests27Database connection count28Four Important Prometheus Metric Types29Counter30Gauge31Histogram32Percentiles: P50, P95, P9933P5034P9535P9936Why Average Latency Is Dangerous37Prometheus Labels38Don't Create High-Cardinality Labels39Prometheus Service Discovery in Kubernetes40ServiceMonitor41Prometheus + Kubernetes42What is Grafana?43Monitoring Kubernetes Nodes44Exporter Concept45Kubernetes Monitoring Architecture46kube-state-metrics47Alerting48Prometheus Alert Rule49Alerting Strategy50Four Golden Signals51Latency52Traffic53Errors54Saturation55RED Method56USE Method57Monitoring Your Node.js API58API59Node.js60PostgreSQL61Redis62Kubernetes63Monitoring HPA64HPA + Metrics Server vs Prometheus65Monitoring vs Logging66A Real Incident67Step 1 โ€” Grafana68Step 2 โ€” Traffic69Step 3 โ€” CPU70Step 4 โ€” HPA71Step 5 โ€” Logs72Step 6 โ€” Database metrics73Monitoring Architecture for Your Final Kubernetes Project74Practical Monitoring Commands75Nodes76Pods77Namespace78All namespaces79Pod status80Deployment status81HPA82Detailed HPA83Important Commands for Monitoring Investigation84Interview Questions ๐ŸŽฏ85Q1. What is Metrics Server?86Q2. Metrics Server vs Prometheus?87Q3. What is Prometheus?88Q4. Does Prometheus normally push or pull metrics?89Q5. What endpoint is commonly exposed by applications?90Q6. What is Grafana?91Q7. What is a Counter?92Q8. What is a Gauge?93Q9. What is a Histogram?94Q10. What is P95?95Q11. Why is P95 useful?96Q12. What is high cardinality?97Q13. What is Node Exporter?98Q14. What is kube-state-metrics?99Q15. What are the Four Golden Signals?100Chapter 22 Cheat Sheet ๐Ÿง 101Remember:102Next โ†’ Chapter 23: Troubleshooting ๐Ÿ”ง

Now we move to the second major pillar of observability:

Metrics โ€” numerical measurements that tell us how the system is performing.

A simple way to remember:

Logs     โ†’ What happened?
Metrics  โ†’ How much / how often?
Traces   โ†’ Where did the request spend time?

1. What is Monitoring?#

Monitoring means continuously collecting measurements from your system and using them to understand its health and performance.

For a Kubernetes application, you might want to know:

CPU usage
Memory usage
Pod count
Restart count
Request rate
Error rate
Response latency
Network traffic
Disk usage
Node health

For example:

node-api
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
CPU          72%
Memory       61%
Requests     2,450/sec
Errors       12/sec
P95 latency  180 ms
Pods         6/10

This gives you a much faster understanding of system health than reading individual logs.


2. Why Logging Alone Isn't Enough#

Imagine your API is slow.

You search logs and find:

Request received
Request completed

Nothing obviously wrong.

But metrics show:

CPU
100% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 80% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 60% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
 40%
 20%
       10AM       11AM

You immediately know:

CPU utilization increased significantly around 11 AM.

Another example:

Requests:     10,000/min
Errors:          800/min

That means your error rate is roughly:

800 / 10,000 = 8%

Metrics make this kind of system-level behavior easy to see.


3. The Three Pillars#

You should now start thinking about observability as:

                 OBSERVABILITY
                      โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ†“             โ†“             โ†“
      Logs         Metrics        Traces
        โ”‚             โ”‚             โ”‚
    Events         Numbers       Request flow
        โ”‚             โ”‚             โ”‚
   "What?"       "How much?"     "Where?"

Example:

Log#

Database query failed

Metric#

Database error rate = 7%

Trace#

API โ†’ PostgreSQL
          โ†“
      2.4 seconds

Together they give you a much better picture.


4. Kubernetes Has Built-In Metrics?#

This needs an important clarification.

Kubernetes itself exposes information about the cluster, but commands such as:

kubectl top pods
kubectl top nodes

normally require a metrics provider such as Metrics Server.

So if you run:

kubectl top pods

and get something like:

error: Metrics API not available

it often means Metrics Server isn't available/configured.


5. Metrics Server#

Metrics Server collects resource usage data from Kubernetes nodes/kubelets and exposes it through the Kubernetes Metrics API.

Conceptually:

              Kubernetes Cluster
                     โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ†“                         โ†“
     Node 1                    Node 2
        โ”‚                         โ”‚
     kubelet                   kubelet
        โ”‚                         โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ†“
              Metrics Server
                     โ”‚
                     โ†“
              Metrics API
                     โ”‚
                     โ†“
                kubectl top

It is mainly useful for basic resource metrics and autoscaling use cases.

It is not a complete observability platform.


6. kubectl top#

Once a metrics provider is available:

kubectl top nodes

might show:

NAME       CPU(cores)   CPU%   MEMORY(bytes)   MEMORY%
node-1     850m         42%    3.2Gi           40%
node-2     1200m        60%    5.1Gi           64%

This tells you current resource usage.


7. Monitor Pods#

kubectl top pods

Example:

NAME                         CPU   MEMORY
node-api-7d8f6b4c9-x2k7m    120m   250Mi
node-api-7d8f6b4c9-q8l4p    450m   600Mi
redis-5d7f8c9d7-k2m4x       80m    180Mi

Immediately you can see:

One Node.js Pod is consuming significantly more CPU/memory than another.


8. Monitor a Namespace#

Suppose your application is in:

production

Use:

kubectl top pods -n production

You can also use:

kubectl top pods -A

to inspect Pods across namespaces.


9. Monitoring vs Requests/Limits#

Remember Chapter 13.

You learned:

resources:
  requests:
    cpu: "250m"
    memory: "256Mi"
 
  limits:
    cpu: "500m"
    memory: "512Mi"

Now monitoring lets you compare actual usage.

Example:

CPU Request: 250m
CPU Usage:   220m
CPU Limit:   500m

So:

Usage / Request
= 220 / 250
= 88%

This can matter for HPA because CPU utilization targets are commonly calculated relative to CPU requests.


10. Why Requests Matter for HPA#

Suppose:

resources:
  requests:
    cpu: "500m"

Actual usage:

400m

Then utilization is approximately:

400 / 500 = 80%

If HPA target is:

70%

HPA may want more replicas.

But if your request were:

1000m

the same 400m usage would be:

400 / 1000 = 40%

So resource requests affect autoscaling behavior.

That's why:

Bad resource requests can produce bad autoscaling decisions.


11. Metrics Server vs Prometheus#

This distinction is extremely important for interviews.

Metrics Server#

Primarily provides:

CPU
Memory

resource metrics for:

  • kubectl top
  • HPA resource metrics

Prometheus#

Provides a much broader metrics collection and monitoring system.

It can collect:

CPU
Memory
Request rate
Error rate
Latency
Database metrics
Redis metrics
Application metrics
Business metrics
Kubernetes metrics
Custom metrics

Think:

Metrics Server
     โ†“
Basic Kubernetes resource metrics
 
Prometheus
     โ†“
Full monitoring/metrics platform

12. What is Prometheus?#

Prometheus is a popular open-source monitoring and alerting system.

Its main job is:

Collect and store time-series metrics.

Time-series means:

metric + value + timestamp

For example:

cpu_usage = 72%
timestamp = 10:30

Then:

10:30 โ†’ 72%
10:31 โ†’ 75%
10:32 โ†’ 81%
10:33 โ†’ 90%

Now you can graph the metric.


13. Prometheus Architecture#

Basic architecture:

                 Kubernetes
                     โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ†“             โ†“             โ†“
   Node Exporter   API Server   App Metrics
       โ”‚             โ”‚             โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                     โ†“
                 Prometheus
                     โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ†“             โ†“
           Queries        Alerts
              โ”‚
              โ†“
           Grafana

14. Prometheus Pull Model#

One of the most important concepts:

Prometheus generally pulls/scrapes metrics from targets.

For example:

Prometheus
    โ”‚
    โ”‚ GET /metrics
    โ†“
Node.js application

The application exposes:

/metrics

Prometheus periodically fetches it.

This is different from simply having your application push every metric directly to Prometheus.


15. /metrics#

Imagine your Node.js application exposes:

http://node-api:3000/metrics

It might return:

http_requests_total 15420
http_request_errors_total 321
process_cpu_seconds_total 1832

Prometheus scrapes this endpoint.


16. Node.js + Prometheus#

A popular Node.js library is:

prom-client

Example:

import client from "prom-client";
 
const register = new client.Registry();
 
client.collectDefaultMetrics({
  register
});
 
app.get("/metrics", async (req, res) => {
  res.set("Content-Type", register.contentType);
  res.end(await register.metrics());
});

Now:

GET /metrics

returns Prometheus-compatible metrics.


17. What are Metrics?#

A metric is a measurable value.

Common application metrics:

Request count#

http_requests_total

Error count#

http_errors_total

Request duration#

http_request_duration_seconds

Active requests#

http_requests_in_progress

Database connection count#

database_connections

18. Four Important Prometheus Metric Types#

Prometheus commonly works with four metric types:

Counter
Gauge
Histogram
Summary

Let's understand each.


19. Counter#

A Counter is a value that generally only increases.

Example:

HTTP requests
100
101
102
103
...

It can reset when the application restarts.

Examples:

http_requests_total
errors_total
orders_created_total

Example:

const requests = new client.Counter({
  name: "http_requests_total",
  help: "Total HTTP requests"
});

20. Gauge#

Gauge can go:

up
down
up
down

Example:

Active users
Memory usage
Queue length
Temperature

Example:

const activeUsers = new client.Gauge({
  name: "active_users",
  help: "Current active users"
});

If:

100 โ†’ 120 โ†’ 90 โ†’ 140

that's perfectly valid for a Gauge.


21. Histogram#

Histogram is very important for application performance.

Suppose you want to measure:

How long do API requests take?

You might see:

50ms
80ms
100ms
150ms
300ms
2s

A Histogram groups observations into buckets.

Example:

request duration
 
โ‰ค 0.1s    โ†’ 800 requests
โ‰ค 0.5s    โ†’ 950 requests
โ‰ค 1s      โ†’ 990 requests
โ‰ค 5s      โ†’ 1000 requests

This is extremely useful for latency analysis.


22. Percentiles: P50, P95, P99#

These are extremely important in production systems.

Suppose 100 requests were measured.

P50#

50% of requests completed within this time.

Example:

P50 = 100ms

P95#

95% completed within:

P95 = 400ms

P99#

99% completed within:

P99 = 1.2s

This means a small percentage of users are experiencing much slower responses.


23. Why Average Latency Is Dangerous#

Imagine:

99 requests = 100ms
1 request  = 10 seconds

Average:

~199ms

Looks reasonable.

But one user waited:

10 seconds

Percentiles reveal this much better.

You may see:

P50 = 100ms
P95 = 110ms
P99 = 10s

That's much more informative.


24. Prometheus Labels#

Metrics can have labels.

Example:

http_requests_total{
  method="GET",
  route="/users",
  status="200"
}

Another:

http_requests_total{
  method="POST",
  route="/orders",
  status="500"
}

Now you can query:

all POST requests

or:

all 500 errors

or:

/orders requests

25. Don't Create High-Cardinality Labels#

This is a critical production concept.

Bad:

userId="123456"

as a Prometheus label for every user.

Why?

Suppose:

10 million users

You could create millions of unique time series.

That can become extremely expensive.

Avoid labels such as:

userId
requestId
sessionId
email
random UUID

in most Prometheus metrics.

Better:

method
route
status
service
environment

For example:

http_requests_total{
  method="GET",
  route="/users/:id",
  status="200"
}

26. Prometheus Service Discovery in Kubernetes#

This is where Kubernetes becomes interesting.

You don't want to manually configure every Pod.

Prometheus can discover Kubernetes resources.

Conceptually:

Kubernetes API
      โ”‚
      โ†“
Prometheus
      โ”‚
      โ†“
Discover targets
      โ”‚
      โ†“
Scrape metrics

For example:

Pod A
Pod B
Pod C

Prometheus can discover them according to its configuration.


27. ServiceMonitor#

If you're using the Prometheus Operator / kube-prometheus-stack, you'll commonly encounter:

ServiceMonitor

It tells Prometheus how to discover and scrape metrics exposed by a Kubernetes Service.

Conceptually:

ServiceMonitor
      โ”‚
      โ†“
Service
      โ”‚
      โ†“
Pods
      โ”‚
      โ†“
/metrics
      โ”‚
      โ†“
Prometheus

Example:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: node-api
spec:
  selector:
    matchLabels:
      app: node-api
 
  endpoints:
    - port: metrics
      path: /metrics

The exact labels and port configuration depend on your Prometheus Operator setup.


28. Prometheus + Kubernetes#

Imagine:

Node.js API
   โ”‚
   โ””โ”€โ”€ /metrics
          โ”‚
          โ†“
       Service
          โ”‚
          โ†“
    Prometheus
          โ”‚
          โ†“
       Grafana

Now Grafana can display:

Requests/sec
Errors/sec
P95 latency
CPU
Memory
Pod count

29. What is Grafana?#

Prometheus stores and queries metrics.

Grafana is commonly used to visualize them.

Think:

Prometheus
    โ†“
Data
    โ†“
Grafana
    โ†“
Dashboard

Example dashboard:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Node API Production                         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Requests/s  โ”‚ Error Rate  โ”‚ P95 Latency     โ”‚
โ”‚ 2,340       โ”‚ 0.8%        โ”‚ 180ms           โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ CPU Usage                                   โ”‚
โ”‚ โ–โ–‚โ–ƒโ–ƒโ–„โ–…โ–†โ–‡โ–‡โ–ˆ                                  โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Memory Usage                                โ”‚
โ”‚ โ–‚โ–‚โ–ƒโ–ƒโ–„โ–…โ–…โ–†โ–†โ–‡                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

30. Monitoring Kubernetes Nodes#

You may want metrics like:

CPU
Memory
Disk
Network
Load
Filesystem

A common component is:

Node Exporter

It runs on nodes and exposes machine-level metrics.

Typically it is deployed as a DaemonSet.

Node 1 โ†’ Node Exporter
Node 2 โ†’ Node Exporter
Node 3 โ†’ Node Exporter

Prometheus scrapes these exporters.


31. Exporter Concept#

An exporter converts information into Prometheus-readable metrics.

For example:

Node
 โ†“
Node Exporter
 โ†“
/metrics
 โ†“
Prometheus

Other exporters exist for systems such as:

  • PostgreSQL
  • Redis
  • MySQL
  • NGINX
  • Kafka

32. Kubernetes Monitoring Architecture#

A realistic architecture:

                         Kubernetes
                             โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ†“                     โ†“                     โ†“
   Node Exporter        kube-state-metrics     App /metrics
       โ”‚                     โ”‚                     โ”‚
       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ†“
                        Prometheus
                             โ”‚
                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                   โ†“                   โ†“
               Grafana              Alertmanager
                   โ”‚                   โ”‚
                   โ†“                   โ†“
              Dashboards            Alerts

This is an extremely common architecture.


33. kube-state-metrics#

This one is worth understanding.

Node Exporter tells you about the machine.

kube-state-metrics tells you about the state of Kubernetes objects.

For example:

Deployment replicas
Pod status
DaemonSets
StatefulSets
Jobs
CronJobs

Example question:

Deployment wants 10 replicas. How many are actually available?

Kubernetes object-state metrics can help answer that.


34. Alerting#

Monitoring isn't useful if humans have to stare at Grafana all day.

You want alerts.

For example:

CPU > 90% for 10 minutes

or:

Error rate > 5%

or:

Pod restart count increasing

or:

P95 latency > 1 second

Architecture:

Prometheus
    โ”‚
    โ†“
Alert Rule
    โ”‚
    โ†“
Alertmanager
    โ”‚
    โ”œโ”€โ”€ Slack
    โ”œโ”€โ”€ Email
    โ”œโ”€โ”€ PagerDuty
    โ””โ”€โ”€ Other integrations

35. Prometheus Alert Rule#

Conceptually:

groups:
  - name: node-api
    rules:
      - alert: HighErrorRate
        expr: rate(http_requests_total{status=~"5.."}[5m]) > 5
        for: 10m
 
        labels:
          severity: critical
 
        annotations:
          summary: "High API error rate"

This says roughly:

If the matching error metric remains above the threshold for 10 minutes, fire an alert.

The exact expression must match your actual metric schema.


36. Alerting Strategy#

Don't create alerts for everything.

Bad:

CPU = 70%
โ†’ ALERT!

Maybe that's normal.

Better:

CPU > 90%
AND
sustained for 10 minutes

Or more meaningful:

5xx error rate > 5%
for 5 minutes

Good alerts should generally represent something that requires action.


37. Four Golden Signals#

A very useful monitoring concept is the Four Golden Signals:

Latency
Traffic
Errors
Saturation

Latency#

How long requests take.

P95 = 300ms

Traffic#

How much demand exists.

5,000 requests/sec

Errors#

How many requests fail.

2% HTTP 5xx

Saturation#

How close the system is to capacity.

CPU 95%
Memory 90%

These are excellent starting points for application monitoring.


38. RED Method#

For request-driven services, another useful model is:

R โ†’ Rate
E โ†’ Errors
D โ†’ Duration

For your Node.js API:

Rate       โ†’ requests/sec
Errors     โ†’ 5xx/sec
Duration   โ†’ P95 latency

Example:

Rate:       3,200 req/s
Errors:     1.2%
P95:        250ms

39. USE Method#

For infrastructure, another useful framework is:

U โ†’ Utilization
S โ†’ Saturation
E โ†’ Errors

For a Kubernetes node:

CPU utilization
Memory saturation
Network errors
Disk errors

These frameworks help you decide what to monitor, rather than randomly collecting thousands of metrics.


40. Monitoring Your Node.js API#

For your typical architecture:

Next.js
   โ†“
Ingress
   โ†“
Node.js API
   โ”œโ”€โ”€ PostgreSQL
   โ”œโ”€โ”€ Redis
   โ””โ”€โ”€ External APIs

I'd want at least:

API#

Request rate
Error rate
P50 latency
P95 latency
P99 latency
Active requests

Node.js#

CPU
Memory
Heap usage
Event loop behavior
GC behavior

PostgreSQL#

Connections
Query latency
Errors
Connection pool usage

Redis#

Memory
Commands/sec
Latency
Connections
Errors

Kubernetes#

Pod count
Restart count
CPU
Memory
Pending Pods
Node capacity
Deployment availability

41. Monitoring HPA#

Remember Chapter 15.

Traffic
   โ†“
CPU increases
   โ†“
HPA
   โ†“
More Pods

Monitoring helps you understand whether that actually happened.

Example:

10:00
Pods = 2
CPU = 40%
 
10:05
Traffic increases
 
10:06
CPU = 78%
 
10:07
HPA increases Pods
 
Pods = 4
 
10:10
CPU = 45%

Grafana can show this entire story visually.


42. HPA + Metrics Server vs Prometheus#

This is a common interview topic.

For standard resource-based HPA:

HPA
 โ†“
Metrics API
 โ†“
Metrics Server
 โ†“
CPU / Memory

Prometheus is different:

Application
 โ†“
Prometheus
 โ†“
Custom metrics / monitoring

With the appropriate adapters/integrations, Prometheus-derived metrics can also be used for autoscaling.

So don't say:

HPA always uses Prometheus.

That's incorrect.


43. Monitoring vs Logging#

Let's make the distinction crystal clear.

Logging Monitoring
Events Measurements
Text/structured records Time-series numbers
Database failed Error rate = 5%
User login failed Login failure rate
Redis timeout Redis latency
kubectl logs Prometheus/Grafana

Example:

LOG:
 
Database query failed
timeout after 3000ms

Metric:

database_query_errors_total = 152

Dashboard:

DB error rate
       โ”‚
  10% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
   5% โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ โ”‚
   0% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฐโ”€โ•ฏ

44. A Real Incident#

Imagine your production API becomes slow.

Step 1 โ€” Grafana#

You see:

P95 latency
200ms โ†’ 3 seconds

Step 2 โ€” Traffic#

1,000 req/s โ†’ 5,000 req/s

Step 3 โ€” CPU#

CPU
40% โ†’ 95%

Step 4 โ€” HPA#

2 Pods โ†’ 10 Pods

Step 5 โ€” Logs#

You find:

Database connection pool exhausted

Step 6 โ€” Database metrics#

Connections
100 โ†’ 500

Now you've connected:

Metrics
   โ†“
Logs
   โ†“
Root cause investigation

This is what observability is about.


45. Monitoring Architecture for Your Final Kubernetes Project#

Eventually, your course project will look something like:

                         Internet
                            โ”‚
                            โ†“
                         Ingress
                            โ”‚
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
             โ†“                             โ†“
       Next.js Service              Node.js API Service
             โ”‚                             โ”‚
          Pods ร— N                      Pods ร— N
                                           โ”‚
                              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                              โ†“            โ†“            โ†“
                           Redis       PostgreSQL    External APIs
 
                    OBSERVABILITY
                         โ”‚
            โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
            โ†“            โ†“            โ†“
          Logs        Metrics       Traces
            โ”‚            โ”‚
        Fluent Bit    Prometheus
            โ”‚            โ”‚
          Loki        Grafana
            โ”‚            โ”‚
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Dashboard

46. Practical Monitoring Commands#

Nodes#

kubectl top nodes

Pods#

kubectl top pods

Namespace#

kubectl top pods -n production

All namespaces#

kubectl top pods -A

Pod status#

kubectl get pods

Deployment status#

kubectl get deployments

HPA#

kubectl get hpa

Detailed HPA#

kubectl describe hpa node-api-hpa

47. Important Commands for Monitoring Investigation#

When something looks wrong:

kubectl get nodes

Then:

kubectl top nodes

Then:

kubectl get pods -A

Then:

kubectl top pods -A

Then:

kubectl get hpa -A

Then inspect problematic Pods:

kubectl describe pod <pod>

And logs:

kubectl logs <pod>

This gives you a basic operational workflow.


48. Interview Questions ๐ŸŽฏ#

Q1. What is Metrics Server?#

A Kubernetes component that provides basic resource usage metrics, commonly used by kubectl top and resource-based HPA.


Q2. Metrics Server vs Prometheus?#

Metrics Server
โ†’ basic CPU/memory resource metrics
 
Prometheus
โ†’ general-purpose time-series monitoring

Q3. What is Prometheus?#

An open-source monitoring and alerting system that collects and stores time-series metrics.


Q4. Does Prometheus normally push or pull metrics?#

Prometheus primarily uses a pull/scrape model.


Q5. What endpoint is commonly exposed by applications?#

/metrics

Q6. What is Grafana?#

A visualization and dashboarding platform commonly used to query and display metrics from Prometheus and other data sources.


Q7. What is a Counter?#

A metric that generally increases over time.

Example:

http_requests_total

Q8. What is a Gauge?#

A metric that can increase or decrease.

Example:

active_connections

Q9. What is a Histogram?#

A metric type useful for observing distributions such as request latency.


Q10. What is P95?#

The value below which approximately 95% of observations fall.


Q11. Why is P95 useful?#

It helps identify tail latency and user experiences that an average can hide.


Q12. What is high cardinality?#

A metric label creating a very large number of unique time series.

For example:

userId
requestId
sessionId

can create problematic cardinality when used indiscriminately as labels.


Q13. What is Node Exporter?#

A Prometheus exporter that exposes host/node-level system metrics.


Q14. What is kube-state-metrics?#

A component that exposes metrics representing the state of Kubernetes objects.


Q15. What are the Four Golden Signals?#

Latency
Traffic
Errors
Saturation

49. Chapter 22 Cheat Sheet ๐Ÿง #

Monitoring
    โ”‚
    โ”œโ”€โ”€ Metrics Server
    โ”‚      โ””โ”€โ”€ CPU / Memory
    โ”‚
    โ”œโ”€โ”€ Prometheus
    โ”‚      โ””โ”€โ”€ Time-series metrics
    โ”‚
    โ”œโ”€โ”€ Grafana
    โ”‚      โ””โ”€โ”€ Dashboards
    โ”‚
    โ”œโ”€โ”€ Alertmanager
    โ”‚      โ””โ”€โ”€ Notifications
    โ”‚
    โ”œโ”€โ”€ Node Exporter
    โ”‚      โ””โ”€โ”€ Node metrics
    โ”‚
    โ””โ”€โ”€ kube-state-metrics
           โ””โ”€โ”€ Kubernetes object state

Remember:#

kubectl top
    โ†“
Basic resource usage
 
Prometheus
    โ†“
Metrics collection/storage/query
 
Grafana
    โ†“
Visualization
 
Alertmanager
    โ†“
Notifications

๐Ÿ”ฅ Most Important Mental Model#

Your production Kubernetes system should eventually look like:

                         APPLICATION
                              โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ†“               โ†“               โ†“
             Logs          Metrics          Traces
              โ”‚               โ”‚               โ”‚
              โ†“               โ†“               โ†“
          Fluent Bit      Prometheus       OpenTelemetry
              โ”‚               โ”‚               โ”‚
              โ†“               โ†“               โ†“
             Loki          Grafana           Tempo/Jaeger
              โ”‚               โ”‚               โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ†“
                       OBSERVABILITY

And when an incident happens:

Grafana
   โ†“
"What changed?"
   โ†“
Prometheus metrics
   โ†“
"Where is the problem?"
   โ†“
Logs
   โ†“
"What exactly happened?"
   โ†“
Traces
   โ†“
"Which component is slow?"

That is the foundation of production-grade Kubernetes observability.


Next โ†’ Chapter 23: Troubleshooting ๐Ÿ”ง#

This is one of the most important Kubernetes chapters for interviews and real production work.

We'll learn how to systematically debug:

  • CrashLoopBackOff
  • ImagePullBackOff
  • Pending
  • OOMKilled
  • ContainerCreating
  • CreateContainerConfigError
  • ErrImagePull
  • Readiness probe failed
  • Service not reachable
  • DNS failures
  • Ingress failures
  • Pods unable to communicate
  • PVC stuck in Pending
  • Deployment not rolling out

And we'll build a Kubernetes troubleshooting decision tree you can use in real projects.

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 21 โ€” Kubernetes Logging ๐Ÿ“‹Next ChapterChapter 23 โ€” Kubernetes Troubleshooting ๐Ÿ”ง
PrevNext