Menu
CoddyTech

Kubernetes 速查表

最后更新

上下文与集群信息

上下文由集群、用户和默认命名空间组成,保存在 ~/.kube/config 中。

作用命令
显示客户端和服务器版本kubectl version
显示集群地址kubectl cluster-info
列出上下文kubectl config get-contexts
显示当前上下文kubectl config current-context
切换到其他上下文kubectl config use-context prod
为当前上下文设置默认命名空间kubectl config set-context --current --namespace=dev
显示合并后的 kubeconfigkubectl config view
列出节点kubectl get nodes
列出所有资源类型及其简称kubectl api-resources
解释资源的某个字段kubectl explain deployment.spec.replicas

列出资源

kubectl get 列出某种类型的资源。加上 -n 可以查看其他命名空间。

作用命令
列出当前命名空间中的 Podkubectl get pods
列出所有命名空间中的 Podkubectl get pods -A
列出指定命名空间中的 Podkubectl get pods -n kube-system
增加节点和 IP 列kubectl get pods -o wide
显示标签kubectl get pods --show-labels
按标签筛选kubectl get pods -l app=web
按字段筛选kubectl get pods --field-selector status.phase=Running
持续监视变化kubectl get pods -w
同时列出 Deployment、Service 和 Podkubectl get deploy,svc,pods
列出常用资源kubectl get all
以 YAML 查看单个资源kubectl get deployment web -o yaml
列出 Servicekubectl get svc
列出命名空间kubectl get ns
列出最近的事件,从早到晚排序kubectl get events --sort-by=.metadata.creationTimestamp

describe、logs 与 exec

Pod 不是 Running 状态时,从这里开始排查。

作用命令
查看 Pod 的详情和事件kubectl describe pod web-7d4b9c8f6-x2k8p
查看 Deployment 详情kubectl describe deployment web
查看 Pod 日志kubectl logs web-7d4b9c8f6-x2k8p
实时跟踪日志kubectl logs -f web-7d4b9c8f6-x2k8p
查看容器重启前的日志kubectl logs --previous web-7d4b9c8f6-x2k8p
查看 Pod 中某个容器的日志kubectl logs web-7d4b9c8f6-x2k8p -c sidecar
查看 Deployment 下各个 Pod 的日志kubectl logs deployment/web
查看带某个标签的所有 Pod 的日志kubectl logs -l app=web --tail=50
在 Pod 中打开 shellkubectl exec -it web-7d4b9c8f6-x2k8p -- sh
在 Pod 中执行一条命令kubectl exec web-7d4b9c8f6-x2k8p -- env
把本地 8080 端口转发到 Pod 的 80 端口kubectl port-forward pod/web-7d4b9c8f6-x2k8p 8080:80
转发到 Servicekubectl port-forward svc/web 8080:80
从 Pod 中复制文件出来kubectl cp web-7d4b9c8f6-x2k8p:/etc/nginx/nginx.conf ./nginx.conf
查看 CPU 和内存占用(需要 metrics-server)kubectl top pods
启动一个带 shell 的临时 Podkubectl run tmp --rm -it --image=busybox -- sh

创建与应用

apply 会创建资源,或把它更新成与文件一致;用 Git 管理清单时就用这个命令。

作用命令
从文件创建或更新kubectl apply -f deployment.yaml
应用目录中的所有文件kubectl apply -f k8s/
应用 kustomization 目录kubectl apply -k overlays/prod
查看 apply 会改动什么kubectl diff -f deployment.yaml
创建 Deploymentkubectl create deployment web --image=nginx --replicas=3
启动单个 Podkubectl run web --image=nginx
把 Deployment 暴露为 Servicekubectl expose deployment web --port=80 --target-port=8080
在每个节点的端口上暴露kubectl expose deployment web --type=NodePort --port=80
用键值创建 ConfigMapkubectl create configmap app-config --from-literal=LOG_LEVEL=debug
用键值创建 Secretkubectl create secret generic db-pass --from-literal=password=s3cret
从文件创建 ConfigMapkubectl create configmap nginx-conf --from-file=nginx.conf
生成 Deployment 的 YAML 但不创建kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yaml
运行一次 Jobkubectl create job hello --image=busybox -- echo hello
在编辑器中修改运行中的资源kubectl edit deployment web

Deployment 与发布

作用命令
扩容到 5 个副本kubectl scale deployment web --replicas=5
更换镜像(会开始一次发布)kubectl set image deployment/web nginx=nginx:1.27
观察发布直到完成kubectl rollout status deployment/web
查看发布历史kubectl rollout history deployment/web
回滚到上一个版本kubectl rollout undo deployment/web
回滚到指定版本kubectl rollout undo deployment/web --to-revision=2
重启 Deployment 的所有 Podkubectl rollout restart deployment/web
暂停发布kubectl rollout pause deployment/web
继续发布kubectl rollout resume deployment/web
按 CPU 占用自动扩缩容kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70
设置环境变量kubectl set env deployment/web LOG_LEVEL=debug
设置 CPU 和内存请求kubectl set resources deployment web --requests=cpu=100m,memory=128Mi

删除

Deployment 会重建你删除的任何 Pod;要彻底删除这些 Pod,就删除 Deployment。

作用命令
删除文件创建的资源kubectl delete -f deployment.yaml
删除 Podkubectl delete pod web-7d4b9c8f6-x2k8p
删除 Deployment 及其 Podkubectl delete deployment web
按标签删除 Podkubectl delete pods -l app=web
立即删除 Pod,不等待kubectl delete pod web-7d4b9c8f6-x2k8p --grace-period=0 --force
删除命名空间及其中的所有资源kubectl delete namespace dev

命名空间

作用命令
列出命名空间kubectl get namespaces
创建命名空间kubectl create namespace dev
在指定命名空间中运行任意命令kubectl get pods -n dev
设为当前上下文的默认命名空间kubectl config set-context --current --namespace=dev
显示当前上下文的命名空间kubectl config view --minify -o jsonpath='{..namespace}'

标签与注解

作用命令
添加标签kubectl label pods web-7d4b9c8f6-x2k8p tier=frontend
修改标签kubectl label pods web-7d4b9c8f6-x2k8p tier=backend --overwrite
删除标签kubectl label pods web-7d4b9c8f6-x2k8p tier-
添加注解kubectl annotate deployment web owner=team-a
用 patch 修改某个字段kubectl patch deployment web -p '{"spec":{"replicas":2}}'

节点

作用命令
列出节点及其 IP 和操作系统kubectl get nodes -o wide
查看节点的容量、状态和 Podkubectl describe node worker-1
禁止新 Pod 调度到节点上kubectl cordon worker-1
维护前把 Pod 从节点上驱逐kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-data
重新允许 Pod 调度到节点上kubectl uncordon worker-1
添加污点kubectl taint nodes worker-1 gpu=true:NoSchedule
删除污点kubectl taint nodes worker-1 gpu=true:NoSchedule-
查看节点的 CPU 和内存占用(需要 metrics-server)kubectl top nodes

输出格式

在任何 get 后加上 -o,就能改变输出内容。

作用命令
以 YAML 输出完整对象kubectl get pod web-7d4b9c8f6-x2k8p -o yaml
以 JSON 输出完整对象kubectl get pod web-7d4b9c8f6-x2k8p -o json
只输出名称(type/name)kubectl get pods -o name
用 JSONPath 选取字段kubectl get pods -o jsonpath='{.items[*].metadata.name}'
自定义列kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName
按字段排序kubectl get pods --sort-by=.status.startTime
解码 Secret 的值kubectl get secret db-pass -o jsonpath='{.data.password}' | base64 -d

简称

大多数资源类型都有简称,可以代替全名输入。

资源简称
Podpo
Deploymentdeploy
ReplicaSetrs
StatefulSetsts
DaemonSetds
Servicesvc
Ingressing
ConfigMapcm
Namespacens
Nodeno
PersistentVolumeClaimpvc
PersistentVolumepv
ServiceAccountsa
HorizontalPodAutoscalerhpa
CronJobcj

使用 Kubernetes(k8s)时常用的 kubectl 命令,都整理在可直接复制的表格里:先是上下文和命名空间,然后是列出和查看资源、logs 和 exec、应用清单、Deployment 与发布、扩缩容、标签、节点和输出格式。

kubectl 操作的是当前上下文指向的集群,所以删除任何东西之前,先用 kubectl config current-context 确认一下。没有集群也想试试这些命令的话,打开终端在线编辑器即可,里面内置了单节点 Kubernetes 模拟器。

kubectl 速查表常见问题

kubectl 是什么?
kubectl 是 Kubernetes 的命令行客户端,它把你的命令发送到集群的 API 服务器:kubectl get pods 列出 Pod,kubectl apply -f 根据 YAML 创建或更新资源,kubectl logs 和 kubectl exec 用来查看运行中的容器内部。它和哪个集群通信,由 ~/.kube/config 中的当前上下文决定。
k8s 和 Kubernetes 是一回事吗?

是的。k8s 是 Kubernetes 的简写:开头的 k,中间的 8 个字母,加上结尾的 s。k8s 速查表就是 kubectl 速查表,因为操作集群靠的就是 kubectl。

kubectl apply 和 kubectl create 有什么区别?
kubectl create 创建资源,如果资源已存在就会失败。kubectl apply 会创建资源,或把它更新成与文件一致,所以每次修改后都可以再运行一次。用 Git 管理的清单用 apply,临时快速创建的资源用 create。
如何查看 Pod 为什么启动不了?
运行 kubectl describe pod <name>,看底部的 Events:拉取镜像出错、调度失败和探针失败都会显示在这里。如果容器启动后崩溃了,kubectl logs --previous <name> 会显示崩溃那次运行的输出。
如何在 kubectl 中切换命名空间?
只对单条命令生效的话,加上 -n dev;想让 dev 成为当前上下文的默认命名空间,运行 kubectl config set-context --current --namespace=dev。
如何重启 Deployment?
kubectl rollout restart deployment/web 会分批把每个 Pod 替换成新的;副本多于一个时,服务不会中断。kubectl rollout status deployment/web 会一直等到重启完成。
可以在线练习 kubectl 命令吗?
可以。终端在线编辑器里有单节点 Kubernetes 模拟器:kubectl get、describe、apply、logs、exec、Deployment、发布和 Service 的响应都和真实集群一样,无需安装任何东西。少数命令,比如 port-forward 和 edit,需要真实的集群,例如 kind 或 minikube。
这份 kubectl 速查表免费吗?

免费。可以免费阅读、复制和打印,无需注册。

Coddy 编程语言插图

在 Coddy 学习 Kubernetes

开始