How to use the command 'stern' (with examples)
Stern is a command-line tool used to tail logs from multiple pods and containers within a Kubernetes cluster. It makes it easier to monitor and debug applications by providing real-time log outputs. This article will provide examples for each use case of the ‘stern’ command.
Use case 1: Tail all pods within a current namespace
Code:
stern .
Motivation: This use case allows users to tail logs from all pods within the current namespace. It is useful when monitoring the overall health and performance of the applications within the namespace.
Explanation: The command ‘stern .’ is executed with a period (’.’) as the argument. The period represents the current namespace, and stern will tail logs from all pods within that namespace.
Example Output:
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
Use case 2: Tail all pods with a specific status
Code:
stern . --container-state running
Motivation: This use case allows users to filter pods based on their container state, such as running, waiting, or terminated. It can be useful when troubleshooting issues related to pod readiness or lifecycle.
Explanation: The command ‘stern .’ is executed with the additional argument ‘–container-state running’. The ‘running’ argument filters the pods and only tails logs from pods with a running container state.
Example Output:
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
Use case 3: Tail all pods that matches a given regular expression
Code:
stern pod_query
Motivation: This use case allows users to tail logs from pods that match a given regular expression. It is helpful when dealing with a large number of pods and wanting to focus on specific ones based on a naming pattern or label convention.
Explanation: The command ‘stern pod_query’ is executed with ‘pod_query’ as the argument. The ‘pod_query’ argument can be a regular expression that matches the desired pods.
Example Output:
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
Use case 4: Tail matched pods from all namespaces
Code:
stern pod_query --all-namespaces
Motivation: This use case allows users to tail logs from pods that match a regular expression across all namespaces. It is useful when monitoring logs from a specific type of pod across multiple namespaces.
Explanation: The command ‘stern pod_query’ is executed with the additional argument ‘–all-namespaces’. This argument tells stern to search for matching pods in all namespaces.
Example Output:
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
Use case 5: Tail matched pods from 15 minutes ago
Code:
stern pod_query --since 15m
Motivation: This use case allows users to tail logs from pods that match a regular expression starting from a specific time, here 15 minutes ago. It is useful when investigating issues that might have occurred recently and require analysis of recent log entries.
Explanation: The command ‘stern pod_query’ is executed with the additional argument ‘–since 15m’. This argument specifies that stern should tail logs from pods that match the regular expression starting from 15 minutes ago.
Example Output:
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
Use case 6: Tail matched pods with a specific label
Code:
stern pod_query --selector release=canary
Motivation: This use case allows users to tail logs from pods that match a regular expression and have a specific label. It is useful when monitoring the logs of pods with a specific characteristic, such as a canary release.
Explanation: The command ‘stern pod_query’ is executed with the additional argument ‘–selector release=canary’. This argument filters the pods and only tails logs from pods that have the label ‘release=canary’.
Example Output:
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
[POD_NAME] [CONTAINER_NAME]: [LOG_MESSAGE]
Conclusion:
The ‘stern’ command is a powerful tool for tailing logs from multiple pods and containers within a Kubernetes cluster. With its various options and filter capabilities, it provides flexibility in monitoring and debugging applications. By following the examples provided in this article, users can effectively leverage the ‘stern’ command to streamline their log analysis workflows.