> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lab Solution Logging Optional

> How to inspect Kubernetes Pod logs and troubleshoot single and multi container log issues to identify user login and order failures

In this lesson we’ll learn how to inspect application logs in Kubernetes, interpret log output, and troubleshoot two common scenarios:

* Identify a user issue from logs for a single-container Pod.
* Handle Pods with multiple containers when viewing logs.

## 1) Inspect a single-container Pod and identify the issue

Steps to inspect logs for a Pod that contains a single container:

1. Confirm the Pod is running:

```bash theme={null}
kubectl get pods
```

2. View the application logs for the Pod:

```bash theme={null}
kubectl logs <pod-name>
```

Example application log output (single-container Pod):

```text theme={null}
[2022-04-16 19:55:20,874] INFO in event-simulator: USER4 is viewing page1
[2022-04-16 19:55:21,876] INFO in event-simulator: USER4 logged in
[2022-04-16 19:55:22,877] INFO in event-simulator: USER4 is viewing page3
[2022-04-16 19:55:23,878] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:55:23,878] INFO in event-simulator: USER4 logged in
[2022-04-16 19:55:24,879] INFO in event-simulator: USER1 is viewing page2
[2022-04-16 19:55:25,880] INFO in event-simulator: USER2 is viewing page3
[2022-04-16 19:55:26,881] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2022-04-16 19:55:26,881] INFO in event-simulator: USER3 is viewing page3
[2022-04-16 19:55:27,883] INFO in event-simulator: USER1 is viewing page1
[2022-04-16 19:55:28,884] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:55:28,884] INFO in event-simulator: USER2 logged out
[2022-04-16 19:55:29,885] INFO in event-simulator: USER2 logged in
[2022-04-16 19:55:30,887] INFO in event-simulator: USER2 is viewing page3
[2022-04-16 19:55:31,888] INFO in event-simulator: USER2 is viewing page1
[2022-04-16 19:55:32,889] INFO in event-simulator: USER4 logged in
[2022-04-16 19:55:33,891] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:55:33,891] INFO in event-simulator: USER1 logged in
[2022-04-16 19:55:34,892] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2022-04-16 19:55:34,893] INFO in event-simulator: USER2 is viewing page2
[2022-04-16 19:55:35,894] INFO in event-simulator: USER4 is viewing page1
[2022-04-16 19:55:36,895] INFO in event-simulator: USER4 logged in
[2022-04-16 19:55:37,896] INFO in event-simulator: USER2 is viewing page2
[2022-04-16 19:55:38,897] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:55:38,897] INFO in event-simulator: USER2 logged out
[2022-04-16 19:55:39,899] INFO in event-simulator: USER4 logged in
[2022-04-16 19:55:40,900] INFO in event-simulator: USER2 is viewing page3
[2022-04-16 19:55:41,901] INFO in event-simulator: USER2 is viewing page3
[2022-04-16 19:55:42,903] WARNING in event-simulator: USER7 Order failed as the item is OUT OF STOCK.
[2022-04-16 19:55:42,903] INFO in event-simulator: USER1 is viewing page3
[2022-04-16 19:55:43,904] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:55:43,904] INFO in event-simulator: USER2 is viewing page2
[2022-04-16 19:55:44,905] INFO in event-simulator: USER1 is viewing page1
[2022-04-16 19:55:45,907] INFO in event-simulator: USER1 is viewing page2
[2022-04-16 19:55:46,908] INFO in event-simulator: USER1 is viewing page1
```

Analysis

* Look for repeated WARNING entries. Here, multiple WARNINGs indicate USER5 repeatedly failed to log in.
* The log message clearly states the reason: the account is locked due to many failed login attempts.

Conclusion

* USER5 cannot access the application because their account is locked after too many failed login attempts.

## 2) Inspect logs for a multi-container Pod and identify the issue

When a Pod runs more than one container, you must specify which container's logs you want to read.

If you run the logs command without a container name:

```bash theme={null}
kubectl logs webapp2
```

You may see an error like this:

```text theme={null}
error: a container name must be specified for pod webapp2, choose one of: [db webapp]
```

This indicates the Pod hosts multiple containers (for example, `webapp` and `db`). To read logs from the web application container, include `-c` with the container name:

```bash theme={null}
kubectl logs webapp2 -c webapp
```

Example logs from the `webapp` container:

```text theme={null}
[2022-04-16 19:57:24,773] INFO in event-simulator: USER2 logged in
[2022-04-16 19:57:25,774] INFO in event-simulator: USER1 logged in
[2022-04-16 19:57:26,775] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:57:26,776] INFO in event-simulator: USER4 is viewing page1
[2022-04-16 19:57:27,777] INFO in event-simulator: USER1 is viewing page1
[2022-04-16 19:57:28,779] WARNING in event-simulator: USER30 Order failed as the item is OUT OF STOCK.
[2022-04-16 19:57:28,779] INFO in event-simulator: USER4 is viewing page3
[2022-04-16 19:57:29,780] INFO in event-simulator: USER1 logged in
[2022-04-16 19:57:30,781] INFO in event-simulator: USER3 logged in
[2022-04-16 19:57:31,783] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:57:31,783] INFO in event-simulator: USER2 logged in
[2022-04-16 19:57:32,784] INFO in event-simulator: USER3 logged out
[2022-04-16 19:57:33,785] INFO in event-simulator: USER4 logged in
[2022-04-16 19:57:34,786] INFO in event-simulator: USER3 is viewing page1
[2022-04-16 19:57:35,787] INFO in event-simulator: USER3 logged out
[2022-04-16 19:57:36,788] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:57:36,789] WARNING in event-simulator: USER30 Order failed as the item is OUT OF STOCK.
[2022-04-16 19:57:36,789] INFO in event-simulator: USER3 logged in
[2022-04-16 19:57:37,790] INFO in event-simulator: USER1 logged in
[2022-04-16 19:57:38,791] INFO in event-simulator: USER1 is viewing page1
[2022-04-16 19:57:39,792] INFO in event-simulator: USER4 is viewing page2
[2022-04-16 19:57:40,794] INFO in event-simulator: USER1 logged in
[2022-04-16 19:57:41,794] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:57:41,794] INFO in event-simulator: USER4 is viewing page1
[2022-04-16 19:57:42,796] INFO in event-simulator: USER1 logged out
[2022-04-16 19:57:43,797] INFO in event-simulator: USER1 is viewing page3
[2022-04-16 19:57:44,798] WARNING in event-simulator: USER30 Order failed as the item is OUT OF STOCK.
[2022-04-16 19:57:45,799] INFO in event-simulator: USER1 logged in
[2022-04-16 19:57:46,801] WARNING in event-simulator: USER5 Failed to Login as the account is locked due to MANY FAILED ATTEMPTS.
[2022-04-16 19:57:46,802] INFO in event-simulator: USER4 logged out
[2022-04-16 19:57:47,803] INFO in event-simulator: USER3 logged out
[2022-04-16 19:57:48,804] INFO in event-simulator: USER1 is viewing page2
```

Analysis

* This webapp container log includes WARNING entries for two different user-related issues:
  * USER5: repeated failed logins because the account is locked.
  * USER30: order failure due to an item being out of stock.
* The purchasing problem reported by users is represented by the USER30 warnings: the item is OUT OF STOCK.

Best practice: if you’re unsure which container to inspect, list containers for the Pod:

```bash theme={null}
kubectl get pod webapp2 -o jsonpath='{.spec.containers[*].name}'
# or
kubectl describe pod webapp2
```

<Callout icon="lightbulb" color="#1CB2FE">
  When a Pod has multiple containers, always use `kubectl logs <pod> -c <container>` to select which container's logs you want to read.
</Callout>

## Quick reference

| Scenario | Command | Result |
| - | -: | - |
| List Pods | `kubectl get pods` | See Pod names and status |
| View logs (single container Pod) | `kubectl logs <pod-name>` | Stream logs from the only container |
| View logs (multi-container Pod) | `kubectl logs <pod-name> -c <container>` | Stream logs from a specific container |
| List containers in a Pod | `kubectl get pod <pod-name> -o jsonpath='{.spec.containers[*].name}'` | Show container names (space-separated) |

## Summary

* Single-container Pod: Inspect logs with `kubectl logs <pod-name>`. From the sample logs, USER5 cannot access the app because their account is locked after too many failed login attempts.
* Multi-container Pod: Specify the container with `-c` (for example, `kubectl logs webapp2 -c webapp`). From the webapp container logs, the purchase failures are caused by items being OUT OF STOCK (USER30).

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/67ee36bc-fea0-4136-a5a9-40754b32c5f7/lesson/79b156b3-a3a8-49ab-8fb6-aaf7f593c59f" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.