Kubernetes运维速查手册

Kubernetes 运维速查手册

适用于日常巡检、发布、扩缩容、故障排查和应急处理。示例中的 <namespace><pod> 等占位符需要替换为实际值。

1. 常用变量与别名

1
2
3
4
5
6
7
8
export NS=default

alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get services'
alias kgd='kubectl get deployments'
alias kgn='kubectl get nodes'
alias kdp='kubectl describe pod'

启用 Bash 自动补全:

1
2
source <(kubectl completion bash)
complete -o default -F __start_kubectl k

2. 集群与上下文

操作 命令
查看集群信息 kubectl cluster-info
查看客户端和服务端版本 kubectl version
查看当前上下文 kubectl config current-context
查看所有上下文 kubectl config get-contexts
切换上下文 kubectl config use-context <context>
设置默认命名空间 kubectl config set-context --current --namespace=<namespace>
查看 API 资源 kubectl api-resources
查看 API 版本 kubectl api-versions

生产环境操作前建议确认:

1
2
kubectl config current-context
kubectl config view --minify -o jsonpath='{..namespace}'; echo

3. 资源查询

1
2
3
4
5
6
7
kubectl get nodes -o wide
kubectl get pods -A -o wide
kubectl get all -n <namespace>
kubectl get deployments,statefulsets,daemonsets -n <namespace>
kubectl get ingress,services,endpoints -n <namespace>
kubectl get pvc,pv
kubectl get events -n <namespace> --sort-by='.metadata.creationTimestamp'

持续观察资源:

1
2
kubectl get pods -n <namespace> -w
kubectl get deployment <deployment> -n <namespace> -w

按标签和字段筛选:

1
2
3
4
kubectl get pods -n <namespace> -l app=<name>
kubectl get pods -A --field-selector=status.phase!=Running
kubectl get pods -A --field-selector=spec.nodeName=<node>
kubectl get events -A --field-selector=type=Warning

显示标签:

1
2
kubectl get pods -n <namespace> --show-labels
kubectl get nodes --show-labels

4. 查看资源详情

1
2
3
4
5
kubectl describe pod <pod> -n <namespace>
kubectl describe deployment <deployment> -n <namespace>
kubectl describe node <node>
kubectl get pod <pod> -n <namespace> -o yaml
kubectl get deployment <deployment> -n <namespace> -o json

查看资源字段说明:

1
2
3
kubectl explain deployment
kubectl explain deployment.spec
kubectl explain deployment.spec.strategy

5. 日志排查

1
2
3
4
5
6
kubectl logs <pod> -n <namespace>
kubectl logs <pod> -n <namespace> -c <container>
kubectl logs <pod> -n <namespace> --tail=200
kubectl logs <pod> -n <namespace> --since=30m
kubectl logs <pod> -n <namespace> -f
kubectl logs <pod> -n <namespace> --timestamps

查看崩溃前的容器日志:

1
kubectl logs <pod> -n <namespace> -c <container> --previous

查看带指定标签的所有 Pod 日志:

1
2
kubectl logs -n <namespace> -l app=<name> \
--all-containers=true --prefix --tail=100

6. 进入容器与临时调试

进入容器:

1
2
3
kubectl exec -it <pod> -n <namespace> -- /bin/bash
kubectl exec -it <pod> -n <namespace> -- /bin/sh
kubectl exec -it <pod> -n <namespace> -c <container> -- /bin/sh

执行单条命令:

1
2
kubectl exec <pod> -n <namespace> -- env
kubectl exec <pod> -n <namespace> -- cat /etc/resolv.conf

容器没有排障工具时,使用临时调试容器:

1
2
kubectl debug -it <pod> -n <namespace> \
--image=nicolaka/netshoot --target=<container>

调试节点:

1
kubectl debug node/<node> -it --image=nicolaka/netshoot

7. 应用与修改资源

1
2
3
4
5
kubectl apply -f deployment.yaml
kubectl apply -f ./manifests/
kubectl diff -f deployment.yaml
kubectl create namespace <namespace>
kubectl edit deployment <deployment> -n <namespace>

删除资源前先执行预览:

1
2
kubectl delete -f deployment.yaml --dry-run=client
kubectl delete pod <pod> -n <namespace>

生成 YAML,不直接创建:

1
2
3
4
kubectl create deployment demo \
--image=nginx:1.27 \
--replicas=2 \
--dry-run=client -o yaml

8. 发布与回滚

查看发布状态:

1
2
kubectl rollout status deployment/<deployment> -n <namespace>
kubectl rollout history deployment/<deployment> -n <namespace>

更新镜像:

1
2
3
kubectl set image deployment/<deployment> \
<container>=<image>:<tag> \
-n <namespace>

重启 Deployment:

1
kubectl rollout restart deployment/<deployment> -n <namespace>

暂停和恢复发布:

1
2
kubectl rollout pause deployment/<deployment> -n <namespace>
kubectl rollout resume deployment/<deployment> -n <namespace>

回滚:

1
2
3
4
kubectl rollout undo deployment/<deployment> -n <namespace>
kubectl rollout undo deployment/<deployment> \
--to-revision=<revision> \
-n <namespace>

9. 扩缩容

手动扩缩容:

1
2
3
kubectl scale deployment/<deployment> \
--replicas=5 \
-n <namespace>

创建 HPA:

1
2
3
4
kubectl autoscale deployment/<deployment> \
--min=2 --max=10 \
--cpu-percent=70 \
-n <namespace>

查看 HPA:

1
2
kubectl get hpa -n <namespace>
kubectl describe hpa <hpa> -n <namespace>

HPA CPU/内存指标通常依赖 Metrics Server,同时工作负载应配置 resources.requests

10. 资源使用情况

1
2
3
4
5
kubectl top nodes
kubectl top pods -A
kubectl top pods -n <namespace> --containers
kubectl top pods -A --sort-by=cpu
kubectl top pods -A --sort-by=memory

查看 Pod 资源配置:

1
2
kubectl get pod <pod> -n <namespace> \
-o jsonpath='{range .spec.containers[*]}{.name}{"\nrequests: "}{.resources.requests}{"\nlimits: "}{.resources.limits}{"\n\n"}{end}'

11. Service 与网络排查

1
2
3
4
5
6
kubectl get service <service> -n <namespace> -o wide
kubectl get endpoints <service> -n <namespace>
kubectl get endpointslice -n <namespace> \
-l kubernetes.io/service-name=<service>
kubectl describe ingress <ingress> -n <namespace>
kubectl get networkpolicy -A

端口转发:

1
2
kubectl port-forward -n <namespace> pod/<pod> 8080:80
kubectl port-forward -n <namespace> service/<service> 8080:80

启动临时网络测试 Pod:

1
2
3
4
kubectl run netshoot -it --rm \
--restart=Never \
--image=nicolaka/netshoot \
-n <namespace> -- /bin/bash

常用测试:

1
2
3
4
nslookup <service>
nslookup <service>.<namespace>.svc.cluster.local
curl -v http://<service>:<port>/health
nc -vz <service> <port>

Service 无法访问时依次检查:

  1. Service 的 selector 是否匹配 Pod 标签。
  2. EndpointSlice 是否存在后端地址。
  3. Pod 是否 Ready,监听端口是否正确。
  4. targetPort 是否与容器实际监听端口一致。
  5. NetworkPolicy、CNI、防火墙是否拦截流量。

12. DNS 排查

1
2
3
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl get service -n kube-system kube-dns
kubectl logs -n kube-system -l k8s-app=kube-dns --tail=200

在业务 Pod 内检查:

1
2
cat /etc/resolv.conf
nslookup kubernetes.default.svc.cluster.local

13. ConfigMap 与 Secret

创建 ConfigMap:

1
2
3
4
5
6
7
kubectl create configmap <name> \
--from-file=<file> \
-n <namespace>

kubectl create configmap <name> \
--from-literal=KEY=value \
-n <namespace>

创建 Secret:

1
2
3
4
kubectl create secret generic <name> \
--from-literal=username=<user> \
--from-literal=password=<password> \
-n <namespace>

查看 Secret 键名:

1
2
kubectl get secret <secret> -n <namespace> \
-o jsonpath='{.data}'

解码单个字段:

1
2
kubectl get secret <secret> -n <namespace> \
-o jsonpath='{.data.password}' | base64 -d

Base64 只是编码,不是加密。避免在命令历史、日志和工单中暴露 Secret。

14. 存储排查

1
2
3
4
5
kubectl get pv
kubectl get pvc -A
kubectl get storageclass
kubectl describe pvc <pvc> -n <namespace>
kubectl describe pv <pv>

PVC 一直 Pending 时检查:

  • StorageClass 是否存在且名称正确。
  • 默认 StorageClass 是否设置。
  • CSI Controller 和 Node 插件是否正常。
  • 动态供应器是否有权限创建存储卷。
  • 可用区、访问模式和容量是否满足要求。
  • volumeBindingMode 是否为 WaitForFirstConsumer

查看 PVC 被哪些 Pod 使用:

1
2
3
4
kubectl get pods -n <namespace> -o json \
| jq -r '.items[] |
select(.spec.volumes[]?.persistentVolumeClaim.claimName=="<pvc>") |
.metadata.name'

15. 节点运维

禁止调度新 Pod:

1
kubectl cordon <node>

驱逐工作负载:

1
2
3
kubectl drain <node> \
--ignore-daemonsets \
--delete-emptydir-data

恢复调度:

1
kubectl uncordon <node>

查看节点污点:

1
2
kubectl describe node <node> | grep -A5 Taints
kubectl get nodes -o custom-columns='NAME:.metadata.name,TAINTS:.spec.taints'

添加和删除污点:

1
2
kubectl taint nodes <node> dedicated=infra:NoSchedule
kubectl taint nodes <node> dedicated=infra:NoSchedule-

drain --delete-emptydir-data 会删除 Pod 的 emptyDir 数据,执行前必须确认业务影响。

16. RBAC 权限排查

检查当前用户权限:

1
2
3
kubectl auth can-i get pods -n <namespace>
kubectl auth can-i create deployments -n <namespace>
kubectl auth can-i --list -n <namespace>

模拟 ServiceAccount:

1
2
3
kubectl auth can-i get secrets \
--as=system:serviceaccount:<namespace>:<serviceaccount> \
-n <namespace>

查询关联资源:

1
2
3
kubectl get serviceaccount -A
kubectl get role,rolebinding -A
kubectl get clusterrole,clusterrolebinding

17. JSONPath 与自定义输出

获取所有 Pod 名称:

1
2
kubectl get pods -n <namespace> \
-o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}'

获取 Pod IP 与节点:

1
2
kubectl get pods -n <namespace> \
-o custom-columns='POD:.metadata.name,IP:.status.podIP,NODE:.spec.nodeName'

获取镜像列表:

1
2
3
kubectl get pods -A \
-o jsonpath='{range .items[*].spec.containers[*]}{.image}{"\n"}{end}' \
| sort -u

查看未就绪 Pod:

1
2
kubectl get pods -A \
-o custom-columns='NS:.metadata.namespace,POD:.metadata.name,READY:.status.containerStatuses[*].ready,PHASE:.status.phase'

18. 常见故障快速定位

Pod Pending

1
2
3
4
kubectl describe pod <pod> -n <namespace>
kubectl get events -n <namespace> --sort-by='.metadata.creationTimestamp'
kubectl get nodes
kubectl get pvc -n <namespace>

重点检查:资源不足、污点与容忍、亲和性、PVC、配额和调度约束。

CrashLoopBackOff

1
2
3
kubectl describe pod <pod> -n <namespace>
kubectl logs <pod> -n <namespace> --previous
kubectl get pod <pod> -n <namespace> -o yaml

重点检查:启动命令、配置文件、依赖服务、探针、OOM 和权限。

ImagePullBackOff

1
2
kubectl describe pod <pod> -n <namespace>
kubectl get secret -n <namespace>

重点检查:镜像名称、Tag、镜像仓库连通性、凭据和 imagePullSecrets

OOMKilled

1
2
3
4
kubectl describe pod <pod> -n <namespace>
kubectl top pod <pod> -n <namespace> --containers
kubectl get pod <pod> -n <namespace> \
-o jsonpath='{.status.containerStatuses[*].lastState.terminated.reason}'

重点检查:内存泄漏、峰值内存和 resources.limits.memory

探针失败

1
2
3
kubectl describe pod <pod> -n <namespace>
kubectl logs <pod> -n <namespace>
kubectl get pod <pod> -n <namespace> -o yaml

重点检查:路径、端口、协议、超时、初始延迟和应用启动时间。

Pod 一直 Terminating

1
2
kubectl get pod <pod> -n <namespace> -o yaml
kubectl describe pod <pod> -n <namespace>

重点检查:Finalizer、存储卸载、节点失联以及过长的终止钩子。强制删除应作为最后手段:

1
2
kubectl delete pod <pod> -n <namespace> \
--grace-period=0 --force

19. 高风险操作检查清单

执行删除、回滚、缩容、驱逐或修改生产配置前确认:

  • 当前 kubeconfig 上下文和集群正确。
  • Namespace 和资源名称正确。
  • 已查看变更差异或执行 --dry-run=server
  • 已确认副本数、PDB、存储和业务流量影响。
  • 已准备回滚方案。
  • 不在命令行、截图或日志中暴露 Secret。
  • 谨慎使用 --force--grace-period=0 和批量删除。

Kubernetes运维速查手册
https://blog.t-ao.cn/2026/07/21/Kubernetes运维速查手册/
作者
TAO
发布于
2026年7月21日
许可协议