2026-05-20-kubectl 命令完整手册
kubectl 命令完整手册
一、基础查看命令
1. get - 列出资源
最常用的命令,列出资源。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 kubectl get <资源类型> [名称] [选项] kubectl get pods kubectl get pods -n kube-system kubectl get pods -A kubectl get pods --all-namespaces kubectl get nodes kubectl get nodes -o wide kubectl get nodes --show-labels kubectl get deployments kubectl get services kubectl get configmaps kubectl get secrets kubectl get ingress kubectl get pv kubectl get pvc kubectl get ns kubectl get sa kubectl get events kubectl get jobs kubectl get cronjobs kubectl get all kubectl get all -n monitoring kubectl get pods -o wide kubectl get pods --show-labels kubectl get pods -l app=nginx kubectl get pods --field-selector status.phase=Running kubectl get pods -A --field-selector spec.nodeName=node01 kubectl get pod nginx -o yaml kubectl get pod nginx -o json kubectl get pod nginx -o jsonpath='{.status.podIP}' kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase kubectl get pods --sort-by=.metadata.creationTimestamp kubectl get pods --sort-by=.status.startTime kubectl get nodes --sort-by=.metadata.name
2. describe - 查看资源详情
1 2 3 4 5 6 7 8 9 10 11 12 13 14 kubectl describe <资源类型> <名称> [-n 命名空间] kubectl describe pod nginx-xxx -n default kubectl describe node master01 kubectl describe deployment grafana -n monitoring kubectl describe svc kubernetes kubectl describe pod grafana-xxx -n monitoring | tail -30 kubectl get pods -n kube-system -o name | xargs -I {} kubectl describe {} -n kube-system
何时用:
Pod 启动失败,查原因
看资源的完整配置和运行状态
查 Events(最下面)定位问题
3. logs - 查看日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 kubectl logs <pod名称> [选项] [-n 命名空间] kubectl logs nginx-xxx kubectl logs nginx-xxx -c nginx kubectl logs nginx-xxx --previous kubectl logs nginx-xxx -f kubectl logs nginx-xxx --tail =100 kubectl logs nginx-xxx --since=1h kubectl logs nginx-xxx --since-time=2026-04-23T10:00:00Z kubectl logs grafana-xxx --all-containers --tail =50 kubectl logs grafana-xxx -c grafana-sc-datasources kubectl logs -l app=nginx -n default kubectl logs -l app=nginx --tail =100 --prefix kubectl logs nginx-xxx --previous
4. top - 查看资源使用
需要先装 metrics-server。
1 2 3 4 5 6 7 8 9 10 11 12 kubectl top nodes kubectl top nodes --sort-by=cpu kubectl top nodes --sort-by=memory kubectl top pods kubectl top pods -A kubectl top pods -n monitoring kubectl top pods --containers kubectl top pods --sort-by=memory kubectl top pods --sort-by=cpu -A | head
二、创建和删除资源
5. apply -
声明式创建/更新(推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 kubectl apply -f <文件或URL> kubectl apply -f nginx.yaml kubectl apply -f https://raw.githubusercontent.com/xxx/yaml kubectl apply -f ./manifests/ kubectl apply -f ./manifests/ --recursive kubectl apply -f a.yaml -f b.yaml kubectl apply -f nginx.yaml --dry-run=client kubectl apply -f nginx.yaml --dry-run=servercat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - name: nginx image: nginx:alpine EOF
6. create - 命令式创建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 kubectl create <资源类型> <名称> [选项] kubectl create namespace monitoring kubectl create deployment nginx --image=nginx:alpine kubectl create deployment nginx --image=nginx:alpine --replicas=3 kubectl create service clusterip my-svc --tcp=80:8080 kubectl create configmap my-config --from-literal=key1=val1 kubectl create configmap my-config --from-file=config.txt kubectl create secret generic my-secret --from-literal=password=123456 kubectl create sa admin-user -n default kubectl create token admin-user -n default --duration=8760h kubectl create -f nginx.yaml kubectl create deployment nginx --image=nginx -o yaml --dry-run=client
7. run - 快速跑 Pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 kubectl run <名称> --image=<镜像> [选项] kubectl run nginx --image=nginx:alpine kubectl run test --image=busybox:1.28 --rm -it --restart=Never -- sh kubectl run nginx --image=nginx --port=80 kubectl run debug --image=nicolaka/netshoot --rm -it --restart=Never -- sh
8. delete - 删除资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 kubectl delete <资源类型> <名称> [选项] kubectl delete pod nginx-xxx kubectl delete pod nginx-xxx -n default kubectl delete deployment nginx kubectl delete svc my-svc kubectl delete namespace my-ns kubectl delete -f nginx.yaml kubectl delete pods -l app=nginx kubectl delete pods --all -n default kubectl delete all --all -n my-ns kubectl delete pod nginx-xxx --grace-period=0 --force kubectl delete pods,svc -l app=nginx
三、更新和修改资源
9. set -
修改资源的特定字段(快捷)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 kubectl set image deployment/nginx nginx=nginx:1.25 kubectl set image deployment/grafana -n monitoring grafana=docker.m.daocloud.io/grafana/grafana:10.4.2 kubectl set resources deployment grafana -n monitoring \ --requests=memory=150Mi,cpu=50m \ --limits=memory=400Mi,cpu=300m kubectl set resources deployment nginx --limits=memory=500Mi kubectl set resources deployment nginx --requests=cpu=100m kubectl set env deployment/nginx LOG_LEVEL=debug kubectl set env deployment/nginx LOG_LEVEL- kubectl set selector svc/nginx app=nginx-v2 kubectl set serviceaccount deployment/nginx my-sa
10. edit - 在线编辑(打开编辑器)
1 2 3 4 5 6 7 8 9 10 11 kubectl edit deployment nginx kubectl edit pod nginx-xxx kubectl edit svc my-svc kubectl edit configmap my-config -n monitoring kubectl edit daemonset calico-node -n kube-system KUBE_EDITOR="vim" kubectl edit deployment nginx kubectl edit deployment nginx -o json
11. patch - 精确修改(最灵活)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 kubectl patch deployment nginx -p '{"spec":{"replicas":3}}' kubectl patch deployment nginx -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.25"}]}}}}' kubectl patch node node01 -p '{"spec":{"taints":[{"key":"dedicated","value":"db","effect":"NoSchedule"}]}}' kubectl patch deployment grafana -n monitoring --type =json -p='[ {"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--debug"} ]' kubectl patch namespace calico-apiserver -p '{"metadata":{"finalizers":null}}' --type =merge kubectl patch -f nginx.yaml -p "$(cat patch.yaml) "
12. replace - 替换整个资源
1 2 3 4 5 6 7 8 kubectl replace -f nginx.yaml kubectl replace -f nginx.yaml --force kubectl get pod nginx -o yaml | sed 's/image: nginx:alpine/image: nginx:1.25/' | kubectl replace -f -
区别:
apply:声明式,保留未声明字段
replace:完全替换,未声明字段会丢失
13. scale - 扩缩容
1 2 3 4 5 6 7 8 9 10 11 kubectl scale <资源类型>/<名称> --replicas=<数量> kubectl scale deployment nginx --replicas=3 kubectl scale deployment nginx --replicas=0 kubectl scale statefulset mysql --replicas=5 kubectl scale --replicas=2 -f nginx.yaml kubectl scale deployment nginx --current-replicas=2 --replicas=3
14. rollout - 滚动管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 kubectl rollout history deployment nginx kubectl rollout history deployment nginx --revision=3 kubectl rollout undo deployment nginx kubectl rollout undo deployment nginx --to-revision=2 kubectl rollout restart deployment nginx kubectl rollout restart deployment grafana -n monitoring kubectl rollout pause deployment nginx kubectl rollout resume deployment nginx kubectl rollout status deployment nginx
四、标签和污点
15. label - 管理标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 kubectl label nodes node01 workload=apps kubectl label pod nginx env =prod kubectl label nodes node01 workload=db --overwrite kubectl label nodes node01 workload- kubectl label nodes node01 node02 tier=frontend kubectl label nodes --all team=devops kubectl label pods -l app=nginx env =prod
16. annotate - 管理注解
用法和 label 类似,注解是给工具/系统用的。
1 2 3 kubectl annotate pod nginx description="production web server" kubectl annotate pod nginx description- kubectl annotate namespace monitoring scheduler.alpha.kubernetes.io/node-selector="workload=apps"
17. taint - 管理污点
1 2 3 4 5 6 7 8 9 kubectl taint nodes node01 dedicated=database:NoSchedule kubectl taint nodes node01 dedicated- kubectl taint nodes node01 dedicated:NoSchedule- kubectl taint nodes --all node-role.kubernetes.io/control-plane-
五、调试和交互
18. exec - 进入容器执行命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 kubectl exec nginx -- ls /etc/nginx kubectl exec nginx -- env kubectl exec nginx -- cat /etc/hosts kubectl exec -it nginx -- bash kubectl exec -it nginx -- sh kubectl exec -it grafana-xxx -c grafana -- bash kubectl exec -it nginx -n default -- bash
19. cp -
在容器和本地间复制文件
1 2 3 4 5 6 7 8 9 kubectl cp default/nginx:/etc/nginx/nginx.conf ./nginx.conf kubectl cp nginx:/tmp/log.txt ./log.txt kubectl cp ./config.yaml default/nginx:/tmp/config.yaml kubectl cp nginx:/tmp/file.txt ./file.txt -c nginx
20. port-forward - 端口转发
把集群内服务临时映射到本地
1 2 3 4 5 6 7 8 9 10 11 12 13 14 kubectl port-forward pod/nginx 8080:80 kubectl port-forward svc/nginx 8080:80 kubectl port-forward deployment/grafana -n monitoring 3000:3000 kubectl port-forward --address 0.0.0.0 svc/grafana 3000:80 -n monitoring kubectl port-forward svc/grafana 3000:80 -n monitoring &
21. proxy - 代理到 API server
1 2 3 4 5 6 7 8 9 kubectl proxy kubectl proxy --port=8080 curl http://localhost:8001/api/v1/namespaces curl http://localhost:8001/healthz
22. debug - 调试工具(1.25+)
1 2 3 4 5 6 7 8 kubectl debug -it nginx --image=busybox:1.28 --target=nginx kubectl debug nginx -it --copy-to=nginx-debug --container=nginx kubectl debug node/node01 -it --image=ubuntu
六、集群和节点管理
23. cluster-info - 集群信息
1 2 3 4 5 6 7 kubectl cluster-info kubectl cluster-info dump kubectl version kubectl api-versions kubectl api-resources kubectl api-resources --namespaced=true kubectl api-resources -o wide
24. cordon / uncordon -
节点暂停调度
1 2 3 4 5 kubectl cordon node01 kubectl uncordon node01
25. drain - 驱逐 Pod(节点维护)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 kubectl drain node01 kubectl drain node01 --ignore-daemonsets kubectl drain node01 --ignore-daemonsets --force kubectl drain node01 --ignore-daemonsets --delete-emptydir-data kubectl drain node01 --timeout =300s kubectl cordon node01 kubectl drain node01 --ignore-daemonsets --delete-emptydir-data kubectl uncordon node01
七、配置和上下文
26. config - kubeconfig 管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 kubectl config view kubectl config view --minify kubectl config view --raw kubectl config current-context kubectl config get-contexts kubectl config use-context my-cluster kubectl config set-context --current --namespace=monitoring kubectl config set-cluster my-cluster --server=https://xxx:6443 kubectl config set-credentials user1 --token=xxx kubectl --kubeconfig=/path/to/kubeconfig get nodesexport KUBECONFIG=/path/to/kubeconfig kubectl get nodes
八、高级技巧
27. 格式化输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 -o yaml -o json -o wide -o name -o jsonpath='{...}' -o jsonpath-as-json='{...}' -o go-template='...' -o custom-columns=<cols> kubectl get pods -o jsonpath='{.items[*].metadata.name}' kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}' kubectl get pods -o custom-columns=\ NAME:.metadata.name,\ STATUS:.status.phase,\ NODE:.spec.nodeName,\ IP:.status.podIP kubectl get pod nginx -o jsonpath='{.status.podIP}' kubectl get deploy nginx -o jsonpath='{.spec.template.spec.containers[0].image}' kubectl get node node01 -o jsonpath='{.status.capacity}'
28. 标签选择器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 kubectl get pods -l app=nginx kubectl get pods -l 'app=nginx,env=prod' kubectl get pods -l 'app=nginx,env!=prod' kubectl get pods -l 'env in (dev,prod)' kubectl get pods -l 'env notin (dev,prod)' kubectl get pods -l app kubectl get pods -l '!app' kubectl get pods --field-selector status.phase=Running kubectl get pods --field-selector spec.nodeName=node01 kubectl get pods --field-selector status.phase!=Running,metadata.namespace=default
29. wait - 等待条件
1 2 3 4 5 6 7 8 9 kubectl wait --for =condition=Ready pod/nginx --timeout =60s kubectl wait --for =condition=ready pod -l app=nginx --timeout =300s kubectl wait --for =delete pod/nginx --timeout =60s kubectl wait --for =condition=available deployment/nginx --timeout =300s
30. explain - 查资源字段文档
不懂 YAML 某字段怎么写?看文档
1 2 3 4 5 kubectl explain pod kubectl explain pod.spec kubectl explain pod.spec.containers kubectl explain pod.spec.containers.resources kubectl explain deployment.spec --recursive
九、常用实用组合
批量操作
1 2 3 4 5 6 7 8 9 10 11 12 13 for d in $(kubectl get deploy -n monitoring -o name); do kubectl rollout restart $d -n monitoringdone kubectl label pods --all env =prod -n my-ns kubectl get pods -A | grep Evicted | awk '{print "kubectl delete pod " $2 " -n " $1}' | sh kubectl top pods -A --sort-by=memory | head
排查问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 kubectl get pods -A | grep -v "Running\|Completed" kubectl get pods -A -o wide --field-selector spec.nodeName=node01 POD=grafana-xxx NS=monitoring kubectl get pod $POD -n $NS -o yaml kubectl describe pod $POD -n $NS kubectl logs $POD -n $NS --all-containers kubectl top pod $POD -n $NS diff <(kubectl get deploy nginx -o yaml) <(kubectl get deploy nginx-v2 -o yaml)
生成 YAML 模板
1 2 3 4 5 6 7 8 9 10 11 kubectl create deployment nginx --image=nginx -o yaml --dry-run=client > nginx.yaml kubectl expose deployment nginx --port=80 -o yaml --dry-run=client > nginx-svc.yaml kubectl create secret generic my-secret --from-literal=key=value -o yaml --dry-run=client kubectl create configmap my-cm --from-literal=key=value -o yaml --dry-run=client
十、修改资源的四种方式对比
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 kubectl set resources deployment grafana -n monitoring --limits=memory=400Mi kubectl edit deployment grafana -n monitoring kubectl patch deployment grafana -n monitoring --type =json -p='[ {"op": "replace", "path": "/spec/template/spec/containers/0/resources/limits/memory", "value": "400Mi"} ]' kubectl apply -f grafana.yaml helm upgrade grafana grafana/grafana -f grafana-values.yaml -n monitoring
方式
优点
缺点
适用
set
简单直接
只能改预定义字段
快速修改
edit
交互式,能看到全文
改错容易出问题
临时调整
patch
精确,能脚本化
语法稍复杂
自动化
apply
声明式,可追溯
要维护 YAML 文件
GitOps
helm
最规范
要改 values 文件
Helm 安装的组件
十一、常用别名和快捷配置
加到 ~/.bashrc 或 ~/.zshrc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 alias k=kubectlalias kgp='kubectl get pods' alias kgpa='kubectl get pods -A' alias kgpw='kubectl get pods -o wide' alias kgn='kubectl get nodes' alias kgs='kubectl get svc' alias kgd='kubectl get deploy' alias kgns='kubectl get ns' alias kd='kubectl describe' alias kdp='kubectl describe pod' alias kl='kubectl logs' alias klf='kubectl logs -f' alias ke='kubectl exec -it' alias kaf='kubectl apply -f' alias kdel='kubectl delete' alias kn='kubectl config set-context --current --namespace' source <(kubectl completion bash) complete -F __start_kubectl k
十二、命令速查卡
打印贴墙上用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ━━━ 查看 ━━━ get 列资源 kubectl get pods describe 看详情 kubectl describe pod xxx logs 看日志 kubectl logs xxx -f top 看资源用量 kubectl top pods ━━━ 创建 ━━━ apply 声明式创建 kubectl apply -f x.yaml create 命令式创建 kubectl create deployment x --image=y run 跑 Pod kubectl run x --image=y ━━━ 修改 ━━━set 改特定字段 kubectl set image deploy/x c=new:tag edit 交互编辑 kubectl edit deploy/x patch 精确修改 kubectl patch deploy x -p '{...}' scale 扩缩容 kubectl scale deploy x --replicas=3 rollout 滚动管理 kubectl rollout restart deploy/x ━━━ 删除 ━━━ delete 删除 kubectl delete pod x ━━━ 调试 ━━━exec 执行命令 kubectl exec -it x -- bashcp 复制文件 kubectl cp x:/path ./local logs 看日志 kubectl logs x port-forward 端口转发 kubectl port-forward svc/x 8080:80 ━━━ 标签 ━━━ label 管标签 kubectl label node x key=value taint 管污点 kubectl taint node x key=value:NoSchedule ━━━ 节点 ━━━ cordon 停调度 kubectl cordon node01 drain 驱逐 Pod kubectl drain node01 --ignore-daemonsets uncordon 恢复调度 kubectl uncordon node01