Today I Learned (TIL)

A summary of new things I learn each day. Handy log of useful and interesting discoveries to remember and share.

View My GitHub Profile

Real-time log streaming and analysis in Kubernetes

Introduction

Kubernetes, a powerful container orchestration platform, manages the lifecycle of containers. As applications become increasingly complex and distributed, effective logging is crucial for monitoring, troubleshooting, and security.

In this post I would like to focus on tools used for real-time log streaming. Real-time log analysis can help identify performance bottlenecks, errors, and anomalies. By examining logs in real-time, developers can quickly spot the root cause of problems.

Command Line Interface

kubectl logs

When it comes to interacting with Kubernetes cluster kubectl is the first tool that comes to mind. It not only allows us to manage the resources, but comes in handy for monitoring and debugging purposes as well. Let’s look at some examples:

Examples

When there is only a single container running on a Pod we can omit the container’s name and provide only the name of the Pod.

kubectl logs <pod>

In case there are multiple containers running on a single pod we can specify the container’s name with -c option.

kubectl logs -c <container> <pod>

To get logs from multiple contianers spread accross multiple pods we can specify deployment:

kubectl logs deployment/<deployment> -c <container>

Some more options and flags:

stern

stern is a powerful tool that allows to tail logs from multiple pods and multiple containers within the pods. It has some useful features like:

Examples

Get logs from pods selected by the pod-query in a given context. In addition, let’s exclude all log lines matched with the exclude regular expression:

stern --context=<context> <pod-query> --exclude=<exclude>

Choosing the right tool for the job

kubectl logs is the best tool for a simple log inspection. The main advantage of kubectl is its multifunctionality and availability. At the same time stern is a powerful and flexible tool for advanced log analysis and troubleshooting.