一、集群基础 & 上下文(1–15)
kubectl version查看客户端 + 集群 apiserver 版本kubectl version --short精简版本输出kubectl cluster-info集群控制平面地址信息kubectl cluster-info dump导出集群完整日志信息kubectl api-versions列出所有 API 版本kubectl api-resources列出全部 k8s 资源、简称、api 组kubectl api-resources --namespaced=false查看集群级资源(ClusterRole/Node)kubectl config view查看 kubeconfig 配置文件kubectl config current-context当前使用集群上下文kubectl config get-contexts列出所有集群上下文kubectl config use-context dev切换到 dev 集群kubectl config rename-context old new重命名上下文kubectl config delete-context test删除无用集群上下文kubectl config set-context --current --namespace=biz设置默认命名空间kubectl config unset users.xxx删除 kubeconfig 中用户配置
二、节点管理(16–30)
kubectl get nodes查看所有节点基础信息kubectl get nodes -o wide节点 IP、系统版本、内核、容器运行时kubectl describe node node-01节点详情(资源、污点、事件、Pod 分配)kubectl get node node-01 -o yaml导出节点完整 yamlkubectl top nodes节点 CPU 内存实时占用kubectl cordon node-01封锁节点,禁止新 Pod 调度kubectl uncordon node-01解除节点封锁,恢复调度kubectl drain node-01 --ignore-daemonsets驱逐节点所有 Pod(维护前)kubectl drain node-01 --delete-local-data同时删除节点本地 emptyDir 数据kubectl delete node node-01删除集群节点kubectl label nodes node-01 zone=sh给节点打标签kubectl label nodes node-01 zone-删除节点 zone 标签kubectl annotate node node-01 env=prod增加节点注解kubectl annotate node node-01 env-删除节点注解kubectl get nodes --show-labels查看所有节点标签
三、命名空间 Namespace(31–40)
kubectl get ns列出所有命名空间kubectl create ns business创建命名空间 businesskubectl delete ns business删除命名空间(连带内部所有资源)kubectl get all -n business查看该 ns 下所有核心资源kubectl get pods -A全命名空间 Pod 列表kubectl get events -n business查看命名空间事件kubectl get events -A --sort-by=.metadata.creationTimestamp全局按时间排序事件kubectl describe ns business命名空间配额、状态详情kubectl get ns business -o yaml导出 ns 配置kubectl delete all --all -n business清空命名空间所有资源(不删 ns 本身)
四、Pod 全生命周期操作(41–70)
kubectl get pods当前 ns 所有 Podkubectl get pods -o widePod IP、所属节点、镜像、就绪状态kubectl get pods -l app=u-chat按标签过滤 Podkubectl get pods --field-selector status.phase=Running只看运行中 Podkubectl get pods --sort-by=.status.restartCount按重启次数排序kubectl get pod pod-name -o yaml导出 Pod 完整定义kubectl describe pod pod-namePod 排错核心命令(事件、资源、挂载、探针)kubectl create -f pod-demo.yaml基于 yaml 创建 Podkubectl apply -f pod-demo.yaml创建 / 更新 Pod(推荐)kubectl delete pod pod-name删除单个 Podkubectl delete pods -l app=u-chat按标签批量删除 Podkubectl delete pod pod-name --grace-period=0 --force强制立刻删除僵死 Podkubectl exec -it pod-name -- /bin/bash进入 Pod 终端kubectl exec -it pod-name -c sidecar -- sh多容器 Pod 指定 sidecar 进入kubectl logs pod-name查看 Pod 当前容器日志kubectl logs -f pod-name实时滚动跟踪日志kubectl logs pod-name -c sidecar查看指定容器日志kubectl logs pod-name --previous查看上一次崩溃退出容器日志(排错必备)kubectl logs pod-name --tail=100只输出最后 100 行日志kubectl logs pod-name --since=30m查看近 30 分钟日志kubectl logs pod-name --timestamps日志附带时间戳kubectl port-forward pod/pod-name 8080:80本地端口转发 Podkubectl port-forward pod/pod-name 0:80随机本地端口映射kubectl cp pod-name:/data/log ./local-log从 Pod 拷贝文件到本地kubectl cp ./local.conf pod-name:/etc/config本地文件上传至 Podkubectl top pod查看所有 Pod CPU 内存占用kubectl top pod pod-name单个 Pod 资源监控kubectl label pod pod-name group=relayPod 打标签kubectl label pod pod-name group-删除 Pod 标签kubectl get pods -w实时监听 Pod 创建 / 删除 / 重启变化
五、Deployment 无状态应用(71–95)
kubectl get deploy列出 Deploymentkubectl get deploy -o wide副本数、镜像、更新策略、标签kubectl describe deploy uchat-relayDeployment 事件、滚动更新详情kubectl create deploy nginx --image=nginx:alpine快速创建 deploykubectl apply -f deploy-uchat.yaml加载 yaml 创建 / 更新 deploykubectl get deploy uchat-relay -o yaml > deploy-bak.yaml导出备份 yamlkubectl edit deploy uchat-relay在线直接编辑 deploy 配置kubectl set image deploy/uchat-relay uchat=uchat:v2更新容器镜像kubectl rollout status deploy/uchat-relay查看滚动更新进度kubectl rollout history deploy/uchat-relay查看所有发布修订版本kubectl rollout undo deploy/uchat-relay回滚到上一个版本kubectl rollout undo deploy/uchat-relay --to-revision=2指定版本回滚kubectl rollout restart deploy/uchat-relay滚动重启所有 Podkubectl scale deploy uchat-relay --replicas=5手动扩容副本至 5kubectl scale deploy uchat-relay --replicas=1缩容副本kubectl autoscale deploy uchat-relay --min=2 --max=10 --cpu-percent=70创建 HPA 自动扩缩容kubectl get hpa查看水平自动扩缩容策略kubectl delete hpa uchat-relay删除 HPA 规则kubectl delete deploy uchat-relay删除 Deploymentkubectl get pods -l app=uchat查看该 deploy 下所有 Podkubectl logs deploy/uchat-relay -f直接查看 deploy 全部 Pod 日志kubectl logs deploy/uchat-relay --all-pods=true输出所有副本日志kubectl rollout pause deploy/uchat-relay暂停滚动更新kubectl rollout resume deploy/uchat-relay恢复滚动更新kubectl get deploy -o name只输出资源名称
六、StatefulSet 有状态应用(96–110)
kubectl get sts列出 StatefulSetkubectl get sts mysql-sts -o wide副本、serviceName、镜像信息kubectl describe sts mysql-sts有序启停、PVC 绑定、事件kubectl apply -f sts-mysql.yaml创建有状态应用kubectl scale sts mysql-sts --replicas=3扩缩 StatefulSet 副本kubectl rollout restart sts mysql-sts滚动重启 stskubectl rollout undo sts mysql-stssts 回滚版本kubectl get pvc -l app=mysql查看 sts 自动创建的 PVCkubectl delete sts mysql-sts删除 sts(保留 PVC)kubectl delete sts mysql-sts --cascade=foreground同步删除 Podkubectl get pod mysql-sts-0查看固定序号 sts Podkubectl exec -it mysql-sts-0 -- mysql -uroot -p进入 0 号实例kubectl logs sts/mysql-sts -f直接查看 sts 日志kubectl get sts mysql-sts -o yaml导出 sts 完整配置kubectl label sts mysql-sts env=prod给 statefulset 打标签
七、DaemonSet 节点级守护进程(111–120)
kubectl get ds查看 DaemonSet(calico/cilium/ 监控采集器)kubectl get ds calico-node -n kube-system -o widekubectl describe ds calico-node -n kube-system节点调度匹配规则kubectl rollout restart ds calico-node -n kube-system滚动重启 dskubectl rollout undo ds calico-node -n kube-systemds 回滚镜像kubectl set image ds/calico-node calico=calico:v3.26 -n kube-systemkubectl delete ds cilium-agent -n kube-system删除守护进程kubectl get pods -n kube-system -l k8s-app=calico-node查看 ds 所有 Podkubectl logs ds/calico-node -n kube-system -f实时查看网络插件日志kubectl apply -f ds-spider-agent.yaml部署 spiderpool-agent
八、Service & Endpoint 服务暴露(121–135)
kubectl get svc列出所有 Servicekubectl get svc -o wide类型、ClusterIP、端口、标签选择器kubectl describe svc uchat-svc查看后端 Endpoint、端口、协议kubectl get endpoints uchat-svc查看 service 关联的 Pod IP 列表kubectl create svc clusterip uchat-svc --tcp=443:443创建 ClusterIP 服务kubectl create svc nodeport uchat-svc --tcp=8080:8080创建 NodePort 服务kubectl create svc loadbalancer uchat-svc --tcp=8443:8443创建 LB 服务kubectl apply -f svc-uchat.yamlyaml 创建 servicekubectl port-forward svc/uchat-svc 8080:443本地转发 servicekubectl delete svc uchat-svc删除服务kubectl get svc uchat-svc -o yaml导出 service 配置kubectl label svc uchat-svc zone=shservice 打标签kubectl get ep -A全命名空间所有 endpointkubectl get svc --field-selector spec.type=NodePort只查看 NodePort 服务kubectl edit svc uchat-svc在线修改 service 端口 / 类型
九、ConfigMap / Secret 配置管理(136–150)
ConfigMap
kubectl get cm查看所有 ConfigMapkubectl create cm app-conf --from-literal=LOG_LEVEL=info键值对创建 cmkubectl create cm app-conf --from-file=app.ini单个文件生成 cmkubectl create cm app-conf --from-file=./config/整个目录生成 cmkubectl describe cm app-conf查看 cm 内全部配置内容kubectl get cm app-conf -o yaml导出配置明文kubectl delete cm app-conf删除配置
Secret
kubectl get secret查看密钥资源kubectl create secret generic db-secret --from-literal=user=root --from-literal=pass=123456kubectl create secret docker-registry harbor-secret --docker-server=harbor.xxx.com --docker-user=admin --docker-password=xxx私有镜像仓库密钥kubectl describe secret db-secret查看 secret 基础信息kubectl get secret db-secret -o yaml查看 base64 编码密钥kubectl delete secret db-secret删除密钥kubectl apply -f cm-secret-all.yaml一次性加载 cm+secretkubectl get cm,secret -n biz同时查看两类配置资源
十、存储 PV/PVC(151–162)
kubectl get pv集群所有持久卷kubectl get pv -o wide容量、访问模式、绑定状态、存储类kubectl describe pv pv-xxxPV 详情、回收策略、事件kubectl get pvc -n biz命名空间下 PVCkubectl describe pvc mysql-pvc -n bizPVC 绑定 PV 状态kubectl get storageclasses查看存储供给类 SCkubectl get sc default -o yaml查看默认存储类配置kubectl apply -f pvc-mysql.yaml创建 PVCkubectl delete pvc mysql-pvc -n biz删除 PVCkubectl get pv --sort-by=.spec.capacity.storagePV 按容量排序kubectl get pvc -l app=mysql筛选 sts 绑定 PVCkubectl edit pvc mysql-pvc -n biz修改 PVC 容量
十一、RBAC 权限 SA/ClusterRole/ClusterRoleBinding(163–178)
ServiceAccount
kubectl get sa -A全命名空间 ServiceAccountkubectl create sa spider-agent-sa -n kube-system创建服务账号kubectl describe sa spider-agent-sa -n kube-system查看绑定 token secretkubectl get sa spider-agent-sa -o yaml导出 SA 定义kubectl delete sa spider-agent-sa -n kube-system删除 SA
ClusterRole
kubectl get clusterroles全局权限角色kubectl get clusterrole calico-full -o yaml查看权限 ruleskubectl describe clusterrole spiderpool-ippool-adminkubectl create -f cr-ippool.yaml创建操作 IPPool 的 ClusterRole
ClusterRoleBinding
kubectl get clusterrolebindings全局权限绑定kubectl describe clusterrolebinding spiderpool-bind查看绑定 SA 主体kubectl get clusterrolebinding spiderpool-bind -o yamlkubectl delete clusterrolebinding spiderpool-bind删除权限绑定kubectl get clusterroles | grep -E calico|cilium|ippool过滤网络相关权限kubectl apply -f rbac-full.yaml一次性创建 SA+CR+CRBkubectl auth can-i get ippools.projectcalico.org --as=system:serviceaccount:kube-system:calico-node校验 SA 是否拥有操作 IPPool 权限
十二、网络插件 CRD(Calico/Cilium/Spiderpool IPPool)(179–188)
kubectl get ippools.projectcalico.org查看 Calico 地址池kubectl get ippool.projectcalico.org default-pool -o yaml导出 Calico IPPoolkubectl delete ippool.projectcalico.org default-pool删除地址池kubectl get ciliumippools.cilium.ioCilium IP 池kubectl get spiderippools.spiderpool.spidernet.ioSpiderpool IP 池kubectl describe spiderippools underlay-pool查看 Spider 物理网段池kubectl get networkpolicies.networking.k8s.io查看网络策略kubectl apply -f spider-ippool.yaml创建 Underlay IP 地址池kubectl get networkattachmentdefinitions.k8s.cni.cncf.io -n kube-systemMultus 多网卡网络定义kubectl describe networkattachmentdefinitions macvlan-underlay -n kube-system
十三、批量操作、导出、过滤、高级查询(189–200)
kubectl get all -n biz一次性查看 ns 下 pod/deploy/svc/cmkubectl get pods -o jsonpath='{.items[*].metadata.name}'jsonpath 提取 Pod 名称kubectl get pods -o custom-columns=NAME:.metadata.name,IP:.status.podIP,NODE:.spec.nodeName自定义输出列kubectl get deploy -o json > deploy-all.json导出所有 deploy 为 jsonkubectl apply -f ./yaml/加载目录下所有 yaml 资源kubectl delete -f all-resource.yaml删除 yaml 中所有定义资源kubectl replace -f deploy.yaml --force强制重建资源kubectl diff -f deploy-new.yaml对比本地 yaml 与集群线上差异kubectl get events --field-selector involvedObject.kind=Pod只查看 Pod 相关事件kubectl label all pods env=prod -n biz --overwrite批量覆盖所有 pod 标签kubectl get pods -A | grep CrashLoopBackOff全局筛选崩溃 Podkubectl help查看 kubectl 全部子命令帮助
配套使用小技巧
- 简写别名(日常提速)
bash
运行
alias k='kubectl'
alias kg='kubectl get'
alias kd='kubectl describe'
alias ka='kubectl apply -f'
alias kl='kubectl logs -f'
alias ke='kubectl exec -it'
- 排错核心四连:
get pod→describe pod→logs -p→get events