常用 kubectl 命令操作
常用 kubectl 命令操作
Section titled “常用 kubectl 命令操作”- 功能名称: Kubernetes 对象常用操作
- 主要工具:
kubectl - 适用对象: Pod、Deployment、Service、ConfigMap、Secret、Ingress、Job
- 文档更新时间: 2026-06-17
- Agent 友好度: ⭐⭐⭐⭐⭐
kubectl 是 Kubernetes 对象操作的统一入口。本文提供 TKE 集群中最常用的对象查看、部署、更新、删除和排障命令,覆盖日常交付与运维场景。
# 查看常用资源kubectl get pod,deploy,svc,ingress
# 查看所有命名空间下的 Podkubectl get pods -A -o wide
# 查看指定命名空间资源kubectl get deploy,svc -n <namespace>
# 查看对象详情kubectl describe pod <pod-name> -n <namespace>
# 查看事件kubectl get events -n <namespace> --sort-by='.lastTimestamp'
# 查看对象 YAMLkubectl get deployment <name> -n <namespace> -o yaml创建和应用对象
Section titled “创建和应用对象”使用 YAML 声明式部署
Section titled “使用 YAML 声明式部署”kubectl apply -f app.yamlkubectl apply -f ./manifests/部署前查看差异
Section titled “部署前查看差异”kubectl diff -f app.yaml快速创建 Deployment
Section titled “快速创建 Deployment”kubectl create deployment nginx \ --image=nginx:1.25 \ --replicas=3快速暴露 Service
Section titled “快速暴露 Service”kubectl expose deployment nginx \ --port=80 \ --target-port=80 \ --type=ClusterIP创建 ConfigMap 和 Secret
Section titled “创建 ConfigMap 和 Secret”kubectl create configmap app-config \ --from-literal=ENV=production \ --dry-run=client -o yaml | kubectl apply -f -
kubectl create secret generic app-secret \ --from-literal=password='change-me' \ --dry-run=client -o yaml | kubectl apply -f -kubectl set image deployment/nginx nginx=nginx:1.26kubectl rollout status deployment/nginxkubectl scale deployment/nginx --replicas=5kubectl get pods -l app=nginx -w局部 patch
Section titled “局部 patch”kubectl patch deployment nginx \ -p '{"spec":{"template":{"metadata":{"annotations":{"restartedAt":"'$(date -Iseconds)'"}}}}}'kubectl edit deployment nginx# 查看发布状态kubectl rollout status deployment/nginx
# 查看发布历史kubectl rollout history deployment/nginx
# 回滚到上一版本kubectl rollout undo deployment/nginx
# 回滚到指定版本kubectl rollout undo deployment/nginx --to-revision=2
# 暂停和恢复发布kubectl rollout pause deployment/nginxkubectl rollout resume deployment/nginx# 按文件删除kubectl delete -f app.yaml
# 删除指定对象kubectl delete deployment nginxkubectl delete service nginx
# 按标签删除kubectl delete pod -l app=nginx
# 删除命名空间内所有同类对象kubectl delete job --all -n <namespace>!!! warning “删除前确认” 删除 Service、Ingress、LoadBalancer 类型资源可能影响访问入口;删除 Deployment 会级联删除其管理的 Pod。生产环境建议先导出 YAML 备份。
# 查看日志kubectl logs <pod-name> -n <namespace>
# 查看上一次崩溃日志kubectl logs <pod-name> -n <namespace> --previous
# 按标签查看日志kubectl logs -l app=nginx -n <namespace> --tail=100
# 进入容器kubectl exec -it <pod-name> -n <namespace> -- /bin/sh
# 端口转发kubectl port-forward deployment/nginx 8080:80
# 启动临时调试 Podkubectl run debug-shell \ --rm -it \ --restart=Never \ --image=busybox:1.36 \ -- shPod Pending
Section titled “Pod Pending”kubectl describe pod <pod-name> -n <namespace>kubectl get nodeskubectl describe node <node-name>重点检查资源不足、节点污点、亲和性、PVC 绑定和调度事件。
ImagePullBackOff
Section titled “ImagePullBackOff”kubectl describe pod <pod-name> -n <namespace>kubectl get secret -n <namespace>重点检查镜像地址、镜像标签、TCR 访问权限和 imagePullSecrets。
CrashLoopBackOff
Section titled “CrashLoopBackOff”kubectl logs <pod-name> -n <namespace> --previouskubectl describe pod <pod-name> -n <namespace>重点检查启动命令、环境变量、配置文件、探针和依赖服务。
Service Endpoints 为空
Section titled “Service Endpoints 为空”kubectl describe service <service-name> -n <namespace>kubectl get endpoints <service-name> -n <namespace>kubectl get pods --show-labels -n <namespace>重点检查 Service selector 是否匹配 Pod labels。
常用对象速查
Section titled “常用对象速查”| 对象 | 查看 | 常用操作 |
|---|---|---|
| Pod | kubectl get pods -A | logs、exec、describe |
| Deployment | kubectl get deploy | set image、scale、rollout |
| Service | kubectl get svc | expose、describe、检查 Endpoints |
| ConfigMap | kubectl get cm | create configmap、apply |
| Secret | kubectl get secret | create secret、imagePullSecrets |
| Ingress | kubectl get ingress | 检查规则、证书和后端 Service |
| Job | kubectl get job | 查看完成状态和 Pod 日志 |