kubectl - 集群管理命令行工具
kubectl 基本结构
bash
kubectl [command] [TYPE] [NAME] [flags]command:子命令,如 get、describe、create、delete 等。
TYPE:资源类型,如 pod、deployment、service 等,支持复数形式、简写及大小写不敏感。
NAME:资源名称,可指定单个或多个资源。
flags:命令行参数,用于控制命令行为。
kubectl 基础命令汇总
kubectl 是 Kubernetes 的命令行工具,用于与 Kubernetes 集群的 API Server 进行交互。本文档汇总了最常用的 kubectl 命令,按照使用场景分类,便于学习和查阅。
1. 环境配置与上下文管理
| 命令 | 说明 |
|---|---|
kubectl config view | 查看当前 kubectl 配置(kubeconfig 内容) |
kubectl config get-contexts | 列出所有可用上下文 |
kubectl config current-context | 查看当前使用的上下文 |
kubectl config use-context <名称> | 切换到指定上下文 |
kubectl config set-credentials <名称> --username=<用户名> --password=<密码> | 设置用户凭证 |
kubectl config set-cluster <名称> --server=<API Server地址> | 设置集群信息 |
kubectl config set-context <名称> --cluster=<集群名> --user=<用户名> | 创建或修改上下文 |
kubectl config unset <属性> | 删除配置中的某个属性 |
2. 资源基础操作(增删改查)
2.1 创建资源
bash
# 使用 YAML/JSON 文件创建资源
kubectl create -f <文件名.yaml>
# 创建资源(不通过文件,直接指定参数)
kubectl create deployment nginx --image=nginx
kubectl create namespace test
kubectl create configmap my-config --from-literal=key=value
kubectl create secret generic my-secret --from-literal=password=123
# 使用 apply 声明式创建/更新(推荐)
kubectl apply -f <文件名.yaml>
kubectl apply -f <目录> # 应用目录下所有 .yaml 文件
kubectl apply -f <URL> # 应用远程配置文件2.2 查看资源
bash
# 获取资源列表
kubectl get pods
kubectl get pods -o wide # 显示更多信息(IP、节点等)
kubectl get pods -n <命名空间>
kubectl get pods --all-namespaces # 或 kubectl get pods -A
kubectl get pods -l app=nginx # 标签选择器
kubectl get nodes # 查看节点
kubectl get deployments,services # 查看多种资源
kubectl get all # 查看大多数资源(不包含所有)
# 查看资源详情
kubectl describe pod <pod名>
kubectl describe node <节点名>
# 查看资源定义(yaml或json格式)
kubectl get pod <pod名> -o yaml
kubectl get pod <pod名> -o json
kubectl get pod <pod名> -o jsonpath='{.status.phase}'
# 监视资源变化
kubectl get pods -w # watch模式2.3 更新资源
bash
# 直接编辑资源
kubectl edit deployment/<名称>
# 修改镜像版本等字段(无需编辑文件)
kubectl set image deployment/nginx-deploy nginx=nginx:1.22
# 扩缩容
kubectl scale deployment/nginx-deploy --replicas=3
# 打标签
kubectl label pods <pod名> env=prod
kubectl label pods <pod名> env- # 删除标签2.4 删除资源
bash
# 删除指定资源
kubectl delete pod <pod名>
kubectl delete -f <文件名.yaml> # 删除文件中定义的资源
kubectl delete deployment,service -l app=nginx # 按标签删除
# 删除命名空间下所有资源(保留命名空间)
kubectl delete all --all -n <命名空间>
# 优雅删除
kubectl delete pod <pod名> --grace-period=0 --force # 强制删除3. Pod 调试与交互
bash
# 查看Pod日志
kubectl logs <pod名>
kubectl logs <pod名> -c <容器名> # 多容器Pod指定容器
kubectl logs -f <pod名> # 实时跟踪日志
kubectl logs --previous <pod名> # 查看上一个容器的日志(崩溃时)
# 进入Pod内部执行命令
kubectl exec -it <pod名> -- /bin/bash
kubectl exec -it <pod名> -c <容器名> -- sh
kubectl exec <pod名> -- ls /app
# 端口转发(从本地访问Pod端口)
kubectl port-forward pod/<pod名> 本地端口:Pod端口
# 示例:kubectl port-forward pod/nginx 8080:80
# 复制文件
kubectl cp <本地路径> <pod名>:<容器内路径>
kubectl cp <pod名>:<容器内路径> <本地路径>
# 创建临时测试Pod
kubectl run test-pod --image=busybox -it --rm -- sh4. 命名空间操作
bash
# 查看命名空间
kubectl get namespaces
# 创建命名空间
kubectl create namespace <名称>
# 删除命名空间(会删除其下所有资源)
kubectl delete namespace <名称>
# 设置默认命名空间(不修改kubeconfig,仅适用于当前命令)
kubectl config set-context --current --namespace=<命名空间>5. 节点与集群管理
bash
# 查看集群信息
kubectl cluster-info
kubectl cluster-info dump
# 查看节点资源使用(需要 metrics-server)
kubectl top nodes
kubectl top pods -A
# 标记节点
kubectl label nodes <节点名> disktype=ssd
kubectl taint nodes <节点名> key=value:NoSchedule # 添加污点
kubectl taint nodes <节点名> key:NoSchedule- # 删除污点
# 节点维护(驱逐Pod并标记不可调度)
kubectl drain <节点名> --ignore-daemonsets
kubectl cordon <节点名> # 仅标记不可调度
kubectl uncordon <节点名> # 恢复可调度6. 常用资源类型简写
| 资源类型 | 简写 | 完整名称 |
|---|---|---|
| Pod | po | pods |
| Deployment | deploy | deployments |
| ReplicaSet | rs | replicasets |
| StatefulSet | sts | statefulsets |
| DaemonSet | ds | daemonsets |
| Service | svc | services |
| ConfigMap | cm | configmaps |
| Secret | secret | secrets |
| Namespace | ns | namespaces |
| Node | no | nodes |
| PersistentVolume | pv | persistentvolumes |
| PersistentVolumeClaim | pvc | persistentvolumeclaims |
| Ingress | ing | ingresses |
| Job | job | jobs |
| CronJob | cj | cronjobs |
7. 常用选项与技巧
bash
# 指定命名空间(-n)
kubectl get pods -n kube-system
# 所有命名空间(-A 或 --all-namespaces)
kubectl get pods -A
# 输出格式(-o)
kubectl get pods -o wide # 额外列(IP、节点)
kubectl get pods -o yaml # YAML格式
kubectl get pods -o json # JSON格式
kubectl get nodes -o name # 仅资源名称(例如 node/node1)
# 标签选择器(-l)
kubectl get pods -l app=nginx,env=prod
kubectl get pods -l 'app in (nginx,redis)'
# 字段选择器(--field-selector)
kubectl get pods --field-selector status.phase=Running
# 递归处理目录(-R)
kubectl apply -R -f configs/
# 查看事件
kubectl get events --sort-by='.lastTimestamp'
kubectl get events -w8. 常用别名配置(可选)
为了提升效率,可以在 shell 配置文件中添加别名:
bash
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgd='kubectl get deployment'
alias kgs='kubectl get svc'
alias kgn='kubectl get nodes'
alias kd='kubectl describe'
alias kdel='kubectl delete'
alias klog='kubectl logs -f'
alias kex='kubectl exec -it'9. 常用命令速查表
| 操作 | 命令示例 |
|---|---|
| 查看所有Pod | kubectl get pods -A |
| 查看Pod详情 | kubectl describe pod <pod名> |
| 查看日志 | kubectl logs -f <pod名> -c <容器名> |
| 进入Pod shell | kubectl exec -it <pod名> -- /bin/sh |
| 创建资源 | kubectl apply -f manifest.yaml |
| 删除资源 | kubectl delete -f manifest.yaml |
| 更新镜像 | kubectl set image deployment/<名称> <容器名>=<新镜像> |
| 扩缩容 | kubectl scale deployment/<名称> --replicas=5 |
| 端口转发 | kubectl port-forward pod/<pod名> 8080:80 |
| 切换命名空间 | kubectl config set-context --current --namespace=<命名空间> |
| 查看节点资源 | kubectl top nodes |
| 查看集群信息 | kubectl cluster-info |
10. 帮助与自动补全
bash
# 查看帮助
kubectl --help
kubectl get --help
kubectl explain pod # 查看资源字段定义
# 启用 kubectl 自动补全(bash)
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
# (zsh)
source <(kubectl completion zsh)
echo "autoload -U compinit; compinit" >> ~/.zshrc
echo "source <(kubectl completion zsh)" >> ~/.zshrc以上命令涵盖了日常运维和开发中最常使用的 kubectl 操作。建议结合实际集群环境多加练习,熟练掌握后可大幅提升效率。
