logs

Getting logs for Kubernetes resources

Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.

Command

  1. $ kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]

Example

Current State

  1. $ kubectl get deployments
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. nginx 1/1 1 1 13m

Command

  1. $ kubectl logs deployment/nginx

Output

  1. /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
  2. /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
  3. /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
  4. 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
  5. 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
  6. /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
  7. /docker-entrypoint.sh: Configuration complete; ready for start up

Print Logs for a Container in a Pod

Print the logs for a Pod running a single Container

  1. kubectl logs echo-c6bc8ccff-nnj52
  1. hello
  2. hello

Crash Looping Containers

If a container is crash looping and you want to print its logs after it exits, use the -p flag to look at the logs from containers that have exited. e.g. kubectl logs -p -c ruby web-1

Print Logs for all Pods for a Workload

Print the logs for all Pods for a Workload

  1. # Print logs from all containers matching label
  2. kubectl logs -l app=nginx

Workloads Logs

Print all logs from all containers for a Workload by passing the Workload label selector to the -l flag. e.g. if your Workload label selector is app=nginx usie -l "app=nginx" to print logs for all the Pods from that Workload.

Follow Logs for a Container

Stream logs from a container.

  1. # Follow logs from container
  2. kubectl logs nginx-78f5d695bd-czm8z -f

Printing Logs for a Container that has exited

Print the logs for the previously running container. This is useful for printing containers that have crashed or are crash looping.

  1. # Print logs from exited container
  2. kubectl logs nginx-78f5d695bd-czm8z -p

Selecting a Container in a Pod

Print the logs from a specific container within a Pod. This is necessary for Pods running multiple containers.

  1. # Print logs from the nginx container in the nginx-78f5d695bd-czm8z Pod
  2. kubectl logs nginx-78f5d695bd-czm8z -c nginx

Printing Logs After a Time

Print the logs that occurred after an absolute time.

  1. # Print logs since a date
  2. kubectl logs nginx-78f5d695bd-czm8z --since-time=2018-11-01T15:00:00Z

Printing Logs Since a Time

Print the logs that are newer than a duration.

Examples:

  • 0s: 0 seconds
  • 1m: 1 minute
  • 2h: 2 hours
  1. # Print logs for the past hour
  2. kubectl logs nginx-78f5d695bd-czm8z --since=1h

Include Timestamps

Include timestamps in the log lines

  1. # Print logs with timestamps
  2. kubectl logs -l app=echo --timestamps
  1. 2018-11-16T05:26:31.38898405Z hello
  2. 2018-11-16T05:27:13.363932497Z hello