Tips and Tricks: Tail Logs in K8S

Wednesday, Sep 29, 2021 15:00 · 243 words · 2 minutes read

Tips and Tricks

  1. K8S Context Commands
  2. Manually Allocate Elasticsearch Shards
  3. Switch K8S Namespace
  4. Tail Logs in K8S

Overview

In this blog post I will demonstrate how to tail logs from pods containers in Kubernetes.

Solution

kubectl logs is pretty powerfull command, which allows engineeers to tail logs based on different criterion. I will walk through some of the most commonly used scenarios. There are three really helpful arguments of the kubectl logs command:

--all-containers=false: Get all containers logs in the pod(s).
-c, --container=: Print the logs of this container
-f, --follow=false: Specify if the logs should be streamed.

Specific pod

  • return logs from mongodb-1 pod
kubectl logs mongodb-1
  • return logs from exporter container from pod redis-1
kubectl logs redis-1 -c exporter
  • return logs from pod redis-1 with multi containers
kubectl logs redis-1 --all-containers=true

Pods from given deployment

  • streaming the logs from all nginx-ingress-internal-controller deployment’s pods with multi containers, written in the last 3 minutes
kubectl logs deployment/nginx-ingress-internal-controller --all-containers=true --since=3m -f

Pods based on selector

  • return logs from pods defined by label name=backend, written in the last 5 minutes
kubectl logs --selector=name=backend --since=5m

More

Additional information and examples how to use the kubectl logs command can be found in its help:

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

Examples:
  # Return snapshot logs from pod nginx with only one container
  kubectl logs nginx

...
comments powered by Disqus

Buy Me A Coffee