目录
[二、Pod 排错核心命令(生产故障必备)](#二、Pod 排错核心命令(生产故障必备))
[五、ConfigMap / Secret 配置管理](#五、ConfigMap / Secret 配置管理)
[七、RBAC 权限管理命令](#七、RBAC 权限管理命令)
[八、容器运行时 containerd 常用命令](#八、容器运行时 containerd 常用命令)
[九、命令简写 & 别名技巧(提高效率)](#九、命令简写 & 别名技巧(提高效率))
一、基础资源查看命令
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 查看所有命名空间 kubectl get ns # 查看全部Pod(所有命名空间) kubectl get pods -A # 指定命名空间查看Pod,详细信息(节点IP、宿主机) kubectl get pods -n xxx -o wide # 查看各类核心资源 #查看deployment部署 kubectl get deploy -n xxx #简写 kubectl get deploy -n xxx #查看service服务 kubectl get svc -n xxx #查看daemonset守护集 kubectl get daemonset -n xxx #简写 kubectl get ds -n xxx #查看statefulset有状态应用 kubectl get statefulset -n xxx #简写 kubectl get sts -n xxx #查看configmap配置 kubectl get cm -n xxx #查看secret密钥 kubectl get secret -n xxx #一次性查看以上全部资源 kubectl get deploy,svc,ds,sts,cm,secret -n xxx # 查看Service后端就绪Pod端点(排错必用) kubectl get endpoints svc-name -n xxx # 查看集群事件(按时间排序,排错第一步) kubectl get events -n xxx --sort=.lastTimestamp kubectl get events -A --sort=.lastTimestamp # 查看集群节点状态 kubectl get nodes -o wide # 查看K8s API资源及字段 kubectl api-resources kubectl explain deployment.spec # yaml格式输出完整资源 kubectl get pod pod-name -n xxx -o yaml # json格式输出 kubectl get pod pod-name -n xxx -o json # jsonpath提取字段,示例获取PodIP kubectl get pods -n xxx -o jsonpath='{.items\*.status.podIP}' # 自定义列输出 kubectl get pods -n xxx -o custom-columns="POD:.metadata.name,NODE:.spec.nodeName" # 只打印资源名称 kubectl get pods -n xxx -o name # 追加:资源使用率(依赖metrics‑server) kubectl top pods -n xxx kubectl top pods -n xxx --sort-by=cpu kubectl top nodes kubectl top nodes --sort-by=memory # 追加:标签查看、筛选 # 显示资源标签 kubectl get pods -n xxx --show-labels # 根据标签筛选Pod kubectl get pods -n xxx -l app=demo kubectl get pods -n xxx -l app=demo,env=prod # 根据字段筛选,查找调度到node01的Pod kubectl get pods -n xxx --field-selector spec.nodeName=node01 # 筛选异常状态Pod kubectl get pods -n xxx --field-selector status.phase!=Running # 追加集群基础信息 kubectl cluster-info |
二、Pod 排错核心命令(生产故障必备)
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 查看Pod详细事件、报错、启动原因 kubectl describe pod pod-name -n xxx # 查看当前容器日志 kubectl logs pod-name -n xxx # 查看上一次崩溃日志(CrashLoopBackOff 专属) kubectl logs pod-name --previous -n xxx # 实时跟踪日志 kubectl logs -f pod-name -n xxx # 进入Pod容器终端 kubectl exec -it pod-name -n xxx -- bash kubectl exec -it pod-name -n xxx -- sh # 临时网络调试Pod(排查网络、DNS、端口连通性) kubectl run netshoot --rm -it --image=nicolaka/netshoot -n xxx -- bash #本地端口转发Pod kubectl port-forward pod/pod-name -n xxx 8080:80 #本地端口转发Service kubectl port-forward svc/demo-svc -n xxx 8080:80 |
三、应用发布、更新、回滚、扩缩容
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 副本扩缩容 kubectl scale deployment app --replicas=3 -n xxx # 滚动更新镜像 kubectl set image deployment/app 容器名=镜像地址:版本 -n xxx # 查看发布历史版本 kubectl rollout history deployment app -n xxx # 回滚至上一版本 kubectl rollout undo deployment app -n xxx # 回滚到指定版本 kubectl rollout undo deployment app --to-revision=2 -n xxx # 查看滚动更新实时状态 kubectl rollout status deployment app -n xxx # =追加patch修改资源,无需编辑yaml kubectl patch deployment app -n xxx -p '{"spec":{"replicas":5}}' |
四、YAML资源管理(创建/编辑/导出/删除)
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 应用YAML创建/更新资源 kubectl apply -f xxx.yaml # 批量应用文件夹内所有YAML kubectl apply -f ./yaml/ # 导出现有资源YAML(用于备份、复用) kubectl get deploy app -n xxx -o yaml > deploy.yaml # 在线直接编辑资源 kubectl edit deployment app -n xxx # 删除资源 kubectl delete -f xxx.yaml kubectl delete pod pod-name -n xxx # 强制删除卡死Terminating状态Pod kubectl delete pod pod-name -n xxx --grace-period=0 --force # 删除命名空间下全部Pod(不删deployment控制器) kubectl delete pods --all -n xxx # 删除命名空间下全部资源(谨慎操作) kubectl delete all --all -n xxx # 追加dryrun,预生成yaml不实际创建资源 kubectl run testpod --image=nginx -n xxx --dry-run=client -o yaml > pod.yaml |
五、ConfigMap / Secret 配置管理
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 文件方式创建ConfigMap kubectl create configmap app-config -n xxx --from-file=app.conf # 创建私有仓库镜像拉取Secret kubectl create secret docker-registry regsecret -n xxx \ --docker-server=仓库地址 \ --docker-username=用户名 \ --docker-password=密码 # 查看配置资源详情 kubectl describe cm app-config -n xxx kubectl describe secret regsecret -n xxx |
六、节点运维、调度、污点管理
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 禁止新Pod调度到当前节点 kubectl cordon node01 # 驱逐节点所有业务Pod,准备机器维护(忽略DaemonSet) kubectl drain node01 --ignore-daemonsets # 维护完成,恢复节点调度 kubectl uncordon node01 # 给节点打污点(禁止调度) kubectl taint nodes node01 key=value:NoSchedule # 删除节点污点 kubectl taint nodes node01 key- # 追加查看节点污点 kubectl describe node node01 | grep Taints # 追加节点标签操作 kubectl label nodes node01 env=prod kubectl label nodes node01 env- |
七、RBAC 权限管理命令
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| # 创建命名空间只读Role kubectl create role pod-read --verb=get,list --resource=pods -n xxx # 绑定用户与Role(命名空间级授权) kubectl create rolebinding test-rb --role=pod-read --user=test -n xxx # 创建集群级只读权限 kubectl create clusterrole cluster-pod-read --verb=get,list --resource=pods # 全局绑定集群权限 kubectl create clusterrolebinding crb-test --clusterrole=cluster-pod-read --user=test # 追加RBAC查询、权限校验 kubectl get roles -n xxx kubectl get rolebindings -n xxx kubectl get clusterroles kubectl get clusterrolebindings # 校验当前账号是否具备操作权限 kubectl auth can-i create pods -n xxx |
八、容器运行时 containerd 常用命令
|----------------------------------------------------------------------|
| # 查看节点运行容器 crictl ps # 查看本地镜像 crictl images # 清理无用镜像、缓存 crictl prune |
九、命令简写 & 别名技巧(提高效率)
常用简写对照表
- pods → po
- deployment → deploy
- service → svc
- namespace → ns
- configmap → cm
永久配置别名(k代替kubectl)
|----------------------------------------------------------------|
| echo "alias k='kubectl'" >> /etc/profile source /etc/profile |