下面是一份 kubectl 常用命令速查表,偏日常排查和部署使用。
1. 基础信息
查看 kubectl 版本
bash
kubectl version --client
查看客户端和服务端版本:
bash
kubectl version
查看当前集群信息
bash
kubectl cluster-info
查看当前 context
bash
kubectl config current-context
查看所有 context
bash
kubectl config get-contexts
切换 context
bash
kubectl config use-context <context-name>
2. Namespace 命名空间
查看所有 namespace
bash
kubectl get ns
等价:
bash
kubectl get namespaces
创建 namespace
bash
kubectl create namespace prefect
简写:
bash
kubectl create ns prefect
删除 namespace
bash
kubectl delete namespace prefect
设置当前默认 namespace
bash
kubectl config set-context --current --namespace=prefect
切回 default:
bash
kubectl config set-context --current --namespace=default
查看某 namespace 下所有常用资源
bash
kubectl get all -n prefect
3. 查看资源
查看 Pod
当前 namespace:
bash
kubectl get pods
指定 namespace:
bash
kubectl get pods -n prefect
所有 namespace:
bash
kubectl get pods -A
输出更多信息:
bash
kubectl get pods -o wide
指定 namespace 更多信息:
bash
kubectl get pods -n prefect -o wide
查看 Deployment
bash
kubectl get deploy
指定 namespace:
bash
kubectl get deploy -n prefect
查看 Service
bash
kubectl get svc
指定 namespace:
bash
kubectl get svc -n prefect
查看 ConfigMap
bash
kubectl get cm
查看 Secret
bash
kubectl get secret
查看 Job
bash
kubectl get jobs
指定 namespace:
bash
kubectl get jobs -n prefect
查看所有资源
bash
kubectl get all
指定 namespace:
bash
kubectl get all -n prefect
所有 namespace:
bash
kubectl get all -A
4. describe 查看详情
查看 Pod 详情
bash
kubectl describe pod <pod-name>
指定 namespace:
bash
kubectl describe pod <pod-name> -n prefect
查看 Node 详情
bash
kubectl describe node <node-name>
查看 Service 详情
bash
kubectl describe svc <service-name> -n prefect
查看 Deployment 详情
bash
kubectl describe deploy <deployment-name> -n prefect
这个主要用来看:
text
Events
Status
Image
Resources
Volume
Environment
Reason
5. 查看日志
查看 Pod 日志
bash
kubectl logs <pod-name>
指定 namespace:
bash
kubectl logs <pod-name> -n prefect
实时跟踪日志
bash
kubectl logs -f <pod-name> -n prefect
多容器 Pod 指定容器
先查看容器名:
bash
kubectl get pod <pod-name> -n prefect -o jsonpath='{.spec.containers[*].name}'
查看某容器日志:
bash
kubectl logs <pod-name> -n prefect -c <container-name>
查看上一次崩溃日志
bash
kubectl logs <pod-name> -n prefect --previous
指定容器:
bash
kubectl logs <pod-name> -n prefect -c <container-name> --previous
6. 进入容器
exec 进入 Pod
bash
kubectl exec -it <pod-name> -- bash
指定 namespace:
bash
kubectl exec -it <pod-name> -n prefect -- bash
如果镜像没有 bash,用 sh:
bash
kubectl exec -it <pod-name> -n prefect -- sh
多容器指定容器:
bash
kubectl exec -it <pod-name> -n prefect -c <container-name> -- bash
7. 创建 / 应用 YAML
应用 YAML
bash
kubectl apply -f file.yaml
应用目录下所有 YAML:
bash
kubectl apply -f ./manifests/
指定 namespace:
bash
kubectl apply -f file.yaml -n prefect
创建资源
bash
kubectl create -f file.yaml
区别简单理解:
text
apply:声明式,可重复执行,会更新
create:创建式,已存在会报错
8. 删除资源
删除 YAML 中定义的资源
bash
kubectl delete -f file.yaml
删除 Pod
bash
kubectl delete pod <pod-name> -n prefect
删除 Deployment
bash
kubectl delete deploy <deployment-name> -n prefect
删除 Service
bash
kubectl delete svc <service-name> -n prefect
删除 namespace
bash
kubectl delete ns prefect
删除所有 Pod
bash
kubectl delete pods --all -n prefect
9. 重启 / 扩缩容
重启 Deployment
bash
kubectl rollout restart deployment <deployment-name> -n prefect
查看 rollout 状态
bash
kubectl rollout status deployment <deployment-name> -n prefect
查看 rollout 历史
bash
kubectl rollout history deployment <deployment-name> -n prefect
回滚 Deployment
bash
kubectl rollout undo deployment <deployment-name> -n prefect
扩缩容 Deployment
bash
kubectl scale deployment <deployment-name> -n prefect --replicas=3
停掉:
bash
kubectl scale deployment <deployment-name> -n prefect --replicas=0
10. 编辑资源
直接编辑资源
bash
kubectl edit deployment <deployment-name> -n prefect
编辑 Service:
bash
kubectl edit svc <service-name> -n prefect
编辑 RayCluster:
bash
kubectl edit raycluster <raycluster-name> -n prefect
11. 查看 YAML / JSON
输出 YAML
bash
kubectl get pod <pod-name> -n prefect -o yaml
输出 JSON
bash
kubectl get pod <pod-name> -n prefect -o json
查看某字段
bash
kubectl get pod <pod-name> -n prefect -o jsonpath='{.status.phase}'
查看 Pod 容器名:
bash
kubectl get pod <pod-name> -n prefect -o jsonpath='{.spec.containers[*].name}'
查看 Pod IP:
bash
kubectl get pod <pod-name> -n prefect -o jsonpath='{.status.podIP}'
12. 端口转发
转发 Service 端口
bash
kubectl port-forward -n prefect svc/raycluster-kuberay-head-svc 8265:8265
然后访问:
text
http://localhost:8265
转发 Pod 端口
bash
kubectl port-forward -n prefect pod/<pod-name> 8265:8265
13. 事件排查
查看当前 namespace 事件
bash
kubectl get events
指定 namespace:
bash
kubectl get events -n prefect
按时间排序:
bash
kubectl get events -n prefect --sort-by=.lastTimestamp
看所有 namespace 事件:
bash
kubectl get events -A --sort-by=.lastTimestamp
常用于排查:
text
FailedScheduling
ImagePullBackOff
CrashLoopBackOff
OOMKilled
FailedMount
14. 资源使用情况
前提是集群装了 metrics-server。
查看节点资源
bash
kubectl top nodes
查看 Pod 资源
bash
kubectl top pods
指定 namespace:
bash
kubectl top pods -n prefect
所有 namespace:
bash
kubectl top pods -A
15. Node 相关
查看节点
bash
kubectl get nodes
更多信息:
bash
kubectl get nodes -o wide
查看详情:
bash
kubectl describe node <node-name>
禁止调度到某节点
bash
kubectl cordon <node-name>
恢复调度
bash
kubectl uncordon <node-name>
驱逐节点上的 Pod
bash
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data
16. 标签 Label
查看 labels
bash
kubectl get pods --show-labels
指定 namespace:
bash
kubectl get pods -n prefect --show-labels
按 label 查询
bash
kubectl get pods -n prefect -l app=ray
给资源打标签
bash
kubectl label pod <pod-name> env=dev -n prefect
删除标签
bash
kubectl label pod <pod-name> env- -n prefect
17. 常用排查命令组合
Pod Pending
bash
kubectl describe pod <pod-name> -n prefect
kubectl get events -n prefect --sort-by=.lastTimestamp
kubectl describe node <node-name>
常见原因:
text
Insufficient cpu
Insufficient memory
nodeSelector 不匹配
PVC Pending
ImagePullBackOff
bash
kubectl describe pod <pod-name> -n prefect
kubectl get events -n prefect --sort-by=.lastTimestamp
常见原因:
text
镜像名错
tag 不存在
私有仓库没权限
网络拉不到镜像
CrashLoopBackOff
bash
kubectl logs <pod-name> -n prefect
kubectl logs <pod-name> -n prefect --previous
kubectl describe pod <pod-name> -n prefect
常见原因:
text
应用启动报错
命令不存在
配置错误
依赖缺失
健康检查失败
Service 访问不通
bash
kubectl get svc -n prefect
kubectl describe svc <svc-name> -n prefect
kubectl get endpoints -n prefect
kubectl get pods -n prefect --show-labels
排查:
text
selector 是否匹配 Pod label
endpoint 是否为空
端口是否正确
Service 类型是否可外部访问
18. 结合你当前 Ray 场景常用命令
查看 Ray Pod:
bash
kubectl get pods -n prefect -o wide
查看 Ray Service:
bash
kubectl get svc -n prefect
转发 Ray Dashboard:
bash
kubectl port-forward -n prefect svc/raycluster-kuberay-head-svc 8265:8265
看 Ray Head 日志:
bash
kubectl logs -n prefect <ray-head-pod-name>
查看 RayCluster:
bash
kubectl get raycluster -n prefect
kubectl describe raycluster raycluster-kuberay -n prefect
kubectl get raycluster raycluster-kuberay -n prefect -o yaml
删除 RayCluster:
bash
kubectl delete raycluster raycluster-kuberay -n prefect
19. 最常用 20 条
bash
kubectl get ns
kubectl get pods -A
kubectl get pods -n prefect -o wide
kubectl get all -n prefect
kubectl get svc -n prefect
kubectl describe pod <pod-name> -n prefect
kubectl logs <pod-name> -n prefect
kubectl logs -f <pod-name> -n prefect
kubectl exec -it <pod-name> -n prefect -- bash
kubectl apply -f app.yaml
kubectl delete -f app.yaml
kubectl delete pod <pod-name> -n prefect
kubectl scale deployment <name> -n prefect --replicas=0
kubectl rollout restart deployment <name> -n prefect
kubectl get events -n prefect --sort-by=.lastTimestamp
kubectl describe node <node-name>
kubectl top nodes
kubectl top pods -A
kubectl port-forward -n prefect svc/<svc-name> 8080:80
kubectl config set-context --current --namespace=prefect
20. 一个记忆口诀
text
get 看列表
describe 看详情和事件
logs 看日志
exec 进容器
apply 应用 YAML
delete 删除资源
scale 扩缩容
rollout 发布/重启/回滚
port-forward 本地转发访问