kubectl 的命令按「管上下文 / 看资源 / 查日志 / 改资源」四类记最省事。下面按用途分组,版本基准 kubectl 1.28+。

上下文与集群

config 子命令只读写 kubeconfig 文件,不访问集群。

1
2
3
4
5
6
# 列出所有 context,星号列是当前 context
kubectl config get-contexts
# 切换 context,会改写 kubeconfig 的 current-context
kubectl config use-context prod
# 改当前 context 的默认 namespace
kubectl config set-context --current --namespace=kube-system
  • get-contexts -o name 只输出名字;探活用 kubectl cluster-info 与 kubectl get --raw='/readyz'。

查看资源

1
2
3
4
5
6
7
8
9
# 一次可以写多个资源类型
kubectl get pods
kubectl get deploy,svc,ing
# -o wide 多出 NODE、IP 等列
kubectl get pods -o wide
# 完整对象,查字段值与 status
kubectl get pod web-0 -o yaml
# 详情 + 事件,排障第一步
kubectl describe pod web-0
  • -o 取值:wide、yaml、json、name、jsonpath、custom-columns。
  • describe 末尾的 Events 段是调度失败、拉镜像失败、探针失败的第一现场。
  • 字段含义直接查:kubectl explain deploy.spec.strategy.rollingUpdate。

日志与排查

1
2
3
4
5
6
7
8
9
10
# 跟踪日志;多容器 Pod 必须用 -c 指定容器
kubectl logs -f deploy/web --tail=100
kubectl logs pod/web-0 -c istio-proxy
# 看上一轮已退出容器的日志,CrashLoopBackOff 必备
kubectl logs pod/web-0 --previous
# 进容器,-- 之后才是容器内执行的命令
kubectl exec -it pod/web-0 -- sh
# 端口映射到本地,仅用于调试
kubectl port-forward pod/web-0 8080:8080
kubectl port-forward svc/web 8443:https
  • -p 是 --previous 的短选项;--tail 默认 -1,表示全量。
  • -l app=web 可跨 Pod 聚合日志,受 --max-log-requests 上限约束。
  • port-forward 默认只监听 localhost,不经过 Service 负载均衡,也不是生产暴露方式。

创建与变更

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 声明式、幂等,以文件为准
kubectl apply -f deploy.yaml
# 命令式创建
kubectl create deployment web --image=nginx:1.27 --replicas=3
# 原地编辑
kubectl edit svc/web
# 扩缩容
kubectl scale deploy/web --replicas=5
# 滚动更新
kubectl rollout status deploy/web
kubectl rollout history deploy/web
kubectl rollout undo deploy/web --to-revision=2
kubectl rollout restart deploy/web
# 删除,默认级联删掉下属 Pod
kubectl delete -f deploy.yaml
  • apply 维护 last-applied-configuration,与 create 混用于同一对象会产生字段归属冲突。
  • rollout restart 只给 Pod 模板打一个重启时间戳注解,不重建 Deployment 对象。
  • rollout history 的 CHANGE-CAUSE 默认是 <none>,需先执行 kubectl annotate deploy/web kubernetes.io/change-cause="..."。

kubectl run 与 create deployment 的区别

Kubernetes 1.18 起 kubectl run 移除了全部 generator,只创建 Pod;更早版本里 --restart=Always 生成 Deployment 的行为已经不存在。

1
2
3
4
# 裸 Pod,没有控制器兜底,删了就没了
kubectl run debug --image=busybox:1.36 --restart=Never -- sleep 3600
# 要 Deployment 就用 create 子命令
kubectl create deployment web --image=nginx:1.27 --replicas=3
  • --restart 直接写进 Pod 的 restartPolicy,取值 Always / OnFailure / Never。

标签、选择器与命名空间

1
2
3
4
5
6
7
8
kubectl get pods -l app=web,tier=frontend                # 标签选择器
kubectl get pods -l 'app in (web,api)' # 集合式写法
kubectl get pods --field-selector status.phase=Pending # 字段选择器
kubectl get pods --field-selector spec.nodeName=node-1
kubectl get pods -n kube-system # 指定命名空间
kubectl get pods -A # 等价 --all-namespaces
kubectl label deploy/web env=prod # 打标签
kubectl label deploy/web env- # 去掉标签
  • -l 是 --selector 的短选项;--field-selector 的可用字段由资源类型固定,写错时服务端返回 BadRequest;-A 会让输出多出一列 NAMESPACE。

常见报错

  • You must be logged in to the server (Unauthorized):凭据或 context 不对,核对 kubectl config get-contexts。
  • pods "web" not found:多半落到了默认 default 命名空间,补 -n。
  • unable to upgrade connection: container not found:容器还没 Running,或 -c 的容器名写错。
  • Metrics API not available:集群缺少 metrics-server,kubectl top 不可用。

参考