Kubernetes capability
Read logs from a restarted Kubernetes container
Use kubectl logs --previous to view the prior container instance’s logs. A client with access to the Pod log subresource can make the same request using previous=true.
Documentation reviewed
Ways to do it
CLI
kubectl logs --previous
Specify the namespace, pod, and container to read the prior container instance’s available logs.
- Requirements
- A kubeconfig for the cluster and permission to read the pod and its logs.
- Limitations
- kubectl logs only returns the latest log file. Use a separate logging backend to retain logs beyond container log rotation.
Example
kubectl logs -n "$NAMESPACE" "$POD" -c "$CONTAINER" --previousAPI
GET /api/v1/namespaces/{namespace}/pods/{name}/log
Set previous=true and name the container. Use tailLines or limitBytes to bound the response.
- Requirements
- Cluster API access with get permission for the pods/log subresource in the namespace.
- Limitations
- This reads available container logs from the pod log subresource. The endpoint does not reconstruct old logs after they are gone.