This article guides troubleshooting an accessibility issue in a Kubernetes application by diagnosing port configuration mismatches and resolving them for proper connectivity.
In this lesson, we troubleshoot an accessibility issue with our NeonCat application. We deploy both a service and a deployment on Kubernetes, then use kubectl port-forward to diagnose why the application is unreachable. This guide walks through inspecting resources, establishing port forwarding, identifying configuration mismatches, and resolving the issue.
The detailed output from the port-forward command might reveal an error such as:
Copy
Ask AI
controlplane ~ ➜ k port-forward svc/nyancat-service 3000:80Forwarding from 127.0.0.1:3000 -> 8000Handling connection for 3000E0629 20:33:07.147258 27861 portforward.go:413] an error occurred forwarding 3000 -> 8000: error forwarding port 8000 to pod 974be1c7dcce7935a477faa4051ee8d3d61ebe8f112fc112701515331e672a9, uid : failed to execute portforward in network namespace "/var/run/netns/cni-0d9dad6a-e4c8-5a477faa4051ee8d3d61ebe8f112fc112701515331e672a9": dial tcp4 127.0.0.1:8000 inside namespace "974be1c7dcce7935a477faa4051ee8d3d61ebe8f112fc112701515331e672a9": connect: connection refusederror: lost connection to pod
This error indicates that the port-forward command is attempting to map local port 3000 to port 8000 on the pod rather than port 80.
The service is configured to listen on port 80 but forwards traffic to targetPort 8000 on the pod. However, the deployment’s container is actually listening on port 80.
To correct the problem, update the service configuration so that its targetPort matches the container’s listening port. Change the service’s targetPort from 8000 to 80.You can edit the service with the following command:
Test the application again by curling localhost on port 3000 or by opening http://localhost:3000 in your browser. A successful response might look similar to this:
Now, traffic flows correctly from local port 3000 to service port 80 and finally to the container’s port 80.
Ensuring that the service’s targetPort matches the container’s listening port removes unnecessary translation layers and prevents connection issues.
By aligning your port configurations, you eliminate the extra hop (from port 80 to 8000) that caused the connection refusal. This exercise emphasizes the importance of consistent port settings across your Kubernetes resources for reliable application accessibility.