K8s 日常运维指令与问题排查实战
日常基础命令
Section titled “日常基础命令”集群信息查看
Section titled “集群信息查看”# 查看集群节点状态kubectl get nodes
# 查看节点详细信息,包括 IP、容量、状态kubectl get nodes -o wide
# 以 YAML 格式输出节点详情kubectl get nodes -o yaml
# 查看集群组件健康状态(需要 kubeconfig 权限)kubectl get componentstatuses # 已被废弃kubectl get --raw '/healthz?verbose'
# 查看 API Server 版本信息kubectl version --short
# 查看集群信息总览kubectl cluster-info上下文与配置
Section titled “上下文与配置”# 查看当前上下文(集群 + 用户 + 命名空间)kubectl config current-context
# 查看所有上下文kubectl config get-contexts
# 切换上下文kubectl config use-context <context-name>
# 临时在命令中指定命名空间(不用切换上下文)kubectl get pods -n <namespace>
# 设置默认命名空间kubectl config set-context --current --namespace=<namespace>Pod 相关操作
Section titled “Pod 相关操作”Pod 查看与检索
Section titled “Pod 查看与检索”# 查看所有命名空间的 Podkubectl get pods --all-namespaces
# 查看指定命名空间的 Podkubectl get pods -n <namespace>
# 查看 Pod 详细信息(状态、Restart 次数、所在节点)kubectl describe pod <pod-name> -n <namespace>
# 查看 Pod 的实时状态变化(Watch 模式)kubectl get pods -n <namespace> -w
# 按标签筛选 Podkubectl get pods -n <namespace> -l app=nginx
# 查看不受 Deployment 管理的 Pod(Static Pod 或直接创建的)kubectl get pods --all-namespaces | grep -v ReplicationController
# 查找处于非 Running 状态的 Podkubectl get pods -A | grep -v Running
# 统计各命名空间 Pod 数量kubectl get pods -A --no-headers | awk '{print $1}' | sort | uniq -cPod 日志查看
Section titled “Pod 日志查看”# 查看 Pod 日志(实时跟踪加 -f)kubectl logs <pod-name> -n <namespace>
# 实时跟踪日志kubectl logs -f <pod-name> -n <namespace>
# 查看上一个版本(容器重启后)的日志kubectl logs --previous <pod-name> -n <namespace>
# 多容器 Pod 查看指定容器日志kubectl logs <pod-name> -c <container-name> -n <namespace>
# 实时跟踪多容器 Pod 指定容器日志kubectl logs -f <pod-name> -c <container-name> -n <namespace>
# 查看最近 100 行日志kubectl logs --tail=100 <pod-name> -n <namespace>
# 按时间范围过滤日志(需要容器内支持)kubectl logs --since=1h <pod-name> -n <namespace>
# 导出 Pod 日志到文件kubectl logs <pod-name> -n <namespace> > /tmp/pod.logPod 调试与执行命令
Section titled “Pod 调试与执行命令”# 进入 Pod 容器(如有多个容器用 -c 指定)kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
# 指定容器进入kubectl exec -it <pod-name> -c <container-name> -n <namespace> -- /bin/sh
# 在 Pod 中执行单个命令kubectl exec <pod-name> -n <namespace> -- ls /app
# 复制文件到 Podkubectl cp <file> <namespace>/<pod-name>:/path/in/pod
# 从 Pod 复制文件到本地kubectl cp <namespace>/<pod-name>:/path/in/pod/<file> ./local/pathPod 扩缩容
Section titled “Pod 扩缩容”# Scale 扩缩容(不推荐直接使用,推荐用 HPA)kubectl scale deployment <deployment-name> --replicas=5 -n <namespace>
# 基于 CPU/内存自动扩缩容kubectl autoscale deployment <deployment-name> --min=2 --max=10 --cpu-percent=80 -n <namespace>
# 查看 HPA 状态kubectl get hpa -n <namespace>
# 删除 HPAkubectl delete hpa <hpa-name> -n <namespace>Deployment 操作
Section titled “Deployment 操作”# 查看所有 Deploymentkubectl get deployments -n <namespace>
# 查看 Deployment 详细信息kubectl describe deployment <deployment-name> -n <namespace>
# 滚动重启 Deployment(零宕机部署)kubectl rollout restart deployment <deployment-name> -n <namespace>
# 查看滚动更新状态kubectl rollout status deployment/<deployment-name> -n <namespace>
# 查看滚动更新历史kubectl rollout history deployment/<deployment-name> -n <namespace>
# 回滚到上一个版本kubectl rollout undo deployment/<deployment-name> -n <namespace>
# 回滚到指定版本kubectl rollout undo deployment/<deployment-name> --to-revision=<revision> -n <namespace>
# 暂停/恢复滚动更新kubectl rollout pause deployment/<deployment-name> -n <namespace>kubectl rollout resume deployment/<deployment-name> -n <namespace>资源创建与删除
Section titled “资源创建与删除”# 从 YAML 创建资源kubectl apply -f <file.yaml>
# 从目录递归创建kubectl apply -f <directory/>
# 删除资源kubectl delete -f <file.yaml>
# 删除指定类型的所有资源kubectl delete deployment --all -n <namespace>
# 强制删除 Terminating 状态的 Pod(慎用!)kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --force
# 清理已完成的 Job 和 Completed 状态的 Podkubectl delete pod --field-selector=status.phase=Succeeded -n <namespace>kubectl delete job <job-name> -n <namespace>资源配额与限制
Section titled “资源配额与限制”# 查看命名空间资源配额kubectl get resourcequota -n <namespace>
# 查看命名空间资源限制(LimitRange)kubectl get limitrange -n <namespace>
# 查看 Pod 的资源请求和限制kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.containers[*].resources}'
# 集群级别资源使用统计kubectl top nodeskubectl top pods -n <namespace>日志查看与调试
Section titled “日志查看与调试”常见日志场景
Section titled “常见日志场景”# 场景1:Pod 一直处于 Pending 状态# 通常是调度问题或资源不足kubectl describe pod <pod-name> -n <namespace> | grep -A 10 "Events:"kubectl get events -n <namespace> --sort-by='.lastTimestamp'
# 场景2:Pod 一直处于 ImagePullBackOff# 镜像名称错误或 Registry 权限问题kubectl describe pod <pod-name> -n <namespace> | grep -A 5 "Events:"# 检查镜像是否可访问crictl images | grep <image-name>
# 场景3:Pod 一直处于 CrashLoopBackOff# 应用程序启动失败,查看退出码和日志kubectl describe pod <pod-name> -n <namespace> | grep "Exit Code"kubectl logs --previous <pod-name> -n <namespace>
# 场景4:Pod 处于 Terminating 无法删除kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --force# 确认没有 Finalizers 阻止删除kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.finalizers}'全局日志查询
Section titled “全局日志查询”# 查看最近所有命名空间的重要事件kubectl get events -A --sort-by='.lastTimestamp' | tail -50
# 按资源类型过滤事件kubectl get events -A --field-selector involvedObject.kind=Pod
# 实时查看某命名空间所有 Pod 日志(多容器场景)kubectl logs -f <pod-name> -n <namespace> --all-containers=true
# 将某 Deploy 所有 Pod 日志汇总for pod in $(kubectl get pods -n <namespace> -l app=<app-name> -o jsonpath='{.items[*].metadata.name}'); do echo "=== $pod ===" kubectl logs "$pod" -n <namespace> --tail=50done网络问题排查
Section titled “网络问题排查”基础网络检查
Section titled “基础网络检查”# 查看 Service 列表kubectl get svc -n <namespace>
# 查看 Endpoint(确认后端 Pod 是否关联)kubectl get endpoints <service-name> -n <namespace>
# 查看 Ingresskubectl get ingress -n <namespace>
# 描述 Service 查看详情kubectl describe svc <service-name> -n <namespace>
# ClusterIP 是否能通信(在 Pod 内测试)kubectl exec -it <pod-name> -n <namespace> -- curl -s http://<cluster-ip>:<port>DNS 调试
Section titled “DNS 调试”# 查看 CoreDNS 是否运行正常kubectl get pods -n kube-system -l k8s-app=kube-dns
# 测试集群内 DNS 解析kubectl exec -it <pod-name> -n <namespace> -- nslookup kubernetes.defaultkubectl exec -it <pod-name> -n <namespace> -- nslookup <service-name>.<namespace>.svc.cluster.local
# 进入网络调试容器kubectl run netshoot --image=nicolaka/netshoot -n <namespace> --rm -it -- /bin/bash
# 在 netshoot 容器中测试 DNSnslookup kubernetes.default.svc.cluster.localdig kubernetes.default.svc.cluster.local网络连通性测试
Section titled “网络连通性测试”# 测试 Service 连通性(ClusterIP → Pod)kubectl exec -it <pod-name> -n <namespace> -- curl -s http://<service-name>.<namespace>.svc.cluster.local:<port>
# 测试节点端口(NodePort)curl http://<node-ip>:<node-port>
# 测试 ExternalTrafficPolicy 是否有问题kubectl get svc <service-name> -n <namespace> -o jsonpath='{.spec.externalTrafficPolicy}'
# 抓包分析(需要 netshoot 镜像)kubectl exec -it <pod-name> -n <namespace> -- tcpdump -i any -c 100 -w /tmp/capture.pcapkubectl exec <pod-name> -n <namespace> -- tcpdump -i eth0 port 80
# 查看 iptables 规则(节点上)iptables -L -n -t nat | grep <service-name>ipvsadm -L -n | grep <service-ip>Ingress 调试
Section titled “Ingress 调试”# 查看 Ingress Controller 是否正常kubectl get pods -n ingress-nginx
# 查看 Ingress 详细事件kubectl describe ingress <ingress-name> -n <namespace>
# 测试 Ingress 域名解析nslookup <ingress-domain>
# 测试 Ingress 访问(Host Header 必须正确)curl -v -H "Host: <ingress-domain>" http://<ingress-controller-ip>/存储问题排查
Section titled “存储问题排查”PVC 与 PV
Section titled “PVC 与 PV”# 查看 PVC 状态kubectl get pvc -n <namespace>
# 查看 PV 状态kubectl get pv
# 查看 PVC 详细信息kubectl describe pvc <pvc-name> -n <namespace>
# 查找 PVC 未绑定原因kubectl get events -n <namespace> --field-selector involvedObject.name=<pvc-name>
# StorageClass 是否存在kubectl get storageclass
# 查看 PV 的绑定情况kubectl get pv -o wide常见存储问题
Section titled “常见存储问题”# 场景1:PVC 一直处于 Pending# 检查 StorageClass 是否存在kubectl get storageclass# 检查 PVC 的 storageClassName 是否正确# 查看相关的事件日志kubectl describe pvc <pvc-name> -n <namespace>
# 场景2:Pod 无法挂载 Volumekubectl describe pod <pod-name> -n <namespace> | grep -A 10 "Volumes:"kubectl describe pod <pod-name> -n <namespace> | grep -A 5 "Mounts:"
# 场景3:检查节点上存储插件是否正常# NFSshowmount -e <nfs-server># Cephceph status# 其他 CSIkubectl get csidriver常见问题排查案例
Section titled “常见问题排查案例”案例一:Pod 启动后立即崩溃(CrashLoopBackOff)
Section titled “案例一:Pod 启动后立即崩溃(CrashLoopBackOff)”# 第一步:查看 Pod 状态kubectl get pod <pod-name> -n <namespace>
# 第二步:查看最近一次退出的原因kubectl describe pod <pod-name> -n <namespace> | grep -E "Exit Code|Reason|State"
# 第三步:查看应用日志kubectl logs --previous <pod-name> -n <namespace>
# 第四步:常见原因及解决# 原因1:健康检查失败(StartupProbe / Readiness / Liveness)# 解决:调整探针参数或修复应用kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.containers[*].livenessProbe}'kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.containers[*].readinessProbe}'
# 原因2:OOMKilled(内存超限)# 解决:增加 memory limit 或优化应用内存使用kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.status.containerStatuses[*].lastState.terminated.exitCode}'
# 原因3:权限问题(SecurityContext / SELinux)# 解决:调整 Pod SecurityContext 或容器运行用户案例二:Service 无法访问
Section titled “案例二:Service 无法访问”# 第一步:确认 Endpoint 是否存在kubectl get endpoints <service-name> -n <namespace>
# Endpoint 为空的原因:# 1. Selector 没有匹配到任何 Podkubectl describe svc <service-name> -n <namespace> | grep Selectorkubectl get pods -n <namespace> -l <selector-label>
# 2. 后端 Pod 未就绪(Readiness Gate 问题)kubectl describe pod <pod-name> -n <namespace> | grep -A 10 "Conditions"
# 第二步:在 Pod 内测试kubectl exec -it <pod-name> -n <namespace> -- curl -v telnet://localhost:<port>
# 第三步:确认端口是否一致# Service port vs Container portkubectl get svc <service-name> -n <namespace> -o jsonpath='{.spec.ports[*]}'案例三:节点 NotReady
Section titled “案例三:节点 NotReady”# 查看节点状态和原因kubectl get nodeskubectl describe node <node-name>
# 常见原因:# 1. kubelet 不健康ssh <node-ip> "systemctl status kubelet"
# 2. 节点资源耗尽(内存、磁盘、inode)ssh <node-ip> "df -h" # 磁盘ssh <node-ip> "free -m" # 内存ssh <node-ip> "df -i" # inode
# 3. 网络分区ssh <node-ip> "ip route"ssh <node-ip> "ping <api-server-ip>"
# 4. 节点负载过高kubectl top node <node-name>
# 驱逐节点上的 Pod(标记为不可调度后,手动驱逐)kubectl cordon <node-name> # 标记为不可调度kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data # 驱逐 Pod案例四:Deployment 滚动更新卡住
Section titled “案例四:Deployment 滚动更新卡住”# 查看滚动更新状态kubectl rollout status deployment/<deployment-name> -n <namespace>
# 查看 Deployment 事件kubectl describe deployment <deployment-name> -n <namespace>
# 常见原因:# 1. 镜像拉取失败(滚动更新无法启动新 Pod)kubectl get events -n <namespace> --field-selector involvedObject.kind=Pod | grep <new-pod>
# 2. 资源不足(没有足够的 CPU/内存调度新 Pod)kubectl describe deployment <deployment-name> -n <namespace> | grep -A 5 "Conditions"
# 3. 就绪探针一直失败(新 Pod 无法变为 Ready)kubectl get pods -n <namespace> -l app=<app-name>
# 解决方法:回滚或修复后继续kubectl rollout undo deployment/<deployment-name> -n <namespace>性能分析与监控
Section titled “性能分析与监控”资源使用分析
Section titled “资源使用分析”# 查看集群节点 CPU/内存使用kubectl top nodes
# 查看 Pod 资源使用(需要 metrics-server)kubectl top pods -n <namespace>
# 按资源使用排序查看 Podkubectl top pods -n <namespace> --sort-by=memorykubectl top pods -n <namespace> --sort-by=cpu
# 查看指定命名空间所有容器的资源使用kubectl top pods -n <namespace> --all-namespaces=false
# 对比 requests 和实际使用kubectl get pod <pod-name> -n <namespace> -o json | \ jq '.spec.containers[0].resources.requests, .spec.containers[0].resources.limits'调度问题分析
Section titled “调度问题分析”# 查看 Pod 调度决策kubectl describe pod <pod-name> -n <namespace> | grep -A 20 "Events:"
# 检查节点亲和性/反亲和性kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.affinity}'
# 检查污点(Taints)影响kubectl describe node <node-name> | grep Taints
# 检查 Pod 优先级kubectl get pod <pod-name> -n <namespace> -o jsonpath='{.spec.priorityClassName}'
# 调度器日志(需要 kube-system 权限)kubectl logs -n kube-system kube-scheduler-<node-name> --tail=100API 对象查询技巧
Section titled “API 对象查询技巧”# 格式化输出(YAML / JSON)kubectl get pod <pod-name> -n <namespace> -o yamlkubectl get pod <pod-name> -n <namespace> -o json
# 按列筛选输出kubectl get pods -n <namespace> -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName
# 统计某类资源数量kubectl api-resources --verbs=list -o name | xargs -I {} sh -c "echo -n '{}: '; kubectl get {} -A --no-headers 2>/dev/null | wc -l"
# 查看对象完整 JSON(方便调试字段)kubectl get pod <pod-name> -n <namespace> -o json | jq '.spec.containers[0].env'常用运维脚本
Section titled “常用运维脚本”一键排查 Pod 问题
Section titled “一键排查 Pod 问题”#!/bin/bash# 命名空间和问题 Pod 名NS=$1POD=$2
echo "========== 基础状态 =========="kubectl get pod "$POD" -n "$NS"
echo "========== Describe =========="kubectl describe pod "$POD" -n "$NS"
echo "========== 最近日志 =========="kubectl logs --tail=100 "$POD" -n "$NS"
echo "========== 上次退出日志 =========="kubectl logs --previous "$POD" -n "$NS" 2>&1 || echo "无上次日志"
echo "========== 事件 =========="kubectl get events -n "$NS" --field-selector involvedObject.name="$POD" --sort-by='.lastTimestamp'批量查看所有 Deploy 的状态
Section titled “批量查看所有 Deploy 的状态”kubectl get deployments -A -o wide | awk 'NR==1{print;next} {print}' | column -t
for dep in $(kubectl get deployments -A -o jsonpath='{.items[*].metadata.name}'); do NS=$(kubectl get deployments "$dep" -o jsonpath='{.metadata.namespace}') echo "Deployment: $NS/$dep" kubectl rollout status "deployment/$dep" -n "$NS" --timeout=10s 2>&1 || echo "状态异常"done查找资源使用异常的 Pod
Section titled “查找资源使用异常的 Pod”# CPU 使用最高的 10 个 Podkubectl top pods -A --sort-by=cpu | tail -n +2 | tail -10
# 内存使用最高的 10 个 Podkubectl top pods -A --sort-by=memory | tail -n +2 | tail -10
# restart 次数最多的 Podkubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\t"}{.status.containerStatuses[*].restartCount}{"\n"}{end}' | \ awk '{for(i=3;i<=NF;i++) sum+=$i} END{print}' | sort -rn | head本文覆盖了 Kubernetes 日常运维中最核心的指令和问题排查思路。关键要点:
- 善用
kubectl describe和 events — 大多数问题从事件日志中能找到根因 - 分层排查 — 从集群 → 节点 → 网络 → Pod → 容器逐层定位
- 日志是金 — 应用日志、Kubelet 日志、Docker/Crictl 日志各有各的价值
- 理解资源限制 — OOM、CPU Throttling 往往是性能问题的根源
- 滚动更新和回滚是运维的生命线 — 任何变更都要留好回退方案
日常高频操作建议配合 kubectl alias 使用:
alias k='kubectl'alias kgp='kubectl get pods'alias kgd='kubectl get deployment'alias kgs='kubectl get svc'alias kds='kubectl describe svc'alias kdp='kubectl describe pod'alias klf='kubectl logs -f'alias kex='kubectl exec -it'希望这篇文章能帮助你在日常 K8s 运维中快速定位问题、减少排查时间 🚀