kubectl logs
- Docker & Kubernetes course
- Intermediate
- Lesson 72/82
- 4 min
Learn to view Pod logs with kubectl logs.
kubectl logs
The first step of troubleshooting is the logs. kubectl logs shows a container's stdout/stderr output; you can select the pod by name or through its owner (deploy/web):
kubectl logs web-7c5b9f6d8-abcde
kubectl logs deploy/web # one pod of the deployment
kubectl logs -f deploy/web --since=10m # live, last 10 minutes
kubectl logs api-xyz -c log-gonderici # multi-container pod
kubectl logs api-xyz --previous # the previous, crashed containerWhen are logs lost?
The kubelet keeps logs on the node and rotates them. When a pod is deleted or moved to another node, its logs go too. That is why in production logs are sent to a central system (in the advanced section: Fluentd, Elasticsearch, Loki). For short-lived problems, --previous still gives the last log of the previous container.
kubectl logs -l app=api --all-containers --prefix --tail=20
kubectl logs api-xyz --timestamps | grep -i errorPractice Scan the logs
Write three commands: show the last 20 lines of all pods labelled app=api with prefixes; show the previous container log of api-5d8f7c9b4-qrstu; follow the last 10 minutes of deploy/web live.
Starter code
# buraya yazSample solution
kubectl logs -l app=api --tail=20 --prefix
kubectl logs api-5d8f7c9b4-qrstu --previous
kubectl logs -f deploy/web --since=10mExpected output
[pod/api-5d8f7c9b4-qrstu/api] ERROR veritabanına bağlanılamadı: db:5432 [pod/api-5d8f7c9b4-vwxyz/api] INFO istek /saglik 200 panic: dial tcp 10.96.12.4:5432: connect: connection refused 10.244.1.1 - - "GET / HTTP/1.1" 200 615
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does kubectl logs deploy/web show when there are several replicas?
- It errors
- The log of only one of the pods Correct answer
- The logs of all pods
- The deployment events
Explanation: For all replicas, -l app=web --prefix is used.
2 Get the logs of all containers.
kubectl logs api-xyz ____
kubectl logs api-xyz --all-containers
Explanation: --all-containers covers every container in the pod.
3 Match.
| Term | Meaning |
|---|---|
-f |
Follow live |
-c |
Pick a container |
--previous |
Crashed one's log |
--prefix |
Pod name prefix |