kubectl命令完整手册

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 <资源类型> [名称] [选项]

# 看 Pod
kubectl get pods # 默认 namespace
kubectl get pods -n kube-system # 指定 namespace
kubectl get pods -A # 所有 namespace
kubectl get pods --all-namespaces # 同上,长写法

# 看节点
kubectl get nodes
kubectl get nodes -o wide # 显示更多列(IP、OS等)
kubectl get nodes --show-labels # 显示标签

# 看其他资源
kubectl get deployments # 简写: kubectl get deploy
kubectl get services # 简写: kubectl get svc
kubectl get configmaps # 简写: kubectl get cm
kubectl get secrets
kubectl get ingress
kubectl get pv # 持久卷
kubectl get pvc # 持久卷声明
kubectl get ns # 命名空间
kubectl get sa # ServiceAccount
kubectl get events # 事件
kubectl get jobs
kubectl get cronjobs

# 常用组合
kubectl get all # 看当前 namespace 所有主要资源
kubectl get all -n monitoring # 看 monitoring namespace 所有资源
kubectl get pods -o wide # 显示 IP、节点等额外信息
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 # YAML 格式
kubectl get pod nginx -o json # 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

# 常用场景: Pod 启动失败,看 Events
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 # 单容器 Pod
kubectl logs nginx-xxx -c nginx # 多容器 Pod,指定容器名
kubectl logs nginx-xxx --previous # 容器重启过,看上次的日志
kubectl logs nginx-xxx -f # 跟随日志(tail -f)
kubectl logs nginx-xxx --tail=100 # 最后 100 行
kubectl logs nginx-xxx --since=1h # 最近 1 小时
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 # 所有 nginx Pod 日志(10 行各)
kubectl logs -l app=nginx --tail=100 --prefix # 加上 Pod 名前缀

# 上次崩溃的日志(排查 CrashLoopBackOff)
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

# 看 Pod 资源
kubectl top pods # 当前 namespace
kubectl top pods -A # 所有 namespace
kubectl top pods -n monitoring
kubectl top pods --containers # 按容器细分
kubectl top pods --sort-by=memory # 按内存排序
kubectl top pods --sort-by=cpu -A | head # 全集群 CPU TOP

二、创建和删除资源

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/ # 目录下所有 YAML
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=server

# 从 stdin 创建
cat <<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

# 从 YAML 创建(apply 也能,区别:create 遇到已存在会报错)
kubectl create -f nginx.yaml

# 生成 YAML(不真创建,打印 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

# 跑临时 Pod 排查网络
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 # 会删除整个 namespace 下所有东西

# 按文件删
kubectl delete -f nginx.yaml

# 按标签删
kubectl delete pods -l app=nginx

# 删所有
kubectl delete pods --all -n default
kubectl delete all --all -n my-ns # 危险! 删除所有主要资源

# 强制删除(卡在 Terminating 时)
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- # 删除变量(key 后加减号)

# 修改 Selector(很少用)
kubectl set selector svc/nginx app=nginx-v2

# 修改 ServiceAccount
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

# 用 JSON 格式编辑(默认 YAML)
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
# 基础用法:三种 patch 类型
# strategic(默认,k8s 原生)
# merge(标准 JSON merge)
# json(JSON Patch 操作)

# 常用示例

# 改副本数
kubectl patch deployment nginx -p '{"spec":{"replicas":3}}'

# 改镜像
kubectl patch deployment nginx -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.25"}]}}}}'

# 加污点(node)
kubectl patch node node01 -p '{"spec":{"taints":[{"key":"dedicated","value":"db","effect":"NoSchedule"}]}}'

# json patch(最精确,能用索引)
kubectl patch deployment grafana -n monitoring --type=json -p='[
{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--debug"}
]'

# 移除 finalizer(清理卡住资源时用)
kubectl patch namespace calico-apiserver -p '{"metadata":{"finalizers":null}}' --type=merge

# 从文件 patch
kubectl patch -f nginx.yaml -p "$(cat patch.yaml)"

12. replace - 替换整个资源

1
2
3
4
5
6
7
8
# 基于 YAML 文件替换
kubectl replace -f nginx.yaml

# 强制替换(先删后创建)
kubectl replace -f nginx.yaml --force

# 从 stdin
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 # 缩容到 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 # 回到指定版本

# 重启(重建所有 Pod,不改配置)
kubectl rollout restart deployment nginx
kubectl rollout restart deployment grafana -n monitoring # 重启让新配置生效

# 暂停/恢复(批量改配置时用)
kubectl rollout pause deployment nginx
# ...这时候 set image 等改不会立即生效...
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

# 删除标签(key 后加减号)
kubectl label nodes node01 workload-

# 批量加
kubectl label nodes node01 node02 tier=frontend

# 给所有节点加
kubectl label nodes --all team=devops

# 按 label 筛选操作
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

# 删除污点(key 或 key:effect 后加减号)
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 # alpine 镜像用 sh

# 多容器 Pod 指定容器
kubectl exec -it grafana-xxx -c grafana -- bash

# 指定 namespace
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 # 简写(当前 ns)

# 从本地到容器
kubectl cp ./config.yaml default/nginx:/tmp/config.yaml

# 多容器 Pod
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
# 转发 Pod 端口
kubectl port-forward pod/nginx 8080:80

# 转发 Service 端口
kubectl port-forward svc/nginx 8080:80

# 转发 Deployment
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
# 启动代理(默认 8001 端口)
kubectl proxy

# 指定端口
kubectl proxy --port=8080

# 然后可以直接用 curl 访问
curl http://localhost:8001/api/v1/namespaces
curl http://localhost:8001/healthz

22. debug - 调试工具(1.25+)

1
2
3
4
5
6
7
8
# 创建 ephemeral 容器调试
kubectl debug -it nginx --image=busybox:1.28 --target=nginx

# 复制 Pod 调试
kubectl debug nginx -it --copy-to=nginx-debug --container=nginx

# 调试节点(进入节点 shell)
kubectl debug node/node01 -it --image=ubuntu

六、集群和节点管理

23. cluster-info - 集群信息

1
2
3
4
5
6
7
kubectl cluster-info                       # 基础信息
kubectl cluster-info dump # 详细 dump(大量输出)
kubectl version # 客户端和服务端版本
kubectl api-versions # 所有支持的 API 版本
kubectl api-resources # 所有 API 资源
kubectl api-resources --namespaced=true # 只看 namespace 级资源
kubectl api-resources -o wide # 看简写和 API group

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
# 驱逐节点上的所有 Pod(会先 cordon)
kubectl drain node01

# 忽略 DaemonSet
kubectl drain node01 --ignore-daemonsets

# 强制删除无控制器的 Pod
kubectl drain node01 --ignore-daemonsets --force

# 删除本地存储数据的 Pod
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 # 只看当前 context
kubectl config view --raw # 完整(含证书)

# 上下文管理
kubectl config current-context # 当前 context
kubectl config get-contexts # 所有 context
kubectl config use-context my-cluster # 切换

# 默认 namespace
kubectl config set-context --current --namespace=monitoring # 改当前 context 的默认 ns

# 设置集群、用户
kubectl config set-cluster my-cluster --server=https://xxx:6443
kubectl config set-credentials user1 --token=xxx

# 使用不同 kubeconfig
kubectl --kubeconfig=/path/to/kubeconfig get nodes
# 或环境变量
export 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 # YAML
-o json # JSON
-o wide # 宽格式
-o name # 只要名字
-o jsonpath='{...}' # JSONPath 表达式
-o jsonpath-as-json='{...}' # JSON 格式输出
-o go-template='...' # Go 模板
-o custom-columns=<cols> # 自定义列

# JSONPath 示例
kubectl get pods -o jsonpath='{.items[*].metadata.name}' # 所有 Pod 名
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' # AND
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 # 有 app 标签的
kubectl get pods -l '!app' # 没有 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
# 等 Pod 就绪
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

# 等 Deployment 滚动完成
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
# 批量重启所有 Deployment
for d in $(kubectl get deploy -n monitoring -o name); do
kubectl rollout restart $d -n monitoring
done

# 给所有 Pod 加 label
kubectl label pods --all env=prod -n my-ns

# 删除所有 Evicted 状态的 Pod
kubectl get pods -A | grep Evicted | awk '{print "kubectl delete pod " $2 " -n " $1}' | sh

# 找出使用最多内存的 Pod
kubectl top pods -A --sort-by=memory | head

排查问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 找没跑起来的 Pod
kubectl get pods -A | grep -v "Running\|Completed"

# 看某节点上的所有 Pod
kubectl get pods -A -o wide --field-selector spec.nodeName=node01

# 查某个 Pod 全家桶信息
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
# 生成 Deployment YAML(不创建)
kubectl create deployment nginx --image=nginx -o yaml --dry-run=client > nginx.yaml

# 生成 Service YAML
kubectl expose deployment nginx --port=80 -o yaml --dry-run=client > nginx-svc.yaml

# 生成 Secret YAML
kubectl create secret generic my-secret --from-literal=key=value -o yaml --dry-run=client

# 生成 ConfigMap YAML
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
# 以改 Grafana 内存为例

# 方式 1: set resources(最简单,针对性强)
kubectl set resources deployment grafana -n monitoring --limits=memory=400Mi

# 方式 2: edit(交互式,灵活)
kubectl edit deployment grafana -n monitoring

# 方式 3: patch(脚本化,精确)
kubectl patch deployment grafana -n monitoring --type=json -p='[
{"op": "replace", "path": "/spec/template/spec/containers/0/resources/limits/memory", "value": "400Mi"}
]'

# 方式 4: apply(声明式,可版本控制)
# 先改 grafana.yaml
kubectl apply -f grafana.yaml

# 方式 5: helm upgrade(如果是 Helm 装的,最规范)
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=kubectl
alias 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'
# 用法: kn monitoring

# 自动补全
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 -- bash
cp 复制文件 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

kubectl命令完整手册
https://anyue967.github.io/posts/41908a5e.html
作者
anyu967
发布于
2026年5月20日
许可协议