k8s-kubectl命令

下面是一份 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 本地转发访问
相关推荐
x***r1518 分钟前
jdk-11.0.16.1_windows使用步骤详解(附JDK 11环境变量配置与验证教程)
java·开发语言·windows
弹简特44 分钟前
【Java项目-轻聊】01-项目演示+项目介绍+准备工作+项目源码
java
网宿安全演武实验室1 小时前
当AI跑进容器:全链路容器安全检测与智能运营实
人工智能·安全·容器·k8s
luck_bor1 小时前
File类&递归作业
java·开发语言
武子康1 小时前
Java-07 深入浅出 MyBatis数据库一对多关系模型实战:表结构设计与查询实现
java·后端
REDcker3 小时前
Linux OverlayFS详解
java·linux·运维
Royzst4 小时前
xml知识点
java·服务器·前端
鱼鳞_4 小时前
苍穹外卖-Day08(缓存套餐)
java·redis·缓存
过期动态4 小时前
【LeetCode 热题 100】移动零
java·数据结构·算法·leetcode·职场和发展·rabbitmq
一点事5 小时前
docker:安装oracle 19c
docker·oracle·容器