# 查看所有资源
kubectl get all
# 查看指定命名空间资源
kubectl get all -n<namespace># 查看所有命名空间的资源
kubectl get all --all-namespaces
kubectl get all -A# 查看 Pod
kubectl get pods
kubectl get pods -o wide
kubectl get pods --show-labels
# 查看 Deployment
kubectl get deployments
kubectl get deploy
# 查看 Service
kubectl get services
kubectl get svc
# 查看 ConfigMap
kubectl get configmaps
kubectl get cm
# 查看 Secret
kubectl get secrets
# 查看 PersistentVolumeClaim
kubectl get pvc
# 查看 PersistentVolume
kubectl get pv# 查看节点
kubectl get nodes
kubectl get nodes -o wide
# 查看命名空间
kubectl get namespaces
kubectl get ns
# 查看 Ingress
kubectl get ingress
kubectl get ing
# 查看事件
kubectl get events
kubectl get events --sort-by='.lastTimestamp'
查看资源详情
# 查看 Pod 详情
kubectl describe pod <pod-name># 查看 Deployment 详情
kubectl describe deployment <deployment-name># 查看 Service 详情
kubectl describe service<service-name># 查看节点详情
kubectl describe node<node-name># 查看事件详情
kubectl describe events
Pod 操作
创建和管理 Pod
# 创建 Pod
kubectl apply -f pod.yaml
# 快速创建测试 Pod
kubectl run test-pod --image=nginx --restart=Never
# 创建交互式 Pod
kubectl run -it--rm test-pod --image=busybox -- sh# 删除 Pod
kubectl delete pod <pod-name># 强制删除 Pod
kubectl delete pod <pod-name>--force --grace-period=0# 删除所有 Pod
kubectl delete pods --all
# 查看 Pod 事件
kubectl describe pod <pod-name># 查看集群事件
kubectl get events --sort-by='.lastTimestamp'# 查看指定命名空间事件
kubectl get events -n<namespace># 查看 Pod 资源使用
kubectl top pod <pod-name># 查看节点资源使用
kubectl top nodes
# 查看 Pod 资源请求和限制
kubectl describe pod <pod-name>|grep-A5"Limits\|Requests"
调试 Pod
# 创建调试 Pod
kubectl run -it--rm debug --image=busybox -- sh# 创建调试 Pod(带网络工具)
kubectl run -it--rm debug --image=nicolaka/netshoot -- bash# 测试 DNS 解析
kubectl run -it--rm debug --image=busybox -- nslookup kubernetes
# 测试服务连通性
kubectl run -it--rm debug --image=busybox -- nc-zv<service-name><port>
kubectl run -it--rm debug --image=busybox -- wget -qO- http://<service-name>:<port># 测试外部连接
kubectl run -it--rm debug --image=busybox -- ping google.com
kubectl run -it--rm debug --image=curlimages/curl -- curl-v http://example.com
网络相关
网络策略
# 查看网络策略
kubectl get networkpolicies
kubectl get netpol
# 查看网络策略详情
kubectl describe networkpolicy <name># 创建网络策略
kubectl apply -f networkpolicy.yaml
DNS 相关
# 查看 CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
# 查看 CoreDNS 配置
kubectl get configmap coredns -n kube-system -o yaml
# 测试 DNS
kubectl run -it--rm debug --image=busybox -- nslookup kubernetes
kubectl run -it--rm debug --image=busybox -- nslookup<service-name>.<namespace>.svc.cluster.local
Ingress
# 查看 Ingress
kubectl get ingress
kubectl get ing
# 查看 Ingress 详情
kubectl describe ingress <name># 创建 Ingress
kubectl apply -f ingress.yaml
# 删除 Ingress
kubectl delete ingress <name>
节点管理
节点状态
# 查看节点
kubectl get nodes
# 查看节点详情
kubectl get nodes -o wide
# 查看节点详情
kubectl describe node<node-name># 查看节点资源使用
kubectl top nodes
# 查看节点标签
kubectl get nodes --show-labels
# JSON 格式
kubectl get pods -o json
# YAML 格式
kubectl get pods -o yaml
# 宽格式(显示更多信息)
kubectl get pods -o wide
# 自定义列
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
# 仅显示名称
kubectl get pods -o name
# 显示标签
kubectl get pods --show-labels
# 按标签过滤
kubectl get pods -lapp=nginx
kubectl get pods -l'app in (nginx,redis)'
快捷别名
# 添加到 ~/.bashrc 或 ~/.zshrcaliask='kubectl'aliaskg='kubectl get'aliaskd='kubectl describe'aliaskdel='kubectl delete'aliaska='kubectl apply -f'aliaskl='kubectl logs'aliaske='kubectl exec -it'aliaskpf='kubectl port-forward'# 缩写
kubectl get po # pod
kubectl get svc # service
kubectl get deploy # deployment
kubectl get cm # configmap
kubectl get ns # namespace
kubectl get ing # ingress
kubectl get pvc # persistentvolumeclaim
kubectl get pv# persistentvolume
kubectl get sa # serviceaccount
批量操作
# 删除所有 Evicted 状态的 Pod
kubectl get pods --all-namespaces --field-selector=status.phase=Failed -o json | kubectl delete -f -
# 删除指定标签的所有 Pod
kubectl delete pods -lapp=nginx
# 批量重启 Deployment
kubectl rollout restart deployment -lapp=nginx
# 批量查看多个资源
kubectl get pods,svc,deploy
Pod 故障排查命令
查看 Pod 状态
# 查看所有 Pod 状态
kubectl get pods -n<namespace># 查看 Pod 详细状态
kubectl get pods -n<namespace>-o wide
# 查看特定 Pod
kubectl get pod <pod-name>-n<namespace>
查看 Pod 失败原因
# 查看 Pod 详情(包含事件)
kubectl describe pod <pod-name>-n<namespace># 示例:查看 MySQL Pod 失败原因
kubectl describe pod data-collection-mysql-549fc85557-4hzpr -n data-collection
# 只查看最后 20 行(快速定位问题)
kubectl describe pod <pod-name>-n<namespace>|tail-20
查看事件
# 查看命名空间所有事件
kubectl get events -n<namespace># 按时间排序查看事件
kubectl get events -n<namespace> --sort-by='.lastTimestamp'# 查看所有命名空间事件
kubectl get events -A# 持续监控事件
kubectl get events -n<namespace>--watch
# 查看节点状态
kubectl get nodes
# 查看节点详情
kubectl describe node<node-name># 查看节点资源使用
kubectl top nodes
# 查看节点标签
kubectl get nodes --show-labels
Pod Pending 常见原因
原因
排查命令
解决方案
PVC 未绑定
kubectl get pvc -n <ns>
检查 StorageClass 是否存在
存储类不存在
kubectl get storageclass
创建 StorageClass 或修改配置
节点资源不足
kubectl describe node <node>
释放资源或添加节点
节点选择器不匹配
kubectl get nodes --show-labels
添加节点标签或修改 Pod 配置
污点/容忍问题
kubectl describe node <node> | grep Taints
添加容忍或移除污点
快速诊断脚本
# 一键诊断 Pod 问题#!/bin/bashNAMESPACE="data-collection"POD_NAME="data-collection-mysql-549fc85557-4hzpr"echo"=== Pod Status ==="
kubectl get pod $POD_NAME-n$NAMESPACE-o wide
echo-e"\n=== Pod Events ==="
kubectl describe pod $POD_NAME-n$NAMESPACE|grep-A20"Events:"echo-e"\n=== PVC Status ==="
kubectl get pvc -n$NAMESPACEecho-e"\n=== Node Status ==="
kubectl get nodes
echo-e"\n=== Recent Events ==="
kubectl get events -n$NAMESPACE --sort-by='.lastTimestamp'|tail-10
常见问题排查
Pod 一直处于 Pending 状态
# 查看 Pod 事件
kubectl describe pod <pod-name># 常见原因:# 1. 资源不足:节点 CPU/内存不足# 2. PVC 未绑定:存储卷问题# 3. 节点选择器不匹配:标签问题# 4. 污点/容忍:节点污点问题
# 检查 CoreDNS
kubectl get pods -n kube-system -l k8s-app=kube-dns
# 检查 CoreDNS 日志
kubectl logs -n kube-system <coredns-pod># 测试 DNS
kubectl run -it--rm debug --image=busybox -- nslookup kubernetes
Service 无法访问
# 检查 Service 端点
kubectl get endpoints <service-name># 检查 Pod 标签
kubectl get pods --show-labels
# 检查 Service 选择器
kubectl describe service<service-name># 测试服务连通性
kubectl run -it--rm debug --image=busybox -- nc-zv<service-name><port>
# Pod 一直处于 Pending 状态
kubectl get pods -n data-collection
NAME READY STATUS RESTARTS AGE
data-collection-backend-5df6f9bc68-gdx5p 0/1 Pending 0 16m
data-collection-mysql-549fc85557-4hzpr 0/1 Pending 0 16m
# 事件显示 StorageClass 不存在
kubectl get events -n data-collection
Warning ProvisioningFailed persistentvolumeclaim/mysql-pvc storageclass.storage.k8s.io "standard" not found
Warning FailedScheduling pod/data-collection-mysql pod has unbound immediate PersistentVolumeClaims
排查步骤
# 1. 查看 Pod 状态
kubectl get pods -n data-collection
# 2. 查看事件
kubectl get events -n data-collection --sort-by='.lastTimestamp'# 3. 查看 PVC 状态
kubectl get pvc -n data-collection
# 4. 查看 StorageClass(关键)
kubectl get storageclass
kubectl get sc
# 5. 查看 PVC 详情
kubectl describe pvc mysql-pvc -n data-collection
解决方案
方案1:查看并使用已有的 StorageClass
# 查看已有 StorageClass
kubectl get sc
# 输出示例:# NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE# local-path rancher.io/local-path Delete WaitForFirstConsumer# longhorn driver.longhorn.io Delete Immediate# 修改 PVC 配置使用已有的 StorageClass# 将 storageClassName: standard 改为 storageClassName: local-path