Learn how to efficiently tail the last few lines of large kubectl logs using various commands and techniques for streamlined Kubernetes troubleshooting.
Kubernetes, a powerful container orchestration platform, simplifies application deployment and management. However, troubleshooting and monitoring applications in Kubernetes require efficient log management. This guide provides essential tips and commands for effectively viewing and managing logs in your Kubernetes cluster.
kubectl logs <pod-name>
to view logs from a specific pod.-f
or --follow
to kubectl logs
to stream logs in real time.--previous
or -p
with kubectl logs
to view logs from a previous instance of a pod.-c <container-name>
.-t
or --timestamps
to include timestamps in the log output.--tail=<number>
to limit the output to a specific number of lines from the end of the log.kubectl logs
. Implement a centralized logging solution like the PLG stack (Prometheus, Loki, Grafana) or similar tools.This document provides examples of using kubectl to view logs from Kubernetes pods. It covers basic log retrieval, real-time logs, viewing previous logs, filtering by container, displaying timestamps, limiting log lines, and conceptual examples of centralized logging with Fluent Bit and log rotation. It also briefly touches upon handling multi-line logs and ensuring application code writes logs to stdout and stderr, including a Python example.
This document provides code examples for various Kubernetes log viewing scenarios.
Prerequisites:
kubectl
configured to interact with your cluster1. Basic Log Viewing:
kubectl logs <pod-name>
Example:
kubectl logs my-nginx-pod
2. Real-time Logs:
kubectl logs -f <pod-name>
Example:
kubectl logs -f my-nginx-pod
3. Previous Logs:
kubectl logs --previous <pod-name>
Example:
kubectl logs --previous my-nginx-pod
4. Container-Specific Logs:
kubectl logs <pod-name> -c <container-name>
Example:
kubectl logs my-app-pod -c backend
5. Timestamped Logs:
kubectl logs -t <pod-name>
Example:
kubectl logs -t my-nginx-pod
6. Limited Log Lines:
kubectl logs --tail=100 <pod-name>
Example:
kubectl logs --tail=100 my-nginx-pod
7. Centralized Logging (Conceptual):
While not a direct kubectl
command, here's a basic example of deploying a logging agent (Fluent Bit) as a DaemonSet to collect logs and send them to a centralized system:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluent-bit-agent
spec:
selector:
matchLabels:
app: fluent-bit-agent
template:
metadata:
labels:
app: fluent-bit-agent
spec:
containers:
- name: fluent-bit
image: fluent/fluent-bit:latest
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
8. Log Rotation (Configuration):
Log rotation is typically handled by the logging agent or centralized logging system. Consult your chosen solution's documentation for configuration details.
9. Multi-line Logs (Configuration):
Similar to log rotation, handling multi-line logs is a configuration aspect of your logging tools. Refer to their documentation for specific instructions.
10. Stdout and Stderr (Application Code):
Ensure your application code writes logs to stdout and stderr. Most programming languages and frameworks provide mechanisms for this.
Example (Python):
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
# Your application logic here
result = 10 / 0
except ZeroDivisionError as e:
logger.exception("An error occurred: %s", e)
This code configures a basic logger that outputs to stderr by default.
Remember to adapt these examples to your specific needs and environment.
kubectl logs
is useful for basic troubleshooting, it has limitations in production. It doesn't provide aggregation, search, or long-term storage.-f
can consume significant resources, especially in large clusters. Use it judiciously.kubectl get events
to view them.This table summarizes common methods for viewing and managing logs in Kubernetes:
Feature | Command | Description |
---|---|---|
Basic Log Viewing | kubectl logs <pod-name> |
View logs from a specific pod. |
Real-time Logs | kubectl logs <pod-name> -f |
Stream logs in real time. |
Previous Pod Logs | kubectl logs <pod-name> --previous |
View logs from a previous instance of a pod. |
Container-Specific Logs | kubectl logs <pod-name> -c <container-name> |
View logs from a specific container within a pod. |
Timestamped Logs | kubectl logs <pod-name> -t |
Include timestamps in the log output. |
Limited Log Lines | kubectl logs <pod-name> --tail=<number> |
Limit the output to a specific number of lines from the end of the log. |
Centralized Logging (Recommended) | Implement a centralized logging solution like the PLG stack (Prometheus, Loki, Grafana) or similar tools for production environments. | |
Log Rotation | Ensure your logging solution handles log rotation properly to avoid missing logs. | |
Multi-line Log Handling | Configure your logging tools to handle multi-line logs, especially for applications that produce stack traces. | |
Stdout and Stderr | Configure applications to write logs to stdout and stderr for easier collection in Kubernetes. |
Effective log management is crucial for troubleshooting and monitoring applications in Kubernetes. While kubectl logs
is useful for basic log viewing, centralized logging solutions are essential for production environments. These solutions offer features like aggregation, search, and long-term storage, simplifying log management in complex deployments. Remember to configure log rotation, handle multi-line logs, and ensure applications write logs to stdout and stderr for seamless log collection. By implementing these best practices and choosing the right logging tools, you can gain valuable insights from your logs, improve application performance, and ensure the smooth operation of your Kubernetes applications.
view-secret
, view-cert
, and stern
āto simplify managing CloudNativePG in Kubernetes. Easily inspect secrets, verify certificates, and tail logs for efficient PostgreSQL management.