Ravindra BagaleCourses & study guides मराठी

Chapter 14: Logging and Light Observability

DevOps

14.11 Troubleshooting table

Why: the problem

Suppose it is 2 a.m. and you are on call for a Swiggy-like app. Logs are missing in CloudWatch. Or docker logs is empty. Or the disk is full of logs. Or times look wrong by five and a half hours. These problems come again and again in every company. A troubleshooting table lists each symptom, its usual cause, the command to check and the fix. Senior engineers carry this table in their heads. In this lesson we build it together and practise each row with a real command, across Docker, Linux, Kubernetes and CloudWatch.

What we will build

You will walk through the most common logging problems one by one, run the check command for each, see the bad output and apply the fix. At the end you will have a table you can print and keep next to your desk.

What: every term in plain English

  • Symptom — What you see, like "no logs".
  • Cause — Why it happens.
  • Check — The command that proves the cause.
  • Fix — The change that solves it.
  • Clock skew — When server clocks are not in sync, log times disagree.
  • chrony / NTP — Tools that keep server time correct.
  • Buffering — The program holds lines in memory and writes later.
  • Back pressure — The log shipper cannot keep up, so lines wait or drop.
No logs in docker logs: check where the app writes Python lines late: check buffering Disk full: find big logs Wrong times: check clock Agent not shipping: check its log Pod logs gone after restart
Troubleshooting: symptom to check to cause to fix

How: step by step

  1. No logs in docker logs: check where the app writes

    docker exec api ls -la /var/log/app/
    

    What it does: If a file grows here, the app is not using stdout.

    Output

    -rw-r--r-- 1 app app 81M Oct 25 02:00 app.log
    
  2. Python lines late: check buffering

    docker exec api printenv PYTHONUNBUFFERED
    

    What it does: Empty means Python buffers output.

    Output

    (empty)
    
  3. Disk full: find big logs

    sudo du -sh /var/lib/docker/containers/*/*-json.log /var/log/* 2>/dev/null | sort -h | tail -n 3
    

    What it does: Find the biggest log files.

    Output

    4.1G /var/lib/docker/containers/9a1.../9a1...-json.log
    
  4. Wrong times: check clock

    timedatectl | grep -E "Time zone|synchronized"
    

    What it does: Zone and NTP sync.

    Output

    Time zone: Etc/UTC (UTC, +0000)
    System clock synchronized: no
    
  5. Agent not shipping: check its log

    sudo tail -n 3 /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log
    

    What it does: Errors from the agent.

    Output

    E! cloudwatchlogs: AccessDeniedException
    
  6. Pod logs gone after restart

    kubectl logs api-7c9f --previous
    

    What it does: Shows logs of the crashed container before restart.

    Output

    panic: config file not found
    

Example 1: The troubleshooting table

Stack. Runbook

Goal. The troubleshooting table.

logging-troubleshooting.md

Symptom                         | Usual cause                       | Check                                   | Fix
docker logs is empty            | App logs to a file in container   | docker exec C ls /var/log               | Log to stdout or symlink to /dev/stdout
Python lines arrive late        | Output buffering                  | printenv PYTHONUNBUFFERED               | ENV PYTHONUNBUFFERED=1
Disk full                       | No max-size, no logrotate         | du -sh .../*-json.log                   | daemon.json max-size, logrotate
Times off by 5h30m              | Mixed UTC and IST                 | timedatectl                             | Log in UTC with zone, show IST in UI
Times disagree across servers   | Clock not synced                  | timedatectl, chronyc tracking           | Enable chrony
Nothing in CloudWatch           | IAM, region or group name         | agent log, describe-log-groups          | Fix role, region, name
Stack trace split into lines    | No multiline setting              | Look at events in console               | multi_line_start_pattern
Pod logs lost after crash       | Container restarted               | kubectl logs --previous                 | Ship logs with Fluent Bit
Logs show twice                 | Two handlers or two shippers      | Count same line                         | Remove one handler
Bill jumped                     | DEBUG on, no retention            | Insights count by logger, retention     | INFO level, retention 30d

What each line does

  • One row per symptom — Read from left to right in an incident.
  • Check column — Always prove the cause before fixing.
  • Fix column — Small, safe change first.

Green output

$ wc -l logging-troubleshooting.md
11 logging-troubleshooting.md

Expected result. Print this. It covers most logging pages you will get.

Example 2: Fix time zone and clock sync on Ubuntu

Stack. Ubuntu, chrony, Amazon Time Sync

Goal. Fix time zone and clock sync on Ubuntu.

Commands

timedatectl
sudo apt-get install -y chrony
sudo systemctl enable --now chrony
chronyc tracking | grep -E "Reference ID|System time"
sudo timedatectl set-timezone UTC
date -u ; TZ=Asia/Kolkata date

What each line does

  • timedatectl — Shows zone and if the clock is synced.
  • chrony — Keeps time in sync with time servers. On AWS it uses 169.254.169.123.
  • chronyc tracking — Shows how far off the clock is.
  • set-timezone UTC — Servers in UTC. Convert to IST only when showing to people.
  • TZ=Asia/Kolkata date — See the same moment in IST.

Green output

Reference ID    : A9FEA97B (169.254.169.123)
System time     : 0.000012 seconds fast of NTP time
Sun Oct 25 06:40:00 UTC 2026
Sun Oct 25 12:10:00 IST 2026

Expected result. All servers agree on time to the microsecond, and logs line up across services.

Example 3: Kubernetes: logs of a crashed pod and all pods of an app

Stack. Kubernetes, kubectl

Goal. Kubernetes: logs of a crashed pod and all pods of an app.

Commands

kubectl get pods -l app=api
kubectl logs api-7c9f6d5b8-x2k4p --previous --tail 20
kubectl logs -l app=api --all-containers --since 10m --prefix | grep ERROR
kubectl describe pod api-7c9f6d5b8-x2k4p | grep -A 3 "Last State"

What each line does

  • --previous — Logs of the last container before the restart.
  • -l app=api — All pods with this label.
  • --prefix — Each line starts with the pod name.
  • describe ... Last State — Shows exit code and reason, like OOMKilled.

Green output

NAME                  READY  STATUS            RESTARTS
api-7c9f6d5b8-x2k4p   0/1    CrashLoopBackOff  5
panic: open /etc/api/config.yaml: no such file or directory
    Last State:  Terminated
      Reason:    Error
      Exit Code: 2

Expected result. The pod crashes because the config file is missing. Fix the ConfigMap mount.

Verify: did it work?

  1. Disk space after cleanup

    df -h / | tail -n 1
    

    What it does: Use percent should drop.

    Output

    /dev/xvda1  30G  11G  19G  37% /
    
  2. Clock synced

    timedatectl | grep synchronized
    

    What it does: Should be yes.

    Output

    System clock synchronized: yes
    

When it is red 1

Disk is 100 percent and you cannot even run commands

Cause. A container log grew to many GB.

Fix, step by step

  1. Truncate the biggest file safely: sudo truncate -s 0 /var/lib/docker/containers/ID/ID-json.log
  2. Add max-size and max-file in daemon.json.
  3. Restart Docker in a quiet window.

When it is red 2

Error from server (BadRequest): previous terminated container "api" in pod not found

Cause. The pod was deleted, not only restarted.

Fix, step by step

  1. Pod logs are gone with the pod.
  2. Search your central store, like CloudWatch or Loki.
  3. Add a shipper so this never blocks you again.

When it is red 3

Fluent Bit: [warn] [engine] failed to flush chunk, retry in 10 seconds

Cause. The output (CloudWatch) is slow or denies access: back pressure.

Fix, step by step

  1. Check IAM and the region.
  2. Check the network to logs.ap-south-1.amazonaws.com.
  3. Raise Mem_Buf_Limit or add storage.type filesystem.

Cleanup

  1. Remove the table file if not needed

    rm -f logging-troubleshooting.md
    

    What it does: Lab cleanup.

    Output

    (no output)
    

Ravindra Bagale's Tip

Check first, then fix. A guess-fix creates the next problem.

Practice tasks

Easy Print the table

Why: Keep it near you.

What to do: Save the table as a page in your notes.

How:
  1. Copy Example 1.

Check: You can find a row in 10 seconds.

Medium Add one row from your job

Why: Every team has its own trap.

What to do: Add one new symptom with check and fix.

How:
  1. Write all four columns.

Check: A teammate agrees it is real.

Hard Game day

Why: Practice makes calm.

What to do: Ask a friend to break logging in your lab while you fix it with the table.

How:
  1. Time yourself.

Check: Fixed in under 15 minutes.

Easy Say logging troubleshooting in two lines

Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.

What to do: Write two short lines about logging troubleshooting for a fresher who knows only Linux, Git and Docker basics.

How:
  1. Write the problem it solves.
  2. Write the one command or config line you use for it.
  3. Read it aloud to a friend.

Check: A fresher understands it without asking a question.

Easy Keep a throwaway log lab

Why: Practice needs a lab you can break. If it gets messy, you delete the containers and start again.

What to do: Run a small app in Docker that prints logs every second.

How:
  1. docker run -d --name ticker busybox sh -c "while true; do date; sleep 1; done"
  2. docker logs -f ticker
  3. Press Ctrl+C, then docker rm -f ticker.

Check: You see one new time line every second.

Medium Do it on a second stack

Why: Logging ideas must work for Python, Node.js and Java. Only the library changes.

What to do: Repeat this lesson with a different language than the one you used first.

How:
  1. If you used Python, use Node.js or Spring Boot now.
  2. Keep the same field names: time, level, msg, request_id.
  3. Compare the two outputs side by side.

Check: Both apps print the same fields in the same shape.

Medium Break it on purpose

Why: You must know the red error by face. In a real outage there is no time to read slowly.

What to do: Cause one error from the "When it is red" part of this lesson and fix it.

How:
  1. Follow the error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only the steps, without searching the web.

Medium Write a one-page runbook

Why: At 2 a.m. nobody remembers log commands. A short page saves the on-call person.

What to do: Write a one-page runbook for this lesson.

How:
  1. Title, when to use, exact commands, how to check, how to undo.

Check: A teammate can follow it alone.

Hard Explain it like an interview

Why: Interviewers want real stories, not definitions.

What to do: Record a 60 second answer about this lesson.

How:
  1. Say the problem, the command you ran, the log line you found and the fix.

Check: Your answer has a real command and a real log line.

Hard Draw the flow top to bottom

Why: A drawing shows where a log line is born and where it ends up.

What to do: Draw this lesson as boxes from top to bottom.

How:
  1. Start at the user click, go through the app, the log driver or agent, and end at the place you search.

Check: Each box matches a real command or file in this lesson.

Homework: try at home

Try at home

At home, cause three rows of the table on purpose in a lab (empty docker logs, late Python lines, wrong time zone) and fix each.

Recap

  • Always check before you fix.
  • docker logs empty means the app writes files.
  • Use UTC and chrony for correct times.
  • kubectl logs --previous saves crashed pod logs.
  • Limit log size to protect the disk.

FAQ

Can I delete a container log file?

Do not rm it while Docker runs. truncate -s 0 is safer.

Why UTC and not IST on servers?

One zone everywhere avoids mix-ups. Show IST in dashboards.

Are pod logs kept forever?

No. They go with the pod. Ship them out.

Fluent Bit drops logs, why?

Back pressure or memory limit. Add buffering.

Where do I start when logs are missing?

At the source: is the app writing? Then the driver, then the shipper, then the store.

Interview questions

  1. Logs are missing in CloudWatch. How do you debug?

    Check the app writes, then the agent or driver logs, IAM role permissions, region and group names, then network.

  2. docker logs is empty, why?

    The app writes to a file or the logging driver does not support reading.

  3. How do you get logs of a crashed container in Kubernetes?

    kubectl logs --previous, describe pod for exit reason, and central logs if the pod is gone.

  4. How do you handle disk full due to logs?

    Truncate the largest file, set Docker max-size and logrotate, add alerts on disk usage.

  5. Why do log timestamps disagree across servers?

    Clock skew or mixed time zones; fix with chrony and UTC everywhere.

Quick quiz

Q1. How do you see logs of a crashed pod?

kubectl logs POD --previous.

Q2. Why are Python lines late in Docker?

Output buffering; set PYTHONUNBUFFERED=1.

Q3. Which tool syncs time?

chrony (NTP).

Q4. What zone should servers use?

UTC.

Q5. Disk full of logs, first safe step?

truncate the biggest log file, then set limits.

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards