Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
kubectl run dnsutils -n shop --image=registry.k8s.io/e2e-test-images/jessie-dnsutils:1.3 --restart=Never -- sleep 3600
kubectl exec -n shop dnsutils -- nslookup kubernetes.default
kubectl exec -n shop dnsutils -- nslookup payments.shop.svc.cluster.local
kubectl -n kube-system get pods -l k8s-app=kube-dnsStep by step
- Test from the same namespace as the broken app (quick answer) — DNS answers and NetworkPolicies depend on where the Pod runs.
- Know the names — inside the same namespace
paymentsworks; from another namespace usepayments.shopor the fullpayments.shop.svc.cluster.local. A Pod name is not a DNS name (except with headless Services). - Look at resolv.conf:
kubectl exec -n shop dnsutils -- cat /etc/resolv.confnameserver must be the IP of the kube-dns Service (kubectl -n kube-system get svc kube-dns), with search shop.svc.cluster.local svc.cluster.local cluster.local and options ndots:5.
- Is CoreDNS healthy? — Pods
Running, endpoints present, no errors in the logs:
kubectl -n kube-system get endpointslices -l k8s-app=kube-dns
kubectl -n kube-system logs -l k8s-app=kube-dns --tail=50- Does the Service exist and have endpoints? — DNS can resolve a Service that has no Pods behind it; then the name works but the connection fails:
kubectl -n shop get svc payments
kubectl -n shop get endpointslices -l kubernetes.io/service-name=paymentsNo endpoints → the Service selector does not match the Pod labels, or the Pods are not Ready.
- NetworkPolicy — a default-deny egress policy also blocks DNS. Allow UDP and TCP 53 to
kube-system:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: shop
spec:
podSelector: {}
policyTypes: [Egress]
egress:
- to:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- {protocol: UDP, port: 53}
- {protocol: TCP, port: 53}- Slow external lookups — with
ndots:5,api.example.comis first tried with every search domain. Use a trailing dot in configs (api.example.com.) or setdnsConfig.optionsndots: "2"in the Pod spec. - Look at the CoreDNS config —
kubectl -n kube-system get configmap coredns -o yaml; add thelogplugin for a short time to see every query, and checkforwardfor external names. - Clean up —
kubectl delete pod -n shop dnsutils.
Check that it worked
nslookup kubernetes.default and nslookup payments.shop.svc.cluster.local return the Service IPs (Server: is the kube-dns IP); nslookup example.com returns a public IP quickly; the app connects without "could not resolve" errors.
Common problems
| Problem | Cause | Fix |
|---|---|---|
connection timed out; no servers could be reached | NetworkPolicy blocks port 53, or CoreDNS Pods down. | Allow UDP+TCP 53 to kube-dns; check CoreDNS Pods and logs. |
NXDOMAIN for a Service | Wrong namespace or name. | Use NAME.NAMESPACE; check kubectl get svc -A. |
| Name resolves, connection refused/time out | Service has no endpoints. | Fix the selector/labels; Pods must be Ready. |
| External names fail, internal work | CoreDNS forward to a broken upstream, or node DNS broken. | Check forward in the Corefile and the node's /etc/resolv.conf. |
CoreDNS CrashLoopBackOff with "loop detected" | Node resolv.conf points to 127.0.0.53 (systemd-resolved). | Point kubelet resolvConf to /run/systemd/resolve/resolv.conf. |
| Lookups slow, many queries | ndots:5 + search domains. | Trailing dot or lower ndots; NodeLocal DNSCache. |
Go further
- Command pages: kubectl, nslookup, dig.
- Next how-tos: Write a Kubernetes NetworkPolicy, Find why a Kubernetes Pod does not start, Open a shell in a running Pod, Check the DNS records of a domain.
- Learn it: CKA — Service DNS, CKA — Troubleshoot the network, CKA — Default deny.
1Why can a Service name resolve while the connection still fails?
DNS returns the Service IP even when no Pods are behind it; the Service needs endpoints from matching, Ready Pods.
2Which ports must a NetworkPolicy allow for DNS?
UDP and TCP port 53 to the CoreDNS Pods in kube-system.