Kubernetes异常排查方式

集群信息:

bash 复制代码
1. 显示 Kubernetes 版本:kubectl version
2. 显示集群信息:kubectl cluster-info
3. 列出集群中的所有节点:kubectl get nodes
4. 查看一个具体的节点详情:kubectl describe node <node-name>
5. 列出所有命名空间:kubectl get namespaces
6. 列出所有命名空间中的所有 pod:kubectl get pods --all-namespaces

Pod 诊断:

bash 复制代码
1. 列出特定命名空间中的 pod:kubectl get pods -n <namespace>
2. 查看一个 Pod 详情:kubectl describe pod <pod-name> -n <namespace>
3. 查看 Pod 日志:kubectl logs <pod-name> -n <namespace>
4. 尾部 Pod 日志:kubectl logs -f <pod-name> -n <namespace>
5. 在 pod 中执行命令:kubectl exec -it <pod-name> -n <namespace> -- <command>

Pod 健康检查:

bash 复制代码
1. 检查 Pod 准备情况:kubectl get pods <pod-name> -n <namespace> -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'
2. 检查 Pod 事件:kubectl get events -n <namespace> --field-selector involvedObject.name=<pod-name>

Service诊断:

bash 复制代码
1. 列出命名空间中的所有服务:kubectl get svc -n <namespace>
2. 查看一个服务详情:kubectl describe svc <service-name> -n <namespace>

Deployment诊断:

bash 复制代码
1. 列出命名空间中的所有Deployment:kubectl get deployments -n <namespace>
2. 查看一个Deployment详情:kubectl describe deployment <deployment-name> -n <namespace>
3. 查看滚动发布状态:kubectl rollout status deployment/<deployment-name> -n <namespace>
4. 查看滚动发布历史记录:kubectl rollout history deployment/<deployment-name> -n <namespace>

StatefulSet诊断:

bash 复制代码
1. 列出命名空间中的所有 StatefulSet:kubectl get statefulsets -n <namespace>
2. 查看一个 StatefulSet详情:kubectl describe statefulset <statefulset-name> -n <namespace>

ConfigMap 和Secret诊断:

bash 复制代码
1. 列出命名空间中的 ConfigMap:kubectl get configmaps -n <namespace>
2. 查看一个ConfigMap详情:kubectl describe configmap <configmap-name> -n <namespace>
3. 列出命名空间中的 Secret:kubectl get secrets -n <namespace>
4. 查看一个Secret详情:kubectl describe secret <secret-name> -n <namespace>

命名空间诊断:

bash 复制代码
1. 查看一个命名空间详情:kubectl describe namespace <namespace-name>

资源使用情况:

bash 复制代码
1. 检查 pod 的资源使用情况:kubectl top pod <pod-name> -n <namespace>
2. 检查节点资源使用情况:kubectl top nodes

网络诊断:

bash 复制代码
1. 显示命名空间中 Pod 的 IP 地址:kubectl get pods -n <namespace> -o custom-columns=POD:metadata.name,IP:status.podIP --no-headers
2. 列出命名空间中的所有网络策略:kubectl get networkpolicies -n <namespace>
3. 查看一个网络策略详情:kubectl describe networkpolicy <network-policy-name> -n <namespace>

持久卷 (PV) 和持久卷声明 (PVC) 诊断:

bash 复制代码
1. 列出PV:kubectl get pv
2. 查看一个PV详情:kubectl describe pv <pv-name>
3. 列出命名空间中的 PVC:kubectl get pvc -n <namespace>
4. 查看PVC详情:kubectl describe pvc <pvc-name> -n <namespace>

节点诊断:

bash 复制代码
1. 获取特定节点上运行的 Pod 列表:kubectl get pods --field-selector spec.nodeName=<node-name> -n <namespace>

资源配额和限制:

bash 复制代码
1. 列出命名空间中的资源配额:kubectl get resourcequotas -n <namespace>
2. 查看一个资源配额详情:kubectl describe resourcequota <resource-quota-name> -n <namespace>

自定义资源定义 (CRD) 诊断:

bash 复制代码
1. 列出命名空间中的自定义资源:kubectl get <custom-resource-name> -n <namespace>
2. 查看自定义资源详情:kubectl describe <custom-resource-name> <custom-resource-instance-name> -n <namespace>

资源伸缩和自动伸缩

bash 复制代码
1. Deployment伸缩:kubectl scale deployment <deployment-name> --replicas=<replica-count> -n <namespace>
2. 设置Deployment的自动伸缩:kubectl autoscale deployment <deployment-name> --min=<min-pods> --max=<max-pods> --cpu-percent=<cpu-percent> -n <namespace>
3. 检查水平伸缩器状态:kubectl get hpa -n <namespace>

作业和 CronJob 诊断:

bash 复制代码
1. 列出命名空间中的所有作业:kubectl get jobs -n <namespace>
2. 查看一份工作详情:kubectl describe job <job-name> -n <namespace>
3. 列出命名空间中的所有 cron 作业:kubectl get cronjobs -n <namespace>
4. 查看一个 cron 作业详情:kubectl describe cronjob <cronjob-name> -n <namespace>

容量诊断:

bash 复制代码
1. 列出按容量排序的持久卷 (PV):kubectl get pv --sort-by=.spec.capacity.storage
2. 查看PV回收策略:kubectl get pv <pv-name> -o=jsonpath='{.spec.persistentVolumeReclaimPolicy}'
3. 列出所有存储类别:kubectl get storageclasses

Ingress和服务网格诊断:

bash 复制代码
1. 列出命名空间中的所有Ingress:kubectl get ingress -n <namespace>
2. 查看一个Ingress详情:kubectl describe ingress <ingress-name> -n <namespace>
3. 列出命名空间中的所有 VirtualServices (Istio):kubectl get virtualservices -n <namespace>
4. 查看一个 VirtualService (Istio)详情:kubectl describe virtualservice <virtualservice-name> -n <namespace>

Pod 网络故障排除:

bash 复制代码
1. 运行网络诊断 Pod(例如 busybox)进行调试:kubectl run -it --rm --restart=Never --image=busybox net-debug-pod -- /bin/sh
2. 测试从 Pod 到特定端点的连接:kubectl exec -it <pod-name> -n <namespace> -- curl <endpoint-url>
3. 跟踪从一个 Pod 到另一个 Pod 的网络路径:kubectl exec -it <source-pod-name> -n <namespace> -- traceroute <destination-pod-ip>
4. 检查 Pod 的 DNS 解析:kubectl exec -it <pod-name> -n <namespace> -- nslookup <domain-name>
相关推荐
orion572 天前
Missing Semester Class1:course overview and introduction of shell
linux
用户120487221612 天前
Linux驱动编译与加载
linux·嵌入式
用户805533698032 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房3 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia4 天前
linux curl命令详解_curl详解
linux
扛枪的书生4 天前
Linux 网络管理器用法速查
linux
顺风尿一寸4 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
lichenyang4534 天前
Docker 学习笔记(四):Dockerfile,把项目打成自己的镜像
docker·容器
lichenyang4534 天前
Docker 学习笔记(三):Docker 网络、bridge、子网和容器互通
docker·容器