关联Service的Pod无法接收流量,访问返回HTTP 502请求排查
Hey there, let's work through why your Service's Pod is showing an "x" next to "Receiving Traffic" (with the message "The pod has no endpoints and is not accepting traffic") and throwing HTTP 502 errors. Based on the details you shared, here's a step-by-step breakdown of what to check:
1. Verify Service-Pod Label Matching
Your Service uses the selector app.kubernetes.io/instance=myapp-instance, and you mentioned the Pod has this label—but double-check that the label's spelling and case are exactly identical (Kubernetes labels are case-sensitive). You can confirm this with:
kubectl get pods -n mynamespace -l app.kubernetes.io/instance=myapp-instance
If this command returns your target Pod, the label match is good. If not, there's a typo or case mismatch in your labels.
2. Check Pod Status and Readiness
Kubernetes only adds Pods to Service endpoints if they're in a Running state and all containers are marked ready. Inspect your Pod's full status with:
kubectl describe pod <your-pod-name> -n mynamespace
Look at the Conditions section—make sure the Ready condition shows True. If the Pod is Pending, CrashLoopBackOff, or has unready containers, it won't be included in the Service's endpoint list.
3. Validate Pod Port Configuration
Your Service targets the port named http/TCP. Confirm that your Pod's container exposes this port with the exact name http. Check the Pod's definition (via kubectl describe pod or its YAML) for a section like:
ports: - name: http containerPort: 80 protocol: TCP
If the port name doesn't match, or the container isn't listening on this port, the Service can't route traffic to the Pod.
4. Inspect the Endpoint Resource
Directly check the Endpoint object linked to your Service to see if any Pod IPs are listed:
kubectl get endpoints -n mynamespace <your-service-name>
If the Endpoints field is empty, that confirms the Service isn't matching any ready Pods. If there are IPs listed, the issue might be with network policies or the application inside the Pod.
5. Test the Application Inside the Pod
If the Endpoint has an IP but you still get 502s, verify the application inside the Pod is actually running and listening on the target port. Exec into the Pod and test with:
kubectl exec -it <your-pod-name> -n mynamespace -- curl localhost:<container-port>
If this returns an error, the problem is with the application itself—check its logs or configuration to ensure it's started correctly.
If you work through these steps and still hit issues, feel free to share the full kubectl describe pod output, and we can dig deeper.
内容的提问来源于stack exchange,提问作者lr-pal

