How to Fix Kubernetes Service Not Accessible
In this tutorial, you'll learn about How to Fix Kubernetes Service Not Accessible. We cover key concepts, practical examples, and best practices.
The Problem
You created a Kubernetes service but cannot access it:
$ curl http://localhost:8080
curl: (7) Failed to connect to localhost port 8080: Connection refused
The service exists but is only reachable inside the cluster:
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service ClusterIP 10.96.123.45 <none> 8080/TCP 2m
Services of type ClusterIP are only accessible within the cluster, not from your local machine.
Quick Fix
Step 1: Use port-forwarding for testing
kubectl port-forward service/my-service 8080:8080
Now access via http://localhost:8080. This is the fastest way to test a service locally.
Step 2: Change the service type to NodePort
kubectl edit service my-service
Change type: ClusterIP to type: NodePort:
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30080
Access via http://node-ip:30080.
Step 3: Use LoadBalancer (cloud only)
kubectl expose deployment my-deployment \
--type=LoadBalancer \
--port=8080 \
--target-port=8080
Get the external IP:
kubectl get svc my-service -w
Step 4: Check network policies
If you still cannot connect, a network policy might be blocking traffic:
kubectl get networkpolicies
Delete or modify policies that deny incoming traffic to your pod.
Alternative Solutions
For ingress-based access, create an Ingress resource:
kubectl create ingress my-ingress \
--rule="/=my-service:8080"
Use kubectl describe for Detailed Diagnostics
kubectl describe pod <pod-name>
# Events:
# Type Reason Age From Message
# ---- ------ ---- ---- -------
# Warning BackOff 5m kubelet Back-off restarting failed container
The Events section at the bottom of kubectl describe output is the most valuable diagnostic tool. It shows a chronological log of scheduling failures, image pull errors, and container crashes.
Additional Troubleshooting
# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"
If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.
Prevention
- Use
NodePortorLoadBalancerfor services that need external access. - Use
ClusterIPfor internal microservice communication. - Use port-forwarding for development and debugging.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro