Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsEasy
A system administrator needs to retrieve the current logs for a specific Pod named 'my-app-pod-xyz' in the 'default' namespace to diagnose an issue. Which `kubectl` command should they use?
- A`kubectl get pod my-app-pod-xyz -o yaml`
- B`kubectl logs my-app-pod-xyz`
- C`kubectl describe pod my-app-pod-xyz`
- D`kubectl exec my-app-pod-xyz -- tail -f /var/log/app.log`
Show answer & explanationAnswer & explanation
Correct answer: B. `kubectl logs my-app-pod-xyz`
The `kubectl logs` command is specifically designed to fetch logs from a container in a Pod. It's the most direct and appropriate command for retrieving standard output and standard error streams from your application.
Why the other options are wrong
- A. `kubectl get -o yaml` retrieves the YAML definition of a resource, not its runtime logs.
- C. `kubectl describe` shows detailed status information about a resource, not its logs.
- D. `kubectl exec` runs commands inside a container; while possible, `kubectl logs` is the dedicated command for logs.
`kubectl logs` command
A `kubectl` command used to fetch logs from a container running in a Pod.
- Retrieves `stdout` and `stderr` streams of containers.
- Can specify a container name if a Pod has multiple containers (`-c <container-name>`).
- Supports `follow` mode (`-f`) for real-time log streaming and `tail` (`--tail=N`) for last N lines.
Memory trick: To 'log' in, use `kubectl logs` to 'see' what's happening.