11.1 kubectl命令行工具
文章目录
- 一、kubectl 基础语法与概述
- 1.1 命令语法结构
- 1.2 多资源操作
- 1.3 帮助系统
- 二、核心资源操作命令
- 2.1 kubectl create - 创建资源
- 2.2 kubectl expose - 暴露服务
- 2.3 kubectl run - 快速启动Pod
- 2.4 kubectl set - 资源配置
- 三、查询与查看命令
- 3.1 kubectl explain - 资源文档
- 3.2 kubectl get - 列出资源
- 3.3 kubectl describe - 详细状态
- 四、编辑与删除命令
- 4.1 kubectl edit - 在线编辑
- 4.2 kubectl delete - 删除资源
- 五、调试与维护命令
- 5.1 kubectl logs - 查看日志
- 5.2 kubectl exec - 执行命令
- 5.3 kubectl attach - 附加到容器
- 5.4 kubectl cp - 文件拷贝
- 六、节点调度与维护命令
- 6.1 kubectl cordon/uncordon - 节点调度控制
- 6.2 kubectl drain - 节点排水
- 6.3 kubectl taint - 污点管理
- 七、配置更新与标签管理
- 7.1 kubectl apply - 声明式更新
- 7.2 kubectl diff - 配置对比
- 7.3 kubectl patch - 字段更新
- 7.4 kubectl label - 标签管理
- 7.5 kubectl annotate - 注解管理
- 八、集群信息与工具命令
- 8.1 kubectl cluster-info - 集群信息
- 8.2 kubectl top - 资源监控
- 8.3 kubectl version - 版本信息
- 8.4 kubectl completion - 命令补全
- 九、高级查询与管理命令
- 9.1 kubectl api-resources - API资源列表
- 9.2 kubectl api-versions - API版本
- 9.3 kubectl config - kubeconfig管理
- 十、资源类型简写参考
- 十一、输出格式控制
- 使用建议
一、kubectl 基础语法与概述
1.1 命令语法结构
kubectl [command] [TYPE] [NAME] [flags]
参数说明:
- command:操作资源的子命令,如
create
、get
、apply
、delete
等 - TYPE:资源类型,支持单数、复数或简写形式
kubectl get pod pod1 # 单数 kubectl get pods pod1 # 复数 kubectl get po pod1 # 简写
- NAME:资源名称(区分大小写),省略则返回该类型所有资源
- flags:可选参数,如
-s
指定 API Server URL
1.2 多资源操作
# 获取多个Pod信息
kubectl get pods pod1 pod2# 获取多种对象信息
kubectl get pod/pod1 rc/rc1# 同时应用多个yaml文件
kubectl apply -f pod1.yaml -f pod2.yaml
1.3 帮助系统
kubectl -h # 查看所有子命令
kubectl get -h # 查看特定子命令详细帮助
二、核心资源操作命令
2.1 kubectl create - 创建资源
# 从YAML文件创建
kubectl create -f nginx.yaml# 创建Deployment示例
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx
spec:replicas: 2selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.20
2.2 kubectl expose - 暴露服务
# 将Deployment暴露为Service
kubectl expose -f nginx.yaml --port=80 --target-port=80 --type=NodePort# 参数说明
--port=80 # Service对外端口
--target-port=80 # Pod实际监听端口
--type=NodePort # Service类型
2.3 kubectl run - 快速启动Pod
# 启动临时测试Pod
kubectl run nginx-run --image=nginx:1.20 --port=81# 注意:现代k8s版本只创建Pod,不创建Deployment
# 适合临时测试,无自愈能力,不适合生产环境
2.4 kubectl set - 资源配置
# 设置资源限制
kubectl set resources deployment nginx -c=nginx \--limits=cpu=200m,memory=512Mi \--requests=cpu=100m,memory=256Mi# 更新镜像版本
kubectl set image deployment/nginx nginx=nginx:1.22
三、查询与查看命令
3.1 kubectl explain - 资源文档
# 查看资源字段说明
kubectl explain deploy
kubectl explain pod# 递归查看所有字段
kubectl explain pod --recursive=true
3.2 kubectl get - 列出资源
kubectl get all # 查看所有资源
kubectl get pods # 查看Pod
kubectl get pods --show-labels # 显示标签
kubectl get node --show-labels # 查看节点标签
kubectl get svc # 查看Service
kubectl get ns # 查看命名空间
kubectl get pod -o wide # 宽格式输出更多信息
3.3 kubectl describe - 详细状态
kubectl describe pod mypod -n myns # 查看Pod详情
kubectl describe deploy mydeploy # 查看Deployment详情
kubectl describe svc mysvc # 查看Service详情
kubectl describe node node01 # 查看节点详情
与explain区别:
explain
:查看资源字段定义(说明书)describe
:查看运行中资源的状态(体检报告)
四、编辑与删除命令
4.1 kubectl edit - 在线编辑
# 编辑Deployment(修改副本数)
kubectl edit deployment/nginx# 编辑Service(修改端口)
kubectl edit service/nginx
4.2 kubectl delete - 删除资源
kubectl delete -f nginx.yaml # 通过文件删除
kubectl delete pods,services -l app=nginx # 按标签删除
kubectl delete pod nginx-pod # 删除单个Pod
kubectl delete deployment nginx # 删除Deployment
kubectl delete pod --all # 删除所有Pod
五、调试与维护命令
5.1 kubectl logs - 查看日志
kubectl logs nginx-pod # 查看日志快照
kubectl logs -f nginx-pod # 实时流式日志
kubectl logs redis-php -c redis # 多容器指定容器名
5.2 kubectl exec - 执行命令
kubectl exec nginx-pod -- date # 执行简单命令
kubectl exec -it nginx-pod -- /bin/bash # 交互式终端
kubectl exec redis-php -c redis -- bash # 指定容器
5.3 kubectl attach - 附加到容器
kubectl attach pod redis-php -c redis # 附加到运行中容器
5.4 kubectl cp - 文件拷贝
# 从容器拷贝到本地
kubectl cp -n default -c nginx nginx-pod:/etc/nginx/nginx.conf ./nginx.conf# 从本地拷贝到容器
kubectl cp config.txt -n default -c nginx nginx-pod:/tmp/config.txt# 注意:必须指定完整文件路径,不能是目录
六、节点调度与维护命令
6.1 kubectl cordon/uncordon - 节点调度控制
# 标记节点不可调度
kubectl cordon node01# 恢复节点可调度
kubectl uncordon node01
6.2 kubectl drain - 节点排水
# 安全驱逐节点上所有Pod
kubectl drain node01 --ignore-daemonsets --force --delete-local-data# 参数说明
--ignore-daemonsets # 忽略DaemonSet管理的Pod
--force # 强制删除无控制器管理的Pod
--delete-local-data # 删除使用emptyDir的Pod
6.3 kubectl taint - 污点管理
# 查看节点污点
kubectl describe node master01 | grep Taint# 删除污点
kubectl taint node master01 node-role.kubernetes.io/control-plane:NoSchedule-
七、配置更新与标签管理
7.1 kubectl apply - 声明式更新
kubectl apply -f nginx.yaml # 应用配置
kubectl apply -f <directory> # 应用目录下所有配置
7.2 kubectl diff - 配置对比
kubectl diff -f nginx.yaml # 对比当前配置与文件差异
7.3 kubectl patch - 字段更新
kubectl patch pod nginx-pod -p '{"spec":{"containers":[{"name":"nginx","image":"nginx:1.22"}]}}'
7.4 kubectl label - 标签管理
kubectl label pods nginx-pod os=kylinos # 添加标签
kubectl label --overwrite pods nginx-pod os=linux # 更新标签
kubectl label pods nginx-pod os- # 删除标签
7.5 kubectl annotate - 注解管理
kubectl annotate pods nginx-pod description='kylin-nginx' # 添加注解
kubectl annotate --overwrite pods nginx-pod description='linux-nginx' # 更新注解
kubectl annotate pods nginx-pod description- # 删除注解
八、集群信息与工具命令
8.1 kubectl cluster-info - 集群信息
kubectl cluster-info # 显示集群基本信息
8.2 kubectl top - 资源监控
# 需要安装metrics-server
kubectl top pods # 查看Pod资源使用
kubectl top nodes # 查看节点资源使用
8.3 kubectl version - 版本信息
kubectl version # 客户端和服务端版本
kubectl version --client # 仅客户端版本
8.4 kubectl completion - 命令补全
# 启用bash补全
source <(kubectl completion bash)# 永久生效
echo "source <(kubectl completion bash)" >> ~/.bashrc
九、高级查询与管理命令
9.1 kubectl api-resources - API资源列表
kubectl api-resources # 列出所有API资源
9.2 kubectl api-versions - API版本
kubectl api-versions # 列出可用API版本
9.3 kubectl config - kubeconfig管理
kubectl config view # 查看当前配置
kubectl config get-contexts # 获取上下文列表
kubectl config use-context my-context # 切换上下文
十、资源类型简写参考
资源类型 | 简写 | 用途 |
---|---|---|
Pod | po | 运行容器 |
Service | svc | 服务暴露 |
Deployment | deploy | 无状态应用部署 |
DaemonSet | ds | 节点守护Pod |
Job/CronJob | job /cj | 任务/定时任务 |
Ingress | ing | HTTP路由管理 |
Namespace | ns | 资源隔离 |
ConfigMap/Secret | cm /secret | 配置管理 |
PersistentVolume/PersistentVolumeClaim | pv /pvc | 存储管理 |
Node | no | 工作节点 |
StorageClass | sc | 存储类 |
十一、输出格式控制
kubectl get pods -o wide # 宽格式显示
kubectl get pods -o yaml # YAML格式
kubectl get pods -o json # JSON格式
kubectl get pods -o name # 仅显示名称
kubectl get pods -o custom-columns=... # 自定义列
使用建议
- 生产环境优先使用
kubectl apply
进行声明式配置管理 - 调试测试可使用
kubectl run
快速验证 - 批量操作善用标签选择器和通配符
- 复杂查询结合
-o
输出格式和--sort-by
排序 - 日常运维掌握
get
、describe
、logs
、exec
核心命令