Kubernetes Service Not Found (DNS) Fix
In this tutorial, you'll learn about Kubernetes Service Not Found (DNS) Fix. We cover key concepts, practical examples, and best practices.
The Problem
A pod cannot reach another service by name:
curl: (6) Could not resolve host: my-service
Or in logs:
Error: getaddrinfo ENOTFOUND my-service
The nslookup inside the pod fails:
kubectl exec my-pod -- nslookup my-service
# ** server can't find my-service: NXDOMAIN
Kubernetes DNS resolution relies on CoreDNS (or kube-dns) running in the cluster. When DNS fails, services are unreachable by name.
Quick Fix
Step 1: Check CoreDNS pods are running
kubectl get pods -n kube-system -l k8s-app=kube-dns
NAME READY STATUS RESTARTS
coredns-7d6c9b5d8f-abc12 1/1 Running 0
coredns-7d6c9b5d8f-xyz99 1/1 Running 0
If CoreDNS pods are not running, describe them:
kubectl describe pods -n kube-system -l k8s-app=kube-dns
Step 2: Check CoreDNS logs
kubectl logs -n kube-system -l k8s-app=kube-dns
Look for errors like:
[ERROR] plugin/errors: 2 example.com. A: dial udp 8.8.8.8:53: i/o timeout
This means CoreDNS cannot reach upstream DNS servers. Check the CoreDNS ConfigMap:
kubectl get configmap coredns -n kube-system -o yaml
WRONG -- incorrect upstream DNS:
Corefile: |
.:53 {
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
forward . 8.8.8.8 # wrong if cluster has no internet access
}
RIGHT -- use cluster DNS or a known working resolver:
Corefile: |
.:53 {
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
forward . /etc/resolv.conf
}
Step 3: Verify the service exists in the correct namespace
kubectl get svc --all-namespaces | grep my-service
Services are only resolvable within the same namespace by short name:
WRONG -- using short name across namespaces:
curl http://my-service # fails if my-service is in a different namespace
RIGHT -- use the fully qualified name:
curl http://my-service.other-ns.svc.cluster.local
Or use the namespace-qualified short name:
curl http://my-service.other-ns
Step 4: Check pod DNS policy
kubectl get pod my-pod -o jsonpath='{.spec.dnsPolicy}'
WRONG -- None without a custom config:
dnsPolicy: None # only works if you provide dnsConfig
RIGHT -- use ClusterFirst (default):
dnsPolicy: ClusterFirst
For host-networked pods, use:
dnsPolicy: ClusterFirstWithHostNet
Step 5: Test DNS resolution from a debug pod
kubectl run dns-test --image=busybox -it --rm --restart=Never -- nslookup kubernetes.default.svc.cluster.local
Expected output:
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: kubernetes.default.svc.cluster.local
Address 1: 10.96.0.1
If this fails, CoreDNS is not serving DNS for the cluster domain. Increase CoreDNS replicas or check for network policies blocking DNS traffic on port 53.
Use DodaTech's DNS Inspector to run DNS queries against your cluster and visualize the resolution chain from pod to CoreDNS to upstream.
Prevention
- Always use FQDN (
service.namespace.svc.cluster.local) for cross-namespace communication. - Monitor CoreDNS metrics (requests, errors, latency) with Prometheus.
- Run at least two CoreDNS replicas for high availability.
- Configure CoreDNS resource requests to prevent OOM kills.
- Test DNS resolution as part of your deployment pipeline.
Common Mistakes with service not found
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world K8S code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro