Container Logs and --previous
When a container restarts, kubectl logs shows you the new one - which has not failed yet, so the error is not in it. --previous reads the dead container's log, and it is where the actual cause lives. This shows that gap directly, then the selector, prefix and timestamp flags that make logs usable across a whole Deployment.
Observability and Maintenance Guide 41 of 44 Beginner
- Kubernetes1.36.4
- Cluster4 nodes
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 14 min
--previous reads the log of the last terminated container, which the kubelet keeps only until that container is garbage-collected. A container that exits instantly and restarts in a tight loop may already have lost it - the failure reads unable to retrieve container logs for containerd://..., which was produced twice while capturing this guide.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA1001 | 192.168.0.175 | Ubuntu 26.04 LTS | Control Plane Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE01 | 192.168.0.176 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE02 | 192.168.0.177 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE03 | 192.168.0.178 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a container restarted and the error is nowhere in the logs. This matters because kubectl logs shows you the container running now, which has not failed yet - the evidence is in the one that died.
- watching a container fail, and finding its error absent from the current logs
- using
--previousto read the container that actually died - combining
-l,--tail,--prefixand--sinceacross a whole Deployment - naming a container when the Pod has more than one
- finding out what you cannot get back at all once the Pod is deleted
Before you start
- A cluster and kubectl.
- The session creates namespace
ckad-logs, a Pod that runs for 25 seconds and then exits 3, a Deployment with three replicas, and a two-container Pod.
-
The error that is not in the logs
-
The flags worth knowing
-
A multi-container Pod needs a name
-
What you cannot get back