
这篇文章是关于使用 Kubectl 进行 Kubernetes 诊断的指南。 列出了 100 个 Kubectl 命令,这些命令对于诊断 Kubernetes 集群中的问题非常有用。这些问题包括但不限于:
集群信息:
kubectl versionkubectl cluster-infokubectl get nodeskubectl describe node <node-name>kubectl get namespaceskubectl get pods --all-namespacesPod 诊断:
kubectl get pods -n <namespace>kubectl describe pod <pod-name> -n <namespace>kubectl logs <pod-name> -n <namespace>kubectl logs -f <pod-name> -n <namespace>kubectl exec -it <pod-name> -n <namespace> -- <command>Pod 健康检查:
kubectl get pods <pod-name> -n <namespace> -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}'kubectl get events -n <namespace> --field-selector involvedObject.name=<pod-name>Service诊断:
kubectl get svc -n <namespace>kubectl describe svc <service-name> -n <namespace>Deployment诊断:
kubectl get deployments -n <namespace>kubectl describe deployment <deployment-name> -n <namespace>kubectl rollout status deployment/<deployment-name> -n <namespace>kubectl rollout history deployment/<deployment-name> -n <namespace>StatefulSet诊断:
kubectl get statefulsets -n <namespace>kubectl describe statefulset <statefulset-name> -n <namespace>ConfigMap 和Secret诊断:
kubectl get configmaps -n <namespace>kubectl describe configmap <configmap-name> -n <namespace>kubectl get secrets -n <namespace>kubectl describe secret <secret-name> -n <namespace>命名空间诊断:
kubectl describe namespace <namespace-name>资源使用情况:
kubectl top pod <pod-name> -n <namespace>kubectl top nodes网络诊断:
kubectl get pods -n <namespace> -o custom-columns=POD:metadata.name,IP:status.podIP --no-headerskubectl get networkpolicies -n <namespace>kubectl describe networkpolicy <network-policy-name> -n <namespace>持久卷 (PV) 和持久卷声明 (PVC) 诊断:
kubectl get pvkubectl describe pv <pv-name>kubectl get pvc -n <namespace>kubectl describe pvc <pvc-name> -n <namespace>节点诊断:
kubectl get pods --field-selector spec.nodeName=<node-name> -n <namespace>资源配额和限制:
kubectl get resourcequotas -n <namespace>kubectl describe resourcequota <resource-quota-name> -n <namespace>自定义资源定义 (CRD) 诊断:
kubectl get <custom-resource-name> -n <namespace>kubectl describe <custom-resource-name> <custom-resource-instance-name> -n <namespace>使用这些命令时,请记住将<namespace>, <pod-name>, <service-name>, <deployment-name>, <statefulset-name>, <configmap-name>, <secret-name>, <namespace-name>, <pv-name>, <pvc-name>, <node-name>, <network-policy-name>, <resource-quota-name>, <custom-resource-name>, 和替换为你的特定值。
<custom-resource-instance-name>这些命令应该可以帮助你诊断 Kubernetes 集群以及在其中运行的应用程序。
资源伸缩和自动伸缩
kubectl scale deployment <deployment-name> --replicas=<replica-count> -n <namespace>kubectl autoscale deployment <deployment-name> --min=<min-pods> --max=<max-pods> --cpu-percent=<cpu-percent> -n <namespace>kubectl get hpa -n <namespace>作业和 CronJob 诊断:
kubectl get jobs -n <namespace>kubectl describe job <job-name> -n <namespace>kubectl get cronjobs -n <namespace>kubectl describe cronjob <cronjob-name> -n <namespace>容量诊断:
kubectl get pv --sort-by=.spec.capacity.storagekubectl get pv <pv-name> -o=jsonpath='{.spec.persistentVolumeReclaimPolicy}'kubectl get storageclassesIngress和服务网格诊断:
kubectl get ingress -n <namespace>kubectl describe ingress <ingress-name> -n <namespace>kubectl get virtualservices -n <namespace>kubectl describe virtualservice <virtualservice-name> -n <namespace>Pod 网络故障排除:
kubectl run -it --rm --restart=Never --image=busybox net-debug-pod -- /bin/shkubectl exec -it <pod-name> -n <namespace> -- curl <endpoint-url>kubectl exec -it <source-pod-name> -n <namespace> -- traceroute <destination-pod-ip>kubectl exec -it <pod-name> -n <namespace> -- nslookup <domain-name>配置和资源验证:
kubectl apply --dry-run=client -f <yaml-file>kubectl auth can-i list pods --as=system:serviceaccount:<namespace>:<serviceaccount-name>RBAC 和安全性:
kubectl get roles,rolebindings -n <namespace>kubectl describe role <role-name> -n <namespace>服务帐户诊断:
kubectl get serviceaccounts -n <namespace>kubectl describe serviceaccount <serviceaccount-name> -n <namespace>清空节点和解除封锁:
kubectl drain <node-name> --ignore-daemonsetskubectl uncordon <node-name>资源清理:
kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --forcePod 亲和性和反亲和性:
kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity}'kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.affinity.podAntiAffinity}'Pod 安全策略 (PSP):
kubectl get psp事件:
kubectl get events --sort-by=.metadata.creationTimestampkubectl get events -n <namespace>节点故障排除:
kubectl describe node <node-name> | grep Conditions -A5kubectl describe node <node-name> | grep -E "Capacity|Allocatable"临时容器(Kubernetes 1.18+):
kubectl debug -it <pod-name> -n <namespace> --image=<debug-image> -- /bin/sh资源指标(需要指标服务器):
kubectl top pod -n <namespace>kuelet诊断:
kubectl logs -n kube-system kubelet-<node-name>使用Telepresence 进行高级调试:
telepresence --namespace <namespace> --swap-deployment <pod-name>Kubeconfig 和上下文:
kubectl config get-contextskubectl config use-context <context-name>Pod 安全标准(PodSecurity 准入控制器):
kubectl get psp -A | grep -vE 'NAME|REVIEWED'Pod 中断预算 (PDB) 诊断:
kubectl get pdb -n <namespace>kubectl describe pdb <pdb-name> -n <namespace>资源锁诊断(如果使用资源锁):
kubectl get resourcelocks -n <namespace>服务端点和 DNS:
kubectl get endpoints <service-name> -n <namespace>kubectl exec -it <pod-name> -n <namespace> -- cat /etc/resolv.conf自定义指标(Prometheus、Grafana):
kubectl port-forward访问Prometheus和Grafana服务来查询自定义指标。Pod 优先级和抢占:
kubectl get priorityclassesPod 开销(Kubernetes 1.18+):
kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.overhead}'存储卷快照诊断(如果使用存储卷快照):
kubectl get volumesnapshot -n <namespace>kubectl describe volumesnapshot <snapshot-name> -n <namespace>资源反序列化诊断:
kubectl get <resource-type> <resource-name> -n <namespace> -o=json节点污点:
kubectl describe node <node-name> | grep Taints更改和验证 Webhook 配置:
kubectl get mutatingwebhookconfigurationskubectl get validatingwebhookconfigurationsPod 网络策略:
kubectl get networkpolicies -n <namespace>节点条件(Kubernetes 1.17+):
kubectl get nodes -o custom-columns=NODE:.metadata.name,READY:.status.conditions[?(@.type=="Ready")].status -l 'node-role.kubernetes.io/worker='审核日志:
节点操作系统详细信息:
kubectl get node <node-name> -o jsonpath='{.status.nodeInfo.osImage}'这些命令应该涵盖 Kubernetes 中的各种诊断场景。确保将<namespace>、<pod-name>、<deployment-name>等占位符替换为你的集群和用例的实际值