Courseiva
CKADChapter 7 of 18Objective 4.1

Application Observability: Logs and Metrics

How do you know if your application is healthy or broken when you can't see the machine it's running on? In Kubernetes, your containers are scattered across a cluster, and you cannot just 'remote desktop' into them to check; you need a way to see what they are doing. This chapter covers logging and monitoring — the two key practices that give you visibility into your application's behaviour, which is essential for passing the CKAD exam question on application observability.

12 min read
Intermediate
Updated Jul 23, 2026
Editorial oversight: Johnson Ajibi· Security Systems Architect · MSc IT Security · IEEE Senior Member

A simple way to picture Application Observability: Logs and Metrics

The Chef's Kitchen Log Analogy

A restaurant kitchen is a high-pressure system. The head chef's primary tool for understanding how the kitchen is running is the kitchen log. This is not a simple to-do list; it is a detailed, timestamped record of every significant event. Each ticket that comes in is an event, logged with the time it was ordered, the time prep started, and the time the plate was sent out. A dropped spoon is an error log entry. A complaint about a cold steak is a critical alert.

The chef also has a set of monitors, like a thermometer for the walk-in cooler and a timer for the grill. These are the metrics. The thermometer tells the chef the current temperature of the cooler (a real-time metric), and it might track the average temperature over the last hour (a historical metric). The grill timer shows how long a steak has been on the fire (a request duration metric).

Now, imagine a customer sends back a plate because it's burnt. The chef looks at the logs: the ticket shows the steak was ordered medium-rare at 7:03 PM. The chef looks at the metrics: the grill timer shows that steak was on for 15 minutes, but a medium-rare steak should only take 8 minutes. The log and the metric together tell the story. The logs answered the 'what' (a burnt steak), and the metrics answered the 'why' (overcooking time). The chef can then use this evidence to adjust the workflow, perhaps resetting the grill timer or retraining the cook. In IT, we do the same thing. We look at application logs for specific error events and at metrics for system performance data to diagnose problems and observe the health of our applications.

How It Actually Works

Application observability is the ability to understand the internal state of a system by examining its outputs. For a Kubernetes application, you cannot look inside the container, so you must rely on the data it emits. This data falls into two main categories: logs and metrics.

Logs are a record of discrete events. Each entry in a log is a text line (or a structured object) that describes something that happened at a specific point in time. For example, a web server might write a log line every time it receives a request: '192.168.1.1 - - [10/Oct/2023:13:55:36] "GET /index.html" 200 2326'. This tells you who requested it, when, what they asked for, and the server's response code. Logs are great for investigating a specific failure because they give you the timeline of events leading up to it.

You view logs in Kubernetes using the kubectl logs command. If you have a pod named my-app-7d4f8, you run kubectl logs my-app-7d4f8 to see the output from the main container. If the pod has multiple containers, you specify the container name with a flag (e.g., kubectl logs my-app-7d4f8 -c sidecar). To keep an eye on live output, you use the -f flag (short for 'follow'), which streams new log lines to your terminal, similar to watching a command output in real-time.

Metrics are numerical measurements collected at intervals. They are not events; they are snapshots of a value over time. Common metrics include CPU usage percentage, memory utilisation, request count per second, and error rate. Metrics are aggregated over time (e.g., the average CPU usage over the last 5 minutes) and are displayed on dashboards. They are excellent for identifying trends and anomalies. For example, if memory usage is steadily climbing over 24 hours, that indicates a memory leak, which you would then investigate using logs.

In Kubernetes, basic node-level and pod-level metrics (CPU and memory) are provided by the Metrics Server. This is a cluster add-on that collects resource usage data and makes it available to the kubectl top command. You use kubectl top pod to see the current CPU and memory consumption of your pods. However, the CKAD exam focuses more on the application-level metrics that your application emits, not just the infrastructure metrics. To monitor custom metrics (like request duration), you typically use a monitoring platform like Prometheus, but the exam expects you to understand how to get the metrics that are built-in to Kubernetes.

The key distinction for a developer is this: logs answer 'what happened?', while metrics answer 'how is the system performing?'. For the CKAD, you are not required to set up a full monitoring stack, but you must be able to use kubectl logs to retrieve application output and kubectl top to view resource usage. Additionally, you need to understand the concept of sidecar containers for logging. A sidecar container is a separate container running in the same pod that processes the logs from the main application container. For example, you could have a sidecar that reads the application logs and forwards them to a central logging service like Elasticsearch. The exam might ask you to edit a pod manifest to add a sidecar container for logging purposes.

Finally, remember that containerised applications should write their logs to stdout (standard output) and stderr (standard error). They should not write to a file inside the container, because if the container crashes, that file is lost. Kubernetes automatically captures stdout and stderr and makes them available via kubectl logs. This pattern is called 'logging to stdout/stderr' and is a best practice for 12-factor applications.

Flowchart showing how logs and metrics flow from an application pod to the developer's terminal via `kubectl logs` and `kubectl top`.

Walk-Through

1

Identify the Target Pod

First, find the exact name of the pod you need to investigate. Use `kubectl get pods` to list all pods in the current namespace. Note the pod name exactly because `kubectl logs` is case-sensitive and requires the full name.

2

Check for Multi-Container Pods

If the pod has more than one container (check with `kubectl describe pod <name>` or by reading the pod manifest), you must specify which container you want logs from. Use `-c <container-name>`. This step prevents the error message that says 'a container name must be specified for pod ... with more than one container'.

3

Retrieve Logs for Error Detection

Run `kubectl logs <pod-name> -c <container-name>` to view all current log lines. If you receive an empty output but the pod is erroring, the current container may be freshly started after a crash. In that case, re-run the command with `--previous` to see the logs from the terminated container that holds the error.

4

View Real-Time Resource Usage

Run `kubectl top pod` to get the current CPU and memory consumption of all pods. This metric is vital to understand if a pod is resource-starved, which can cause slowdowns or restarts. Look at the 'MEMORY(bytes)' and 'CPU(cores)' columns to identify potential issues.

5

Correlate Logs and Metrics

Combine the information from both steps. For example, if the logs show 'OutOfMemoryError' and the metrics show memory utilisation at 95%, you have a clear diagnosis. This step is about using the two data sources together to form a complete picture of the application's health.

6

Take Remedial Action

Based on your findings, decide on an action. This could involve scaling the deployment (`kubectl scale deployment <name> --replicas=5`), editing the pod to increase resource limits, or rolling back to a previous version. The exam may ask you to implement a change based on the logs and metrics you observed.

What This Looks Like on the Job

Imagine you are a site reliability engineer for an e-commerce website running on Kubernetes. On a typical Tuesday afternoon, you get a notification from your monitoring dashboard that the checkout service's error rate has suddenly increased from 0.1% to 15%. This is a metric alert. You are now in firefighting mode.

Your first step is to look at the metrics to understand the scope. You open your dashboard (e.g., Grafana) and see a spike in HTTP 500 errors from the checkout-api pods. You also notice that CPU usage for those pods is at 95%, compared to a normal 30%. This tells you the problem is likely resource contention, not a bug in the code. However, you need more detail.

Next, you switch to logs. You run kubectl logs -l app=checkout-api --tail=50 to see the last 50 log lines from all pods with the label app=checkout-api. You see a lot of java.lang.OutOfMemoryError exceptions. Now you know the specific error: the Java heap is full. The logs give you the exact stack trace, which points to a memory leak in a new feature that was deployed this morning.

Now you use metrics to confirm. You look at the pod memory metric (kubectl top pod -l app=checkout-api). One pod is using 500MB of the configured 512MB limit. Another pod is using 504MB. The metrics confirm that the pods are hitting their memory limits.

With this evidence, you take action. You could scale the deployment horizontally (add more replicas) to spread the load, but that would only delay the inevitable. You need to fix the memory leak. You identify the offending pod with the highest memory usage and run kubectl logs checkout-api-6f4d8 –previous. This shows the logs from the previous terminated container (the one that crashed due to OutOfMemory), which might have additional clues.

The standard operating procedure in this scenario involves the following steps:

Confirm the metric alert is real by checking the dashboard for the last hour of data.

Use kubectl logs to find specific error messages associated with the spike.

Use kubectl logs with the --previous flag to see logs from a crashed container.

Use kubectl top pod to get real-time resource consumption data.

Correlate the log errors with the metric patterns to identify the root cause (here, a memory leak).

Roll back the deployment to the previous known-good version while the development team fixes the code.

This real-world process shows that logs and metrics are not separate tools; they are complementary. Metrics give you the 'what' (something is wrong), and logs give you the 'why' (this specific exception occurred). A professional always uses both to diagnose effectively.

How CKAD Actually Tests This

The CKAD exam tests your ability to use Kubernetes built-in tools for application observability, not to set up complex monitoring stacks. The core command you must master is kubectl logs. Expect multiple questions that require you to retrieve logs from a pod, follow live logs, or get logs from a specific container within a multi-container pod.

A common exam trap is the multi-container pod. You will be given a pod manifest with two containers. The question will ask you to find an error message from one of them. If you run kubectl logs <pod-name> without specifying the container, you will get an error because the command expects a container name. You must append -c <container-name> to target the correct one. Another trap involves the --previous flag. The exam might present a pod that is crashing and restarting. The current container's logs might be empty because it just started. You need to use kubectl logs <pod-name> –previous to see the logs from the last terminated container to find the crash reason.

You must also know kubectl top pod and kubectl top node. These commands display CPU and memory usage metrics. The exam may ask you to identify which pod is using the most memory or which node is overloaded. The output format is a table with columns for NAME, CPU(cores), MEMORY(bytes). You need to read this table and answer a specific question about resource usage.

The exam will not test Prometheus queries or Grafana dashboard creation. It sticks to the concepts explicitly listed in the curriculum: 'Use logging and monitoring tools to observe application behavior'.

Here are the specific exam topics you must be comfortable with:

Using kubectl logs to view standard output and standard error from a pod.

Using kubectl logs -f to stream logs in real-time.

Using kubectl logs --previous to view logs from the previous container instance.

Using kubectl logs -c <container> to get logs from a specific container in a multi-container pod.

Using kubectl top pod to view resource usage of pods.

Using kubectl top node to view resource usage of nodes.

Trap patterns to watch for:

A question asking for an error message, but the pod has two containers. You must remember the -c flag.

A pod that is in 'CrashLoopBackOff' state. The current container may have no logs, but the previous one does. You need --previous.

A question asking you to 'monitor' the logs. This means use -f to follow the log output.

A question about resource usage that requires you to look at the 'MEMORY(bytes)' column in the kubectl top output and identify the pod with the highest value.

Key definitions to memorise:

kubectl logs [pod-name] — prints the log output from the default container.

kubectl logs [pod-name] -c [container-name] — prints logs from a specific container.

kubectl logs [pod-name] --previous — prints logs from the previous terminated container.

kubectl logs [pod-name] -f — streams logs in real-time.

kubectl top pod — shows current CPU and memory usage for all pods in a namespace.

kubectl top node — shows current CPU and memory usage for all nodes in the cluster.

The correct answer pattern for log-based questions is almost always one of the four variations above. Do not overthink it. If you are asked to find an error, start with kubectl logs. If it fails because there are multiple containers, add -c. If the pod is crashing, add --previous. This stepwise approach will solve the majority of exam questions on this topic.

Key Takeaways

Logs answer 'what happened' and metrics answer 'how is the system performing' — they are complementary for debugging.

Use `kubectl logs <pod-name> -c <container-name>` to get logs from a specific container in a multi-container pod.

Use `kubectl logs <pod-name> --previous` to see logs from a previously crashed container when the current one has no useful output.

Always write container application logs to stdout and stderr, never to a file inside the container, or you will lose data on pod restart.

`kubectl top pod` provides real-time CPU and memory metrics for all pods in the current namespace.

The CKAD exam only tests basic built-in commands (`kubectl logs`, `kubectl top`) — you do not need to install Prometheus or external tools.

A sidecar container can collect logs from your main application container and forward them to an external logging service.

Use `kubectl logs -f` to stream live logs from a pod, which is essential for monitoring ongoing processes.

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

Logs

Event-driven text lines

Answers 'what happened?'

Useful for debugging specific errors

Metrics

Numerical snapshots over time

Answers 'how is the system performing?'

Useful for identifying trends and anomalies

kubectl logs

Shows container output text

Can stream live with -f

Requires container name for multi-container pods

kubectl top

Shows CPU and memory tables

Shows only current usage (real-time)

Does not require container name (pod-level)

Current container logs

From the currently running container instance

Used when pod is running normally

Accessed with kubectl logs <pod>

Previous container logs

From the last terminated container instance

Used when pod has crashed and restarted

Accessed with kubectl logs <pod> --previous

Single container pod

kubectl logs works directly

No -c flag needed

Pod runs one application process

Multi-container pod

kubectl logs requires -c flag

Must specify container name

Pod runs multiple co-operating processes (e.g., sidecar)

Watch Out for These

Mistake

I need to install a complex tool like Prometheus to pass the CKAD observability questions.

Correct

The CKAD only tests basic built-in commands: `kubectl logs` and `kubectl top`. Prometheus is not on the exam.

Beginners often confuse 'observability' with 'monitoring tools' and assume they need a full stack. The exam is simpler than that.

Mistake

If a pod is crashed, I should use `kubectl logs` to see the crash error from the current running instance.

Correct

If a pod is in CrashLoopBackOff, the current container may be newly started and have no logs. You must use `kubectl logs <pod-name> --previous` to see the logs from the crashed instance.

Beginners assume the latest pod instance has the error, when actually the error was in the previous one that terminated.

Mistake

`kubectl top pod` shows historical metrics like average CPU over the last hour.

Correct

`kubectl top pod` shows the current (real-time) CPU and memory usage, not historical data.

The word 'top' reminds people of the Linux `top` command, which shows real-time processes. But beginners might think it shows aggregated history.

Mistake

I should configure my application to write logs to a file inside the container for persistence.

Correct

Container logs should be written to stdout and stderr, so they are captured by the container runtime and accessible via `kubectl logs`. Writing to a file inside the container means the logs are lost if the container crashes.

Beginners are used to traditional server setups where logs are written to files. They don't understand that container filesystems are ephemeral.

Mistake

`kubectl logs -f` will show the entire log history before showing new lines.

Correct

`kubectl logs -f` streams new log lines as they are written, but it can also show recent logs depending on the implementation (usually the last few lines). It does not replay the entire history unless combined with other flags.

People confuse 'follow' with 'tail -f' on Linux, which does show the last 10 lines before following. The behaviour can vary, leading to assumptions.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

Why does `kubectl logs my-pod` return 'error: a container name must be specified for pod my-pod'?

This means your pod has more than one container. You need to specify which container's logs you want using the `-c` flag, for example: `kubectl logs my-pod -c my-container`.

My pod is in CrashLoopBackOff and `kubectl logs` shows nothing. Where is the error?

The current container instance just started fresh and has no logs yet. Use `kubectl logs my-pod --previous` to view the logs from the container that crashed.

What is the difference between `kubectl logs` and `kubectl top pod`?

`kubectl logs` shows text output (events) from your application container, while `kubectl top pod` shows numerical metrics (CPU and memory usage) for your pods.

How do I see logs from all containers in a pod at once without running multiple commands?

You cannot view all containers simultaneously with a single `kubectl logs` command. You must run separate commands for each container using the `-c` flag. There is no built-in shortcut.

Is it a problem if my application writes logs to a file like `/var/log/app.log` in the container?

Yes, that is a problem. If the container crashes or is deleted, the file and its logs are lost. Instead, write logs to stdout/stderr, which are captured by the container runtime and accessible via `kubectl logs`.

Does the CKAD exam test Prometheus or Grafana?

No, the CKAD exam does not test Prometheus, Grafana, or any external monitoring tool. It only tests the built-in `kubectl logs` and `kubectl top` commands.

Terms Worth Knowing

Keep going

You've finished Application Observability: Logs and Metrics. Continue through the CKAD study guide to build a complete picture of the exam.

Done with this chapter?