Files
jenkins-pipeline/k8s_yaml/kubectl 命令/查看pod日志.md
2025-10-30 11:20:06 +08:00

85 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 对应关系总结
| Linux 命令 | kubectl logs 等效命令 |
|---------------:|---------------------|
| cat logfile | `kubectl logs <pod-name>` |
| tail -n 10 | `kubectl logs --tail=10 <pod-name>` |
| tail -f | `kubectl logs -f <pod-name>` |
| head -n 10 | `kubectl logs --tail=1000 <pod-name> \| head -n 10` |
| grep "error" | `kubectl logs <pod-name> \| grep "error"` |
> 说明:`head` 在 kubectl 中没有直接等价参数,常用方法是获取较多末尾行再用 `head` 截取,或在集群外使用日志聚合/查询工具。
## 查看 Pod 日志示例pod=test-flymoon-admin-deployment-964fb6b74-ns4brnamespace=`test-lessie`
显示所有日志内容(等同于 cat
```bash
kubectl logs test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看最后 10 行(等同于 tail -n 10
```bash
kubectl logs --tail=10 test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看最后 50 行
```bash
kubectl logs --tail=50 test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看前 10 行(等同于 head -n 10
```bash
kubectl logs --tail=1000 test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie | head -n 10
```
实时跟踪日志(等同于 tail -f
```bash
kubectl logs -f test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
从最后 10 行开始实时跟踪
```bash
kubectl logs -f --tail=10 test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
实时跟踪并显示时间戳
```bash
kubectl logs -f --timestamps test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看最近 1 小时的日志
```bash
kubectl logs --since=1h test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看最近 10 分钟的日志
```bash
kubectl logs --since=10m test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看指定时间以来的日志(使用 RFC3339 时间格式)
```bash
kubectl logs --since=2024-01-15T10:00:00Z test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
查看之前实例的日志Pod 重启后)
```bash
kubectl logs --previous test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
只查看特定容器的日志Pod 有多个容器时)
```bash
kubectl logs -c <container-name> test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
带时间戳显示
```bash
kubectl logs --timestamps test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```
限制日志字节数
```bash
kubectl logs --limit-bytes=10240 test-flymoon-admin-deployment-964fb6b74-ns4br -n test-lessie
```