Linux command
kubectl-logs 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
View pod logs
kubectl logs [pod-name]
Follow logs in real-time
kubectl logs -f [pod-name]
View specific container logs
kubectl logs [pod-name] -c [container]
View previous container logs
kubectl logs [pod-name] --previous
Show last N lines
kubectl logs [pod-name] --tail=[100]
View logs from last hour
kubectl logs [pod-name] --since=[1h]
说明
kubectl logs retrieves and displays the standard output and standard error streams from containers running inside Kubernetes pods. It is one of the primary tools for debugging application behavior, diagnosing crashes, and monitoring runtime output in a cluster environment. The command supports real-time log streaming with the -f flag, similar to `tail -f`, and can target a specific container within a multi-container pod using the -c option. You can also view logs from a previous container instance (useful for inspecting crash loops via --previous), limit output to recent entries with --tail, or filter by time window using --since and --since-time. When used with label selectors, it can aggregate logs across multiple pods simultaneously.
参数
- -f, --follow
- Stream logs continuously.
- -c, --container _name_
- Container name.
- --previous
- Print previous container logs.
- --tail _lines_
- Number of lines from end.
- --since _duration_
- Show logs newer than duration.
- --timestamps
- Include timestamps.
FAQ
What is the kubectl-logs command used for?
kubectl logs retrieves and displays the standard output and standard error streams from containers running inside Kubernetes pods. It is one of the primary tools for debugging application behavior, diagnosing crashes, and monitoring runtime output in a cluster environment. The command supports real-time log streaming with the -f flag, similar to `tail -f`, and can target a specific container within a multi-container pod using the -c option. You can also view logs from a previous container instance (useful for inspecting crash loops via --previous), limit output to recent entries with --tail, or filter by time window using --since and --since-time. When used with label selectors, it can aggregate logs across multiple pods simultaneously.
How do I run a basic kubectl-logs example?
Run `kubectl logs [pod-name]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -f, --follow do in kubectl-logs?
Stream logs continuously.