liveBook · Manning

Kubectl logs

As explained in Acing the Certified Kubernetes Administrator Exam, Second Edition

kubectl logs is a Kubernetes command for retrieving logs from containers in pods.

Use `kubectl logs POD` to read a container’s stdout and stderr when a pod starts, crashes, or behaves unexpectedly. First find the pod with `kubectl get po`; then use its log output to identify an error you can act on. section 8.1 section 8.1.1

Find a crash-loop cause

A MySQL pod shows `CrashLoopBackOff`. Running `kubectl logs mysql-68f7776797-w92l6` reveals that the database is uninitialized and no password option is set. Add an indicated password environment variable to the Deployment, then check `kubectl get po` for a running replacement pod. section 8.1

Choose the scope of the logs you need:

  • For one container in a multi-container pod, specify its name: `kubectl logs busybox -c sidecar`. Use `-c busybox` to inspect the other container. section 8.1.1
  • To retrieve logs from every container in that pod, run `kubectl logs busybox --all-containers`. section 8.1.1
  • For a pod outside the current namespace, specify the namespace: `kubectl logs kube-controller-manager-server1 -n kube-system`. section 1.8

Container stdout and stderr are sent to log files managed by kubelet under `/var/log/containers`; `kubectl logs` is the usual way to retrieve pod logs. Do not confuse them with node and service logs: use `journalctl` to investigate processes such as kubelet or containerd. When investigating pod errors, `kubectl describe po` also supplies container state and events. section 8.1 section 8.1.1 section 1.8

Investigate a control-plane failure

If new Deployment replicas remain `Pending`, inspect the relevant control-plane pod in `kube-system`. In a scheduler failure, `kubectl -n kube-system logs kube-scheduler-kind-control-plane` exposes `Error: unknown flag: --kkubeconfig`; the scheduler’s static-pod manifest is the place to investigate the bad command. section 8.2

root@kind-control-plane:/# kubectl logs mysql-68f7776797-w92l6
2022-12-04 16:51:13+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL 
 Server 8.0.31-1.el8 started.
2022-12-04 16:51:13+00:00 [Note] [Entrypoint]: Switching to dedicated user 
 'mysql'
2022-12-04 16:51:13+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL 
 Server 8.0.31-1.el8 started.
2022-12-04 16:51:13+00:00 [ERROR] [Entrypoint]: Database is uninitialized 
 and password option is not specified
    You need to specify one of the following as an environment variable:
    - MYSQL_ROOT_PASSWORD
    - MYSQL_ALLOW_EMPTY_PASSWORD
    - MYSQL_RANDOM_ROOT_PASSWORD

The MySQL log output names the missing password configuration and the environment-variable options, making the next repair concrete. section 8.1

How Acing the Certified Kubernetes Administrator Exam, Second Edition covers it

How deep it goes
Works through pod, named-container, and all-container commands, then distinguishes container output from service logs and pod events.
Examples
Uses a MySQL CrashLoopBackOff, a two-container BusyBox pod, and a scheduler flag error as diagnostic cases.
What it assumes
Assumes basic `kubectl` pod inspection while demonstrating how to select containers and specify a namespace.
What you can do after
Diagnose/debug with it by selecting a pod or container, reading its error, making a targeted fix, and checking pod status.
How it connects
Connects pod status, Deployments, namespaces, multi-container pods, kubelet, journalctl, and static pods to troubleshooting decisions.

Also explained in

Compare how 3 books explain kubectl logs →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Certified Kubernetes Administrator Exam, Second Edition. The section links point to the parts of the book it draws on.