How to Fix Service Not Accessible in Kubernetes Quickly
Kubernetes Service is not accessible, first check if the Service and its Pods are correctly running and matched by labels. Also verify the Service type and port settings, and ensure network policies or firewall rules are not blocking access.Why This Happens
This problem usually happens because the Service selector does not match any Pods, the Service type is incorrect for your access method, or network rules block traffic. For example, if the Service selector labels do not match the Pods labels, the service has no endpoints to route traffic to.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: wrong-label
ports:
- protocol: TCP
port: 80
targetPort: 8080The Fix
Update the Service selector to match the correct labels on the Pods. Also, confirm the Service type fits your use case (e.g., ClusterIP for internal access, NodePort or LoadBalancer for external). Check ports match between Service and Pods. Finally, verify no network policies or firewalls block traffic.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIPPrevention
Always keep Service selectors and Pod labels in sync. Use kubectl describe service and kubectl get endpoints to verify connectivity. Apply network policies carefully and test access after changes. Use readiness probes on Pods to ensure they only receive traffic when ready.
Related Errors
- Pods not ready: Service has no endpoints because pods are failing readiness checks.
- Wrong port: Service port does not match pod container port.
- NetworkPolicy blocks traffic: Network rules prevent access to service.