Kubectl 命令使用总结

Kubectl 命令使用总结

基础命令

查看集群信息

bash 复制代码
# 查看集群信息
kubectl cluster-info

# 查看 Kubernetes 版本
kubectl version

# 查看 API 资源列表
kubectl api-resources

# 查看 API 版本
kubectl api-versions

查看节点信息

bash 复制代码
# 查看所有节点
kubectl get nodes

# 查看节点详细信息
kubectl get nodes -o wide

# 查看节点详细信息(JSON/YAML)
kubectl get nodes -o json
kubectl get nodes -o yaml

# 查看特定节点
kubectl get node <node-name>

# 描述节点
kubectl describe node <node-name>

资源查看

Pod 相关

bash 复制代码
# 查看所有命名空间的 Podkubectl get pods --all-namespaces
kubectl get pods -A

# 查看指定命名空间的 Podkubectl get pods -n <namespace>

# 查看 Pod 详细信息
kubectl get pods -o wide

# 查看 Pod 详细信息(JSON/YAML)
kubectl get pods -o json
kubectl get pods -o yaml

# 查看特定 Podkubectl get pod <pod-name> -n <namespace>

# 查看 Pod 标签
kubectl get pods --show-labels

Deployment 相关

bash 复制代码
# 查看所有 Deploymentkubectl get deployments
kubectl get deploy

# 查看 Deployment 详细信息
kubectl get deployments -o wide

# 查看特定 Deploymentkubectl get deployment <deployment-name> -n <namespace>

Service 相关

bash 复制代码
# 查看所有 Servicekubectl get services
kubectl get svc

# 查看 Service 详细信息
kubectl get services -o wide

# 查看特定 Servicekubectl get service <service-name> -n <namespace>

其他资源

bash 复制代码
# 查看所有资源类型
kubectl get all

# 查看 ConfigMapkubectl get configmaps
kubectl get cm

# 查看 Secretkubectl get secrets

# 查看 PersistentVolumekubectl get pv

# 查看 PersistentVolumeClaimkubectl get pvc

# 查看 Ingresskubectl get ingress
kubectl get ing

# 查看 Jobkubectl get jobs

# 查看 CronJobkubectl get cronjobs

# 查看 DaemonSetkubectl get daemonsets
kubectl get ds

# 查看 StatefulSetkubectl get statefulsets
kubectl get sts

资源操作

创建资源

bash 复制代码
# 从文件创建资源
kubectl create -f <file.yaml>

# 从 URL 创建资源
kubectl create -f <url>

# 从目录创建所有资源
kubectl create -f <directory>/

# 创建命名空间
kubectl create namespace <namespace-name>

应用资源

bash 复制代码
# 应用配置文件(推荐)
kubectl apply -f <file.yaml>

# 应用目录下所有文件
kubectl apply -f <directory>/

# 应用并记录配置
kubectl apply -f <file.yaml> --record

# 强制替换资源
kubectl apply -f <file.yaml> --force

删除资源

bash 复制代码
# 删除资源
kubectl delete -f <file.yaml>

# 删除特定资源
kubectl delete pod <pod-name> -n <namespace>
kubectl delete deployment <deployment-name> -n <namespace>
kubectl delete service <service-name> -n <namespace>

# 删除所有资源
kubectl delete all --all -n <namespace>

# 删除命名空间(会删除命名空间下所有资源)
kubectl delete namespace <namespace-name>

# 强制删除 Podkubectl delete pod <pod-name> --force --grace-period=0

编辑资源

bash 复制代码
# 编辑资源(使用默认编辑器)
kubectl edit pod <pod-name> -n <namespace>
kubectl edit deployment <deployment-name> -n <namespace>

# 编辑并应用
kubectl edit -f <file.yaml>

替换资源

bash 复制代码
# 替换资源
kubectl replace -f <file.yaml>

# 强制替换
kubectl replace -f <file.yaml> --force

调试和日志

查看日志

bash 复制代码
# 查看 Pod 日志
kubectl logs <pod-name> -n <namespace>

# 查看前 N 行日志
kubectl logs <pod-name> -n <namespace> --tail=100

# 实时跟踪日志
kubectl logs <pod-name> -n <namespace> -f

# 查看之前容器的日志
kubectl logs <pod-name> -n <namespace> --previous

# 查看多容器 Pod 中特定容器的日志
kubectl logs <pod-name> -c <container-name> -n <namespace>

# 查看指定时间范围的日志
kubectl logs <pod-name> -n <namespace> --since=1h
kubectl logs <pod-name> -n <namespace> --since-time="2024-01-01T00:00:00Z"

进入容器

bash 复制代码
# 进入 Pod 容器(交互式)
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh

# 在容器中执行命令
kubectl exec <pod-name> -n <namespace> -- <command>

# 在特定容器中执行命令
kubectl exec <pod-name> -c <container-name> -n <namespace> -- <command>

调试工具

bash 复制代码
# 查看 Pod 详细信息
kubectl describe pod <pod-name> -n <namespace>

# 查看事件
kubectl get events -n <namespace> --sort-by='.lastTimestamp'

# 查看资源使用情况
kubectl top nodes
kubectl top pods -n <namespace>

# 查看资源使用情况(所有命名空间)
kubectl top pods --all-namespaces

配置管理

ConfigMap 操作

bash 复制代码
# 查看 ConfigMapkubectl get configmap <configmap-name> -n <namespace>

# 查看 ConfigMap 内容
kubectl get configmap <configmap-name> -n <namespace> -o yaml

# 从文件创建 ConfigMapkubectl create configmap <configmap-name> --from-file=<file-path>

# 从目录创建 ConfigMapkubectl create configmap <configmap-name> --from-file=<directory>/

# 从字面量创建 ConfigMapkubectl create configmap <configmap-name> --from-literal=key1=value1 --from-literal=key2=value2

# 编辑 ConfigMapkubectl edit configmap <configmap-name> -n <namespace>

# 删除 ConfigMapkubectl delete configmap <configmap-name> -n <namespace>

Secret 操作

bash 复制代码
# 查看 Secretkubectl get secret <secret-name> -n <namespace>

# 查看 Secret 内容(Base64 编码)
kubectl get secret <secret-name> -n <namespace> -o yaml

# 解码 Secretkubectl get secret <secret-name> -n <namespace> -o jsonpath='{.data.<key>}' | base64 -d

# 从文件创建 Secretkubectl create secret generic <secret-name> --from-file=<file-path>

# 从字面量创建 Secretkubectl create secret generic <secret-name> --from-literal=username=admin --from-literal=password=secret

# 从 Docker 注册表创建 Secretkubectl create secret docker-registry <secret-name> --docker-server=<server> --docker-username=<username> --docker-password=<password>

# 编辑 Secretkubectl edit secret <secret-name> -n <namespace>

# 删除 Secretkubectl delete secret <secret-name> -n <namespace>

端口转发

bash 复制代码
# 转发 Pod 端口到本地
kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace>

# 转发 Service 端口到本地
kubectl port-forward service/<service-name> <local-port>:<service-port> -n <namespace>

# 转发 Deployment 端口到本地
kubectl port-forward deployment/<deployment-name> <local-port>:<container-port> -n <namespace>

# 示例:转发 MySQL 服务
kubectl port-forward service/mysql 3306:3306 -n default

执行命令

bash 复制代码
# 在 Pod 中执行命令
kubectl exec <pod-name> -n <namespace> -- <command>

# 在 Pod 中执行交互式命令
kubectl exec -it <pod-name> -n <namespace> -- <command>

# 在特定容器中执行命令
kubectl exec <pod-name> -c <container-name> -n <namespace> -- <command>

# 示例
kubectl exec my-pod -- ls /tmp
kubectl exec -it my-pod -- /bin/bash

标签和选择器

bash 复制代码
# 查看资源标签
kubectl get pods --show-labels

# 给资源添加标签
kubectl label pod <pod-name> app=myapp -n <namespace>

# 更新标签
kubectl label pod <pod-name> app=newapp --overwrite -n <namespace>

# 删除标签
kubectl label pod <pod-name> app- -n <namespace>

# 使用标签选择器查询
kubectl get pods -l app=myapp -n <namespace>
kubectl get pods -l 'app in (myapp,yourapp)' -n <namespace>
kubectl get pods -l 'app=myapp,version=v1' -n <namespace>

滚动更新

bash 复制代码
# 查看滚动更新状态
kubectl rollout status deployment/<deployment-name> -n <namespace>

# 查看滚动更新历史
kubectl rollout history deployment/<deployment-name> -n <namespace>

# 回滚到上一个版本
kubectl rollout undo deployment/<deployment-name> -n <namespace>

# 回滚到指定版本
kubectl rollout undo deployment/<deployment-name> --to-revision=<revision-number> -n <namespace>

# 暂停滚动更新
kubectl rollout pause deployment/<deployment-name> -n <namespace>

# 恢复滚动更新
kubectl rollout resume deployment/<deployment-name> -n <namespace>

# 扩展 Deploymentkubectl scale deployment/<deployment-name> --replicas=<number> -n <namespace>

资源描述

bash 复制代码
# 描述 Podkubectl describe pod <pod-name> -n <namespace>

# 描述 Deploymentkubectl describe deployment <deployment-name> -n <namespace>

# 描述 Servicekubectl describe service <service-name> -n <namespace>

# 描述 Nodekubectl describe node <node-name>

# 描述命名空间
kubectl describe namespace <namespace-name>

事件查看

bash 复制代码
# 查看所有事件
kubectl get events -n <namespace>

# 查看所有命名空间的事件
kubectl get events --all-namespaces

# 按时间排序查看事件
kubectl get events -n <namespace> --sort-by='.lastTimestamp'

# 查看特定资源的事件
kubectl get events --field-selector involvedObject.name=<pod-name> -n <namespace>

# 实时监控事件
kubectl get events -n <namespace> --watch

命名空间管理

bash 复制代码
# 查看所有命名空间
kubectl get namespaces
kubectl get ns

# 查看命名空间详细信息
kubectl get namespace <namespace-name> -o yaml

# 创建命名空间
kubectl create namespace <namespace-name>

# 删除命名空间
kubectl delete namespace <namespace-name>

# 切换到命名空间(使用 kubectx/kubens 工具)
# 或者使用上下文
kubectl config set-context --current --namespace=<namespace-name>

上下文和配置

bash 复制代码
# 查看当前上下文
kubectl config current-context

# 查看所有上下文
kubectl config get-contexts

# 切换上下文
kubectl config use-context <context-name>

# 查看配置
kubectl config view

# 查看特定上下文的配置
kubectl config view --context=<context-name>

# 设置默认命名空间
kubectl config set-context --current --namespace=<namespace-name>

# 查看集群信息
kubectl config view --minify

# 重命名上下文
kubectl config rename-context <old-name> <new-name>

实用技巧

  1. 输出格式
bash 复制代码
# JSON 格式
kubectl get pods -o json

# YAML 格式
kubectl get pods -o yaml

# 自定义列
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

# JSONPath
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# 表格格式(默认)
kubectl get pods
  1. 过滤和搜索
bash 复制代码
# 按标签过滤
kubectl get pods -l app=myapp

# 按字段选择器过滤
kubectl get pods --field-selector status.phase=Running

# 搜索资源
kubectl get all | grep <keyword>
  1. 批量操作
bash 复制代码
# 删除所有命名空间下的所有 Podkubectl delete pods --all --all-namespaces

# 删除所有命名空间下的所有 Deploymentkubectl delete deployments --all --all-namespaces

# 删除命名空间下的所有资源
kubectl delete all --all -n <namespace>
  1. 导出资源
bash 复制代码
# 导出资源为 YAMLkubectl get pod <pod-name> -n <namespace> -o yaml > pod.yaml

# 导出资源为 JSONkubectl get pod <pod-name> -n <namespace> -o json > pod.json

# 导出并移除集群特定信息
kubectl get pod <pod-name> -n <namespace> -o yaml --export > pod.yaml
  1. 补丁操作
bash 复制代码
# 使用 JSON 补丁
kubectl patch pod <pod-name> -p '{"spec":{"containers":[{"name":"mycontainer","image":"newimage"}]}}'

# 使用策略性合并补丁
kubectl patch deployment <deployment-name> -p '{"spec":{"replicas":3}}'

# 使用 JSON 补丁文件
kubectl patch pod <pod-name> --patch-file=patch.json
  1. 常用组合命令
bash 复制代码
# 查看 Pod 并显示标签
kubectl get pods --show-labels -n <namespace>

# 查看所有命名空间的资源
kubectl get all --all-namespaces

# 查看资源并持续监控
kubectl get pods -w -n <namespace>

# 查看资源并排序
kubectl get pods --sort-by=.metadata.creationTimestamp -n <namespace>

# 查看资源使用情况
kubectl top pods --sort-by=memory -n <namespace>

# 查看资源并过滤
kubectl get pods -n <namespace> | grep <keyword>

# 查看资源并导出
kubectl get deployment <deployment-name> -n <namespace> -o yaml > deployment.yaml
  1. 故障排查命令
bash 复制代码
# 查看 Pod 状态和事件
kubectl describe pod <pod-name> -n <namespace>

# 查看 Pod 日志
kubectl logs <pod-name> -n <namespace> --previous

# 查看所有命名空间的错误 Podkubectl get pods --all-namespaces --field-selector status.phase!=Running

# 查看节点资源使用
kubectl top nodes

# 查看集群事件
kubectl get events --all-namespaces --sort-by='.lastTimestamp'

# 检查 API 服务器连接
kubectl cluster-info dump

# 查看资源配额
kubectl describe quota -n <namespace>

注意事项

  1. 命名空间:大多数命令需要指定 -n 或 --namespace=,如果不指定则使用默认命名空间(通常是 default)
  2. 资源名称:可以使用完整名称或简写形式,例如:
    2.1: pods 或 po
    2.2: services 或 svc
    2.3: deployments 或 deploy
    2.4: namespaces 或 ns
  3. 输出格式:使用 -o 参数指定输出格式,常用格式包括:
    2.1: yaml
    2.2: json
    2.3: wide
    2.4: custom-columns
  4. 强制删除:删除资源时,如果 Pod 处于 Terminating 状态,可以使用:
    bash kubectl delete pod pod-name --force --grace-period=0 -n namespace
  5. 权限:某些命令可能需要相应的 RBAC 权限,确保当前用户有足够的权限执行操作
相关推荐
zzzzzz31016 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode16 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
武子康1 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz4 天前
Maven依赖冲突
java·服务器·maven
Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智4 天前
ARP代理--工作原理
运维·网络·arp·arp代理