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
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
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
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