日志与监控
CKA 考试 Domain 5 — kubectl logs、kubectl describe、kubectl events、kubectl top、JSONPath、自定义列输出
← 返回 CKA 练习目录 日志查看和资源监控是排查问题和掌握集群状态的基本手段。CKA 考试中需要熟练掌握
kubectl logs、kubectl top、JSONPath、custom-columns等命令。
1. kubectl logs 查看 Pod 日志
基本用法
# 查看 Pod 日志(当前容器)
kubectl logs <pod-name>
# 带时间戳查看日志
kubectl logs <pod-name> --timestamps
# 跟踪日志输出(类似 tail -f)
kubectl logs <pod-name> -f
# 查看前一个崩溃实例的日志(CrashLoopBackOff 时使用)
kubectl logs <pod-name> --previous
# 仅查看最后 N 行
kubectl logs <pod-name> --tail=50
# 查看最近一段时间的日志
kubectl logs <pod-name> --since=5m
kubectl logs <pod-name> --since-time="2026-05-27T10:00:00Z"
多容器 Pod
# 指定容器
kubectl logs <pod-name> -c <container-name>
# 带跟踪和前一个崩溃日志
kubectl logs <pod-name> -c <container-name> -f --previous
标签选择器
# 查看所有匹配标签的 Pod 日志
kubectl logs -l app=nginx --tail=20
# 查看 Deployment 下所有 Pod 日志
kubectl logs -l app=<deployment-label>
2. kubectl logs 参数速查表
| 参数 | 用途 | 示例 |
|---|---|---|
-f | 跟踪日志输出 | kubectl logs -f <pod> |
--tail=N | 只显示最后 N 行 | kubectl logs --tail=100 <pod> |
--since=5m | 显示最近 5 分钟日志 | kubectl logs --since=5m <pod> |
--since-time= | 从指定时间开始显示 | kubectl logs --since-time="2026-05-27T00:00:00Z" <pod> |
--previous | 查看前一个容器实例日志 | kubectl logs --previous <pod> |
--timestamps | 显示时间戳 | kubectl logs --timestamps <pod> |
--prefix | 在每行前加上元数据前缀 | kubectl logs --prefix <pod> |
-c | 指定容器名称 | kubectl logs <pod> -c <container> |
-l | 使用标签选择器 | kubectl logs -l app=nginx |
3. kubectl describe 资源详情
# 查看 Pod 详情(最常用)
kubectl describe pod <pod-name>
# 查看 Node 详情
kubectl describe node <node-name>
# 查看 Service 详情
kubectl describe svc <service-name>
# 查看 PVC 详情
kubectl describe pvc <pvc-name>
# 查看 PV 详情
kubectl describe pv <pv-name>
# 查看 Deployment 详情
kubectl describe deployment <deployment-name>
# 查看所有资源(按资源类型)
kubectl describe <resource-type> <resource-name>
关键关注点:
| 资源 | 关注部分 |
|---|---|
| Pod | Status、Conditions、Events |
| Node | Conditions、Capacity、Allocatable、Events |
| Service | Type、ClusterIP、Endpoints |
| Deployment | Replicas、Conditions、Events |
4. kubectl get events
# 查看集群事件(按时间排序)
kubectl get events --sort-by='.lastTimestamp'
# 查看所有命名空间的事件
kubectl get events --all-namespaces
# 查看特定命名空间的事件
kubectl get events -n <namespace>
# 跟踪实时事件输出
kubectl get events --watch
# 过滤 Warning 级别事件
kubectl get events --field-selector type=Warning
# 查看特定 Pod 相关的事件
kubectl get events --field-selector involvedObject.name=<pod-name>
# 自定义事件输出
kubectl get events \
-o custom-columns=LAST_SEEN:.lastTimestamp,TYPE:.type,REASON:.reason,OBJECT:.involvedObject.name,MESSAGE:.message
5. kubectl top 资源监控
需要集群中运行 Metrics Server。
# 查看节点资源使用
kubectl top node
kubectl top node <node-name>
# 查看 Pod 资源使用
kubectl top pod
kubectl top pod <pod-name>
kubectl top pod -n <namespace>
# 查看所有命名空间 Pod
kubectl top pod --all-namespaces
# 按 CPU 或内存排序
kubectl top pod --sort-by=cpu
kubectl top pod --sort-by=memory
kubectl top pod -n kube-system --sort-by=memory
# 查看特定标签的 Pod
kubectl top pod -l app=nginx
6. JSONPath 自定义输出
JSONPath 用于从 Kubernetes API 返回的 JSON 数据中提取特定字段,非常强大。
基础用法
# 获取所有 Pod 的名称
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
# 获取 Pod 的 IP
kubectl get pods -o jsonpath='{.items[*].status.podIP}'
# 获取节点 IP
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}'
格式化输出
# 表格格式输出 Pod 名和 IP
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'
# 输出 Pod 名、节点名、状态
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.nodeName}{"\t"}{.status.phase}{"\n"}{end}'
# 输出所有命名空间 + Pod 名
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}'
# 获取节点的可分配 CPU 和内存
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.allocatable.cpu}{"\t"}{.status.allocatable.memory}{"\n"}{end}'
JSONPath 常用函数
| 表达式 | 说明 | 示例 |
|---|---|---|
$ | 根节点 | {$} |
. | 子节点 | .metadata.name |
.. | 递归搜索 | $..metadata.name |
[n] | 数组索引 | .items[0] |
[*] | 所有元素 | .items[*] |
?() | 过滤 | ?(@.status.phase=="Running") |
range | 遍历 | {range .items[*]}...{end} |
"\t" | 制表符 | 用于格式化表格 |
"\n" | 换行符 | 用于格式化表格 |
高级过滤
# 获取特定状态的 Pod
kubectl get pods -o jsonpath='{range .items[?(@.status.phase=="Running")]}{.metadata.name}{"\n"}{end}'
# 获取在特定节点上的 Pod
kubectl get pods -o jsonpath='{range .items[?(@.spec.nodeName=="node1")]}{.metadata.name}{"\n"}{end}'
# 获取 Ready 状态的节点
kubectl get nodes -o jsonpath='{range .items[?(@.status.conditions[?(@.type=="Ready")].status=="True")]}{.metadata.name}{"\n"}{end}'
# 获取镜像名称(去重)
kubectl get pods -o jsonpath='{range .items[*]}{.spec.containers[*].image}{"\n"}{end}' | sort -u
7. 自定义列输出
# 定义自定义列:NAME, NODE, STATUS
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,STATUS:.status.phase
# 更复杂的自定义列
kubectl get pods -o custom-columns=\
NAME:.metadata.name,\
NODE:.spec.nodeName,\
STATUS:.status.phase,\
IP:.status.podIP,\
NAMESPACE:.metadata.namespace
# 节点自定义列
kubectl get nodes -o custom-columns=\
NAME:.metadata.name,\
INTERNAL-IP:.status.addresses[?\(@.type==\"InternalIP\"\)].address,\
CPU:.status.allocatable.cpu,\
MEMORY:.status.allocatable.memory
# 所有命名空间的 Deployment 副本数
kubectl get deployments --all-namespaces -o custom-columns=\
NAMESPACE:.metadata.namespace,\
NAME:.metadata.name,\
READY:.status.readyReplicas,\
DESIRED:.spec.replicas
注意:在 custom-columns 的 JSONPath 表达式中,
?(@.type=="InternalIP")中的双引号需要用反斜杠转义。
8. 考试高频命令组合
# 1. 快速查找异常 Pod
kubectl get pods --all-namespaces | grep -v Running | grep -v Completed
# 2. 查看 Pod 日志
kubectl logs <pod-name> --tail=20
# 3. 查看崩溃 Pod 的前一次日志
kubectl logs <pod-name> --previous
# 4. 实时查看所有命名空间事件
kubectl get events -A -w
# 5. Pod 和节点资源排行
kubectl top pod --sort-by=memory
kubectl top node --sort-by=cpu
# 6. 自定义 Pod 列表(包含节点和 IP)
kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,IP:.status.podIP,STATUS:.status.phase
# 7. 节点的可分配资源
kubectl get nodes -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}CPU: {.status.allocatable.cpu}{"\t"}MEM: {.status.allocatable.memory}{"\n"}{end}'
# 8. 检查镜像拉取策略
kubectl get pods -o custom-columns=NAME:.metadata.name,IMAGEPULLPOLICY:.spec.containers[*].imagePullPolicy
9. 考试要点
kubectl logs --previous是查看崩溃容器日志的关键kubectl get events --sort-by='.lastTimestamp'按时间排序查看事件kubectl top需要 Metrics Server,考试中通常已安装- JSONPath 和
custom-columns是 CKA 高频考点——必须熟练掌握 --all-namespaces缩写-Akubectl describe的 Events 部分是最重要的诊断信息
🧪 完整操作实例:多容器 Pod 日志收集与资源监控
场景描述
在一个包含主容器和 sidecar 容器的多容器 Pod 中,查看各容器日志、监控资源使用情况,并使用 JSONPath 和 events 命令进行高级排查。
前置条件
- 集群中已安装 Metrics Server
- 有创建 Pod 和查看日志的权限
操作步骤
Step 1: 创建一个多容器 Pod
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: multi-container-pod
labels:
app: web-app
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
- name: sidecar
image: busybox
command: ["sh", "-c", "while true; do echo \"[sidecar] Log entry at \$(date)\"; sleep 5; done"]
EOF
kubectl get pods -w
# NAME READY STATUS RESTARTS AGE
# multi-container-pod 2/2 Running 0 30s
Step 2: 查看主容器(nginx)的日志
kubectl logs multi-container-pod -c nginx
# 默认 nginx 日志输出到文件,容器内 stdout 可能为空
# 或使用 --tail 查看最新日志
kubectl logs multi-container-pod -c nginx --tail=10
Step 3: 查看 sidecar 容器的日志
kubectl logs multi-container-pod -c sidecar
# [sidecar] Log entry at Wed May 27 10:00:05 UTC 2026
# [sidecar] Log entry at Wed May 27 10:00:10 UTC 2026
# [sidecar] Log entry at Wed May 27 10:00:15 UTC 2026
# ...
# 使用 -f 跟踪实时日志
kubectl logs multi-container-pod -c sidecar -f --tail=5
# 输出最新的 5 行并持续跟踪
Step 4: 模拟容器崩溃并查看前一次日志
# 假设 sidecar 崩溃过,查看前一次日志
kubectl logs multi-container-pod -c sidecar --previous
# (如果有前一次实例,会显示其日志)
Step 5: 使用 kubectl top 监控资源使用
# 查看节点资源使用情况
kubectl top node
# NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
# master-node 350m 17% 1200Mi 31%
# worker-node1 280m 14% 980Mi 25%
# 查看 Pod 资源使用情况
kubectl top pod multi-container-pod
# NAME CPU(cores) MEMORY(bytes)
# multi-container-pod 12m 45Mi
# 按 CPU 排序查看所有 Pod
kubectl top pod --sort-by=cpu --all-namespaces
# NAMESPACE NAME CPU(cores) MEMORY(bytes)
# kube-system kube-apiserver-master-node 150m 512Mi
# default multi-container-pod 12m 45Mi
# ...
Step 6: 查看集群事件
# 查看所有事件并按时间排序
kubectl get events --sort-by='.lastTimestamp'
# LAST SEEN TYPE REASON OBJECT MESSAGE
# 5m Normal Scheduled pod/multi-container-pod Successfully assigned default/multi-container-pod to worker-node1
# 5m Normal Pulling pod/multi-container-pod Pulling image "nginx:latest"
# 5m Normal Pulled pod/multi-container-pod Successfully pulled image "nginx:latest"
# 5m Normal Created pod/multi-container-pod Created container nginx
# 5m Normal Started pod/multi-container-pod Started container nginx
# 只查看 Warning 级别事件
kubectl get events --field-selector type=Warning
Step 7: 使用 JSONPath 提取 Pod 信息
# 获取所有 Pod 的名称
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
# multi-container-pod
# 格式化输出:Pod 名、节点、IP、状态
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.nodeName}{"\t"}{.status.podIP}{"\t"}{.status.phase}{"\n"}{end}'
# multi-container-pod worker-node1 10.244.1.5 Running
# 获取容器的镜像名称(多容器)
kubectl get pod multi-container-pod -o jsonpath='{range .spec.containers[*]}{.name}{"\t"}{.image}{"\n"}{end}'
# nginx nginx:latest
# sidecar busybox:latest
验证结果
# 验证所有容器日志可正常查看
kubectl logs multi-container-pod -c nginx --tail=3
kubectl logs multi-container-pod -c sidecar --tail=3
# 验证资源监控可用
kubectl top pod multi-container-pod
# 验证 JSONPath 查询
kubectl get pods -o jsonpath='{.items[0].status.containerStatuses[*].ready}'
# true true
考试提示
- 多容器 Pod 使用
-c <container-name>指定容器查看日志 --tail=N限制日志行数,-f实时跟踪,--previous查看崩溃前日志kubectl top node/pod需要 Metrics Server 安装kubectl get events --sort-by='.lastTimestamp'是查看按时间排序的事件的关键命令- JSONPath 和
custom-columns是 CKA 高频考点,尤其{range .items[*]}...{end}遍历语法 - 考试中如果
kubectl top报错,通常意味着 Metrics Server 未安装