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 22 of 33
21.
Phase 04Read time ~16 minDifficulty intermediate

Kubernetes Logging ๐Ÿ“‹

Warrior, now we enter Observability.

In this chapter
  • 01What is Logging?
  • 02Kubernetes Logging Mental Model
  • 03stdout and stderr
  • 04Basic kubectl logs
  • 05Follow Logs in Real Time
  • 06View Logs of a Deployment
  • 07Logs from a Specific Container
  • 08Multi-Container Pod
  • 09The Most Important Debugging Command: --previous
  • 10Example: Node.js Crash
  • 11Logs vs Describe
  • 12kubectl logs
  • 13kubectl describe
  • 14kubectl logs Useful Options
  • 15Last 100 lines
  • 16Last 20 lines
  • 17Logs from the last hour
  • 18Logs from the last 10 minutes
  • 19Include timestamps
  • 20Follow
  • 21Previous container
  • 22Specific container
  • 23Namespace Matters
  • 24Kubernetes Doesn't Store Your Logs Forever
  • 25Centralized Logging
  • 26Fluent Bit
  • 27Why DaemonSet?
  • 28ELK Stack
  • 29E โ€” Elasticsearch
  • 30L โ€” Logstash
  • 31K โ€” Kibana
  • 32EFK
  • 33Loki + Grafana
  • 34Structured Logging
  • 35Node.js Structured Logging
  • 36Log Levels
  • 37Don't Log Sensitive Information
  • 38Correlation IDs
  • 39Logging Architecture for Your Node.js Application
  • 40What Should Your Application Log?
  • 41Startup
  • 42Request
  • 43Business events
  • 44Errors
  • 45Performance
  • 46What Should NOT Be Logged?
  • 47Next.js Logging
  • 48Kubernetes Logging vs Application Logging
  • 49Application logging
  • 50Kubernetes logging
  • 51Centralized logging
  • 52Important Troubleshooting Scenario
  • 53Step 1
  • 54Step 2
  • 55Step 3
  • 56Step 4
  • 57Another Troubleshooting Scenario
  • 58Log Rotation
  • 59Logging Doesn't Mean "Log Everything"
  • 60Logs + Metrics + Traces
  • 61Logs
  • 62Metrics
  • 63Traces
  • 64Practical Commands Cheat Sheet
  • 65Get Pods
  • 66Logs
  • 67Follow
  • 68Previous container
  • 69Specific container
  • 70Last 100 lines
  • 71Since 10 minutes
  • 72With timestamps
  • 73Namespace
  • 74Labels
  • 75Describe
  • 76Previous + container
  • 77Real Production Debugging Flow
  • 78Interview Questions ๐ŸŽฏ
  • 79Q1. How do you view Pod logs?
  • 80Q2. How do you follow logs?
  • 81Q3. How do you see logs from a crashed container?
  • 82Q4. How do you view logs from a specific container?
  • 83Q5. Where should applications write logs in Kubernetes?
  • 84Q6. Why centralized logging?
  • 85Q7. What is Fluent Bit?
  • 86Q8. What is ELK?
  • 87Q9. What is EFK?
  • 88Q10. Why structured logging?
  • 89Q11. What is a correlation ID?
  • 90Q12. Does kubectl logs give centralized logging?
  • 91Chapter 21 Cheat Sheet ๐Ÿง 
  • 92Commands
  • 93Tools
  • 94Core concepts
  • 95Next โ†’ Chapter 22: Monitoring ๐Ÿ“Š
In this chapter (95 sections)
01What is Logging?02Kubernetes Logging Mental Model03stdout and stderr04Basic kubectl logs05Follow Logs in Real Time06View Logs of a Deployment07Logs from a Specific Container08Multi-Container Pod09The Most Important Debugging Command: --previous10Example: Node.js Crash11Logs vs Describe12kubectl logs13kubectl describe14kubectl logs Useful Options15Last 100 lines16Last 20 lines17Logs from the last hour18Logs from the last 10 minutes19Include timestamps20Follow21Previous container22Specific container23Namespace Matters24Kubernetes Doesn't Store Your Logs Forever25Centralized Logging26Fluent Bit27Why DaemonSet?28ELK Stack29E โ€” Elasticsearch30L โ€” Logstash31K โ€” Kibana32EFK33Loki + Grafana34Structured Logging35Node.js Structured Logging36Log Levels37Don't Log Sensitive Information38Correlation IDs39Logging Architecture for Your Node.js Application40What Should Your Application Log?41Startup42Request43Business events44Errors45Performance46What Should NOT Be Logged?47Next.js Logging48Kubernetes Logging vs Application Logging49Application logging50Kubernetes logging51Centralized logging52Important Troubleshooting Scenario53Step 154Step 255Step 356Step 457Another Troubleshooting Scenario58Log Rotation59Logging Doesn't Mean "Log Everything"60Logs + Metrics + Traces61Logs62Metrics63Traces64Practical Commands Cheat Sheet65Get Pods66Logs67Follow68Previous container69Specific container70Last 100 lines71Since 10 minutes72With timestamps73Namespace74Labels75Describe76Previous + container77Real Production Debugging Flow78Interview Questions ๐ŸŽฏ79Q1. How do you view Pod logs?80Q2. How do you follow logs?81Q3. How do you see logs from a crashed container?82Q4. How do you view logs from a specific container?83Q5. Where should applications write logs in Kubernetes?84Q6. Why centralized logging?85Q7. What is Fluent Bit?86Q8. What is ELK?87Q9. What is EFK?88Q10. Why structured logging?89Q11. What is a correlation ID?90Q12. Does kubectl logs give centralized logging?91Chapter 21 Cheat Sheet ๐Ÿง 92Commands93Tools94Core concepts95Next โ†’ Chapter 22: Monitoring ๐Ÿ“Š

Until now, we learned how Kubernetes runs and manages applications. Now we need to answer:

โ€œMy Pod is running, but my application is not working. How do I find out what happened?โ€

The first tool is logging.


1. What is Logging?#

A log is a record of something that happened inside your application.

For example, your Node.js API may generate:

Server started on port 3000
User login request received
Database connection established
Payment request failed
Redis connection timeout

Logs help us understand:

  • What happened?
  • When did it happen?
  • Which request caused it?
  • Why did an error occur?
  • Which Pod/container produced it?

2. Kubernetes Logging Mental Model#

Think of it like this:

                Kubernetes Cluster
                       โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚              โ”‚              โ”‚
      Pod A          Pod B          Pod C
        โ”‚              โ”‚              โ”‚
   Node.js API     Node.js API    Next.js
        โ”‚              โ”‚              โ”‚
      stdout         stdout         stdout
        โ”‚              โ”‚              โ”‚
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ†“
              Container Runtime
                       โ†“
             Node-level log files
                       โ†“
             Logging Agent
                       โ†“
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ†“              โ†“              โ†“
      Loki           Elasticsearch   Cloud Logs

The important idea:

Your application should generally write logs to stdout/stderr, and the platform handles collecting them.


3. stdout and stderr#

This is extremely important.

Linux processes commonly have:

stdout โ†’ normal output
stderr โ†’ errors

For example:

console.log("Server started");

goes to stdout.

And:

console.error("Database connection failed");

goes to stderr.

In Kubernetes, these streams are captured by the container runtime and made available through Kubernetes logging mechanisms.


4. Basic kubectl logs#

Suppose you have:

node-api-7d9f8c6f5b-x7k2m

You can see its logs:

kubectl logs node-api-7d9f8c6f5b-x7k2m

Example:

Server started on port 3000
Database connected
Redis connected
GET /users
GET /users/123

5. Follow Logs in Real Time#

Similar to:

tail -f

you can use:

kubectl logs -f node-api-7d9f8c6f5b-x7k2m

-f means:

Follow the log stream.

Now when new requests arrive:

GET /users
POST /login
GET /products

you'll see them immediately.

This is extremely useful during debugging.


6. View Logs of a Deployment#

Suppose:

kubectl get pods

returns:

node-api-6d4f7c8d9b-abc12
node-api-6d4f7c8d9b-def34
node-api-6d4f7c8d9b-ghi56

You can select the Pods using labels.

For example:

kubectl logs -l app=node-api

This is useful when you have multiple replicas.

You can also use:

kubectl logs deployment/node-api

Depending on the situation, Kubernetes will retrieve logs from a Pod associated with the Deployment; for multiple replicas, label-based selection is often more useful for examining all instances.


7. Logs from a Specific Container#

A Pod can contain multiple containers.

Example:

Pod
โ”‚
โ”œโ”€โ”€ node-api
โ”‚
โ””โ”€โ”€ logging-sidecar

Then:

kubectl logs my-pod -c node-api

And:

kubectl logs my-pod -c logging-sidecar

The -c option specifies the container.


8. Multi-Container Pod#

Example:

apiVersion: v1
kind: Pod
metadata:
  name: app-pod
spec:
  containers:
    - name: api
      image: my-api:1.0
 
    - name: sidecar
      image: fluent-bit:latest

Now:

kubectl logs app-pod

may not be enough because Kubernetes needs to know which container you mean.

Use:

kubectl logs app-pod -c api

or:

kubectl logs app-pod -c sidecar

9. The Most Important Debugging Command: --previous#

Imagine your Node.js application crashes.

Kubernetes restarts it.

You run:

kubectl logs my-pod

But the logs you're looking for aren't there because you're seeing the current container instance.

Use:

kubectl logs my-pod --previous

This shows logs from the previous terminated container instance.

Very useful for:

CrashLoopBackOff
OOMKilled
Application crash
Unhandled exception
Startup failure

10. Example: Node.js Crash#

Suppose your application contains:

throw new Error("Database connection failed");

Container crashes.

You may see:

Error: Database connection failed
    at connectDatabase (/app/db.js:24:11)
    at startServer (/app/server.js:12:3)

Then Kubernetes restarts the container.

You can inspect:

kubectl logs my-pod --previous

11. Logs vs Describe#

This is a very important distinction.

kubectl logs#

Answers:

What did the application/container print?

kubectl logs my-pod

kubectl describe#

Answers:

What is Kubernetes saying about this Pod?

kubectl describe pod my-pod

For example:

Events:
  Warning  BackOff
  Warning  Unhealthy
  Normal   Pulled
  Normal   Started

So:

Application problem
        โ†“
kubectl logs
 
Kubernetes problem
        โ†“
kubectl describe

In practice, you often use both.


12. kubectl logs Useful Options#

Last 100 lines#

kubectl logs my-pod --tail=100

Last 20 lines#

kubectl logs my-pod --tail=20

Logs from the last hour#

kubectl logs my-pod --since=1h

Logs from the last 10 minutes#

kubectl logs my-pod --since=10m

Include timestamps#

kubectl logs my-pod --timestamps

Follow#

kubectl logs -f my-pod

Previous container#

kubectl logs my-pod --previous

Specific container#

kubectl logs my-pod -c api

You can combine options:

kubectl logs my-pod \
  -c api \
  --previous \
  --timestamps

13. Namespace Matters#

Suppose your application is in:

production

Then:

kubectl logs my-pod

might fail because you're currently looking in another namespace.

Use:

kubectl logs -n production my-pod

For example:

kubectl logs -n production node-api-7d9f8c6f5b-x7k2m

14. Kubernetes Doesn't Store Your Logs Forever#

This is a very important production concept.

Many beginners think:

Kubernetes is my logging system.

Not exactly.

Kubernetes provides mechanisms to access container logs, but production systems commonly use a centralized logging system.

Why?

Imagine:

Node 1
 โ”œโ”€โ”€ Pod A logs
 โ””โ”€โ”€ Pod B logs
 
Node 2
 โ”œโ”€โ”€ Pod C logs
 โ””โ”€โ”€ Pod D logs
 
Node 3
 โ”œโ”€โ”€ Pod E logs
 โ””โ”€โ”€ Pod F logs

If you have 100 nodes and 2,000 Pods, manually checking:

kubectl logs

isn't practical.

You need centralized logging.


15. Centralized Logging#

Typical architecture:

Application
     โ”‚
     โ†“
stdout/stderr
     โ”‚
     โ†“
Container Runtime
     โ”‚
     โ†“
Node
     โ”‚
     โ†“
Logging Agent
     โ”‚
     โ†“
Central Log System
     โ”‚
     โ†“
Dashboard / Search

Examples of central log systems:

  • Elasticsearch
  • OpenSearch
  • Loki
  • CloudWatch
  • Google Cloud Logging
  • Azure Monitor
  • Splunk

16. Fluent Bit#

One popular log collector is:

Fluent Bit

Think of Fluent Bit as:

A small agent that collects logs and forwards them somewhere else.

Architecture:

Pod
 โ”‚
 โ†“
stdout
 โ”‚
 โ†“
Node log
 โ”‚
 โ†“
Fluent Bit
 โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Elasticsearch
 โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ Loki
 โ”‚
 โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ CloudWatch
 โ”‚
 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ OpenSearch

Fluent Bit is commonly deployed as a DaemonSet.

Why DaemonSet?

Because we want one logging agent on each node.

Node 1 โ†’ Fluent Bit
Node 2 โ†’ Fluent Bit
Node 3 โ†’ Fluent Bit
Node 4 โ†’ Fluent Bit

17. Why DaemonSet?#

Remember our earlier chapters.

A DaemonSet ensures a Pod runs on each eligible node.

So:

DaemonSet
    โ”‚
    โ”œโ”€โ”€ Fluent Bit โ†’ Node 1
    โ”œโ”€โ”€ Fluent Bit โ†’ Node 2
    โ”œโ”€โ”€ Fluent Bit โ†’ Node 3
    โ””โ”€โ”€ Fluent Bit โ†’ Node 4

Each Fluent Bit instance collects logs from its node.


18. ELK Stack#

One famous logging architecture is:

E โ€” Elasticsearch#

Stores and searches logs.

L โ€” Logstash#

Processes/transforms logs.

K โ€” Kibana#

Provides UI for searching and visualizing logs.

Architecture:

Kubernetes Pods
      โ”‚
      โ†“
 Fluent Bit
      โ”‚
      โ†“
 Logstash
      โ”‚
      โ†“
Elasticsearch
      โ”‚
      โ†“
   Kibana

This is called:

ELK Stack


19. EFK#

Another architecture:

Kubernetes
    โ†“
Fluent Bit
    โ†“
Elasticsearch
    โ†“
Kibana

This is commonly referred to as:

EFK

The exact components can vary; Fluent Bit can often handle collection and some processing without Logstash.


20. Loki + Grafana#

Another popular modern approach:

Kubernetes
    โ†“
Fluent Bit / Promtail / Alloy
    โ†“
Loki
    โ†“
Grafana

Loki is designed specifically for log aggregation and works particularly well with Grafana-based observability stacks.

Conceptually:

Logs
 โ†“
Loki
 โ†“
Grafana

You can search logs and correlate them with metrics.


21. Structured Logging#

This is extremely important for production Node.js applications.

Avoid logs like:

User login failed

Prefer structured JSON:

{
  "level": "error",
  "message": "User login failed",
  "userId": "12345",
  "service": "auth-service",
  "environment": "production"
}

Why?

Because machines can parse JSON easily.

You can search:

service = auth-service
level = error
userId = 12345

22. Node.js Structured Logging#

A production Node.js application commonly uses a logging library such as:

  • Pino
  • Winston

For high-throughput Node.js services, Pino is a popular choice.

Example:

import pino from "pino";
 
const logger = pino();
 
logger.info({
  service: "node-api",
  environment: "production"
}, "Server started");
 
logger.error({
  userId: "123",
  errorCode: "DB_CONNECTION_FAILED"
}, "Database connection failed");

The output can be JSON:

{
  "level": 30,
  "time": 1780000000000,
  "service": "node-api",
  "msg": "Server started"
}

Your Kubernetes logging pipeline can then collect this JSON.


23. Log Levels#

Common log levels:

Level Meaning
trace Extremely detailed
debug Debugging information
info Normal application events
warn Something unusual
error Failure/error
fatal Critical failure

Example:

logger.debug("Cache lookup");
logger.info("User logged in");
logger.warn("Slow database query");
logger.error("Payment failed");

In production, you usually don't want extremely verbose debug logging everywhere.


24. Don't Log Sensitive Information#

This is extremely important.

Never casually log:

password
JWT token
API key
credit card number
private key
database password

Bad:

console.log({
  username,
  password
});

Also dangerous:

logger.info({
  authorization: req.headers.authorization
});

Because you might expose bearer tokens.

Instead:

logger.info({
  userId,
  action: "login"
}, "User login attempt");

25. Correlation IDs#

Now we reach an important production concept.

Imagine a user sends:

POST /checkout

The request goes through:

Frontend
   โ†“
API Gateway
   โ†“
Node.js API
   โ†“
Payment Service
   โ†“
Database

Each service produces logs.

How do we know which logs belong to the same request?

Use a:

Correlation ID / Request ID

Example:

requestId = abc-123

Then:

{
  "requestId": "abc-123",
  "service": "api",
  "message": "Checkout started"
}

Payment service:

{
  "requestId": "abc-123",
  "service": "payment",
  "message": "Payment initiated"
}

Database service:

{
  "requestId": "abc-123",
  "service": "database",
  "message": "Transaction committed"
}

Now you can search:

requestId = abc-123

and reconstruct the request flow.


26. Logging Architecture for Your Node.js Application#

Let's imagine your application:

Next.js Frontend
       โ”‚
       โ†“
   Ingress
       โ”‚
       โ†“
 Node.js API
       โ”‚
   โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”
   โ†“        โ†“
Redis    PostgreSQL

Running in Kubernetes:

                    Internet
                       โ”‚
                       โ†“
                    Ingress
                       โ”‚
                       โ†“
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ”‚   Node.js API   โ”‚
              โ”‚                 โ”‚
              โ”‚  Pod 1          โ”‚
              โ”‚  Pod 2          โ”‚
              โ”‚  Pod 3          โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ”‚
                 stdout/stderr
                       โ”‚
                       โ†“
                 Node log files
                       โ”‚
                       โ†“
                  Fluent Bit
                       โ”‚
              โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
              โ†“                 โ†“
             Loki          Elasticsearch
              โ”‚                 โ”‚
              โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                       โ†“
                    Grafana

27. What Should Your Application Log?#

For a Node.js API:

Startup#

Server started
Database connected
Redis connected

Request#

GET /users
POST /orders

Business events#

Order created
Payment initiated
Email sent

Errors#

Database query failed
Redis timeout
External API failed

Performance#

Database query took 850ms
External API took 1.2s

28. What Should NOT Be Logged?#

Avoid:

Passwords
JWT tokens
API keys
Credit card numbers
Session cookies
Private keys
Personal data unnecessarily

Use:

userId
requestId
orderId
traceId
errorCode
service
endpoint
duration

where appropriate.


29. Next.js Logging#

For Next.js, logging depends on where code executes.

For server-side code:

console.log("Server request received");

will appear in the server/container logs.

But logs from browser-side code:

console.log("Button clicked");

appear in the user's browser console, not your Kubernetes container logs.

This distinction is important:

Next.js Server
      โ†“
Kubernetes logs
 
Next.js Browser
      โ†“
Browser DevTools

30. Kubernetes Logging vs Application Logging#

Don't confuse them.

Application logging#

Your code produces:

logger.error(...)

Kubernetes logging#

Kubernetes/container runtime makes those container logs accessible:

kubectl logs

Centralized logging#

A logging system stores/searches them:

Loki
Elasticsearch
Cloud Logging
etc.

So:

Application
   โ†“
stdout/stderr
   โ†“
Kubernetes/container runtime
   โ†“
Log collector
   โ†“
Centralized storage
   โ†“
Dashboard

31. Important Troubleshooting Scenario#

Suppose:

kubectl get pods

shows:

node-api-abc123    0/1    CrashLoopBackOff

What should you do?

Step 1#

kubectl logs node-api-abc123

If the current container isn't useful:

Step 2#

kubectl logs node-api-abc123 --previous

Then:

Step 3#

kubectl describe pod node-api-abc123

Look at:

Events

Then:

Step 4#

Check status:

kubectl get pod node-api-abc123

You may discover:

Last State:
  Terminated
  Reason: OOMKilled

Now you know:

The application/container exceeded its memory limit.

This connects Chapter 13 with Chapter 21.


32. Another Troubleshooting Scenario#

Suppose:

Pod: Running
Ready: 0/1

Check:

kubectl logs my-pod

Maybe:

Database connection timeout

Then:

kubectl describe pod my-pod

Maybe readiness probe is failing.

So the application is running, but Kubernetes doesn't consider it ready for traffic.

Remember:

Running โ‰  Ready

33. Log Rotation#

Imagine your application produces:

10 GB logs/day

If you keep everything forever on a node, disk usage can become a problem.

Production logging systems therefore need:

  • log rotation
  • retention policies
  • storage limits
  • compression
  • centralized storage
  • deletion policies

For example:

Hot logs โ†’ 7 days
Archived logs โ†’ 30 days
Delete โ†’ after retention period

Exact retention depends on operational and regulatory requirements.


34. Logging Doesn't Mean "Log Everything"#

This is an important engineering principle.

More logs โ‰  better observability.

Suppose:

1 request
โ†“
100 log lines

With:

100,000 requests/sec

you could generate enormous log volume.

Instead, prefer useful structured events.

For example:

{
  "level": "info",
  "requestId": "abc123",
  "route": "/checkout",
  "statusCode": 200,
  "durationMs": 145
}

Much more useful than printing huge objects everywhere.


35. Logs + Metrics + Traces#

Now we are approaching the bigger concept:

Observability

Three major pillars:

             Observability
                  โ”‚
       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
       โ†“          โ†“          โ†“
     Logs      Metrics     Traces
       โ”‚          โ”‚          โ”‚
    Events      Numbers     Requests

Logs#

Tell you:

What happened?

Metrics#

Tell you:

How much / how often?

Traces#

Tell you:

Where did this request spend its time?

Example:

Request abc123
โ”‚
โ”œโ”€โ”€ API: 20ms
โ”‚
โ”œโ”€โ”€ Redis: 5ms
โ”‚
โ”œโ”€โ”€ PostgreSQL: 80ms
โ”‚
โ””โ”€โ”€ Payment API: 500ms

We'll study metrics in Chapter 22 and later connect this to tracing/observability.


36. Practical Commands Cheat Sheet#

Get Pods#

kubectl get pods

Logs#

kubectl logs <pod>

Follow#

kubectl logs -f <pod>

Previous container#

kubectl logs <pod> --previous

Specific container#

kubectl logs <pod> -c <container>

Last 100 lines#

kubectl logs <pod> --tail=100

Since 10 minutes#

kubectl logs <pod> --since=10m

With timestamps#

kubectl logs <pod> --timestamps

Namespace#

kubectl logs -n production <pod>

Labels#

kubectl logs -l app=node-api

Describe#

kubectl describe pod <pod>

Previous + container#

kubectl logs <pod> -c api --previous

37. Real Production Debugging Flow#

Memorize this:

Application issue
       โ”‚
       โ†“
kubectl get pods
       โ”‚
       โ†“
Is Pod Running?
       โ”‚
   โ”Œโ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”
   No      Yes
   โ”‚        โ”‚
   โ†“        โ†“
describe   logs
pod        โ”‚
   โ”‚       โ†“
   โ”‚    error?
   โ”‚       โ”‚
   โ”‚       โ†“
   โ”‚   --previous
   โ”‚
   โ†“
Events

A more practical workflow:

kubectl get pods -n production
 
kubectl logs -n production <pod>
 
kubectl logs -n production <pod> --previous
 
kubectl describe pod -n production <pod>

Then inspect:

Status
Events
Last State
Restart Count
Probe failures
Resource failures

38. Interview Questions ๐ŸŽฏ#

Q1. How do you view Pod logs?#

kubectl logs <pod>

Q2. How do you follow logs?#

kubectl logs -f <pod>

Q3. How do you see logs from a crashed container?#

kubectl logs <pod> --previous

Q4. How do you view logs from a specific container?#

kubectl logs <pod> -c <container>

Q5. Where should applications write logs in Kubernetes?#

Generally:

stdout / stderr

rather than relying on application-specific files inside ephemeral container filesystems.


Q6. Why centralized logging?#

Because large Kubernetes clusters can have:

100s of nodes
1000s of Pods
millions/billions of log events

Centralized logging allows:

  • searching
  • aggregation
  • retention
  • alerting/investigation
  • cross-service correlation

Q7. What is Fluent Bit?#

A lightweight log collector/forwarder commonly deployed as a DaemonSet.


Q8. What is ELK?#

Elasticsearch
Logstash
Kibana

Q9. What is EFK?#

Commonly:

Elasticsearch
Fluent Bit
Kibana

Q10. Why structured logging?#

Because JSON logs are easier for machines to:

parse
search
filter
aggregate
analyze

Q11. What is a correlation ID?#

A unique identifier used to connect logs/events belonging to the same request across services.


Q12. Does kubectl logs give centralized logging?#

No.

It gives access to container logs through Kubernetes.

Centralized logging requires an additional logging pipeline/system.


39. Chapter 21 Cheat Sheet ๐Ÿง #

stdout/stderr
     โ†“
Container Runtime
     โ†“
Node
     โ†“
Log Collector
     โ†“
Centralized Logging
     โ†“
Search / Dashboard

Commands#

kubectl logs POD
kubectl logs -f POD
kubectl logs POD --previous
kubectl logs POD -c CONTAINER
kubectl logs POD --tail=100
kubectl logs POD --since=10m
kubectl logs POD --timestamps
kubectl logs -l app=node-api

Tools#

Fluent Bit
Fluentd
Loki
Elasticsearch
OpenSearch
CloudWatch
Cloud Logging
Splunk
Grafana
Kibana

Core concepts#

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

๐Ÿง  The One Mental Model to Remember#

If your Node.js application is running inside Kubernetes:

Node.js
   โ”‚
   โ”‚ console.log / logger.info
   โ”‚ console.error / logger.error
   โ†“
stdout / stderr
   โ†“
Container Runtime
   โ†“
Kubernetes Node
   โ†“
Fluent Bit / Collector
   โ†“
Loki / Elasticsearch / Cloud Logging
   โ†“
Grafana / Kibana

And when something breaks:

kubectl logs
        โ†“
kubectl logs --previous
        โ†“
kubectl describe pod
        โ†“
Events + application error

That is the basic Kubernetes logging workflow.


Next โ†’ Chapter 22: Monitoring ๐Ÿ“Š#

We'll learn the second major pillar of observability:

  • Metrics
  • CPU & memory monitoring
  • kubectl top
  • Metrics Server
  • Prometheus
  • Grafana
  • Node.js application metrics
  • Kubernetes cluster metrics
  • Alerts
  • Prometheus architecture
  • ServiceMonitor
  • HPA + Prometheus
  • Production monitoring architecture

And we'll build the mental model:

Logs     โ†’ What happened?
Metrics  โ†’ How is the system performing?
Traces   โ†’ Where did the request spend time?

Finished this chapter?

Mark it as complete to track your mastery.

Previous ChapterChapter 20 โ€” Kubernetes Network Policies ๐ŸŒ๐Ÿ”Next ChapterChapter 22 โ€” Kubernetes Monitoring ๐Ÿ“Š
PrevNext