Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 199Kubernetes · Network

How to debug DNS inside Kubernetes

"Could not resolve host: payments" — a Pod cannot find a Service, or external names are slow. In Kubernetes every Pod asks CoreDNS (Service kube-dns in kube-system) to resolve names like payments.shop.svc.cluster.local. This how-to starts a debug Pod, checks each step from the Pod's resolv.conf to CoreDNS and the Service, and fixes the usual causes: wrong name or namespace, NetworkPolicy blocking port 53, broken CoreDNS, and slow lookups because of ndots.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

199.1

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-dns

199.2

Step by step

  1. Test from the same namespace as the broken app (quick answer) — DNS answers and NetworkPolicies depend on where the Pod runs.
  2. Know the names — inside the same namespace payments works; from another namespace use payments.shop or the full payments.shop.svc.cluster.local. A Pod name is not a DNS name (except with headless Services).
  3. Look at resolv.conf:
kubectl exec -n shop dnsutils -- cat /etc/resolv.conf

nameserver 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.

  1. 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
  1. 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=payments

No endpoints → the Service selector does not match the Pod labels, or the Pods are not Ready.

  1. NetworkPolicy — a default-deny egress policy also blocks DNS. Allow UDP and TCP 53 to kube-system:
allow-dns.yaml
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}
  1. Slow external lookups — with ndots:5, api.example.com is first tried with every search domain. Use a trailing dot in configs (api.example.com.) or set dnsConfig.options ndots: "2" in the Pod spec.
  2. Look at the CoreDNS config — kubectl -n kube-system get configmap coredns -o yaml; add the log plugin for a short time to see every query, and check forward for external names.
  3. Clean up — kubectl delete pod -n shop dnsutils.

199.3

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.

199.4

Common problems

ProblemCauseFix
connection timed out; no servers could be reachedNetworkPolicy blocks port 53, or CoreDNS Pods down.Allow UDP+TCP 53 to kube-dns; check CoreDNS Pods and logs.
NXDOMAIN for a ServiceWrong namespace or name.Use NAME.NAMESPACE; check kubectl get svc -A.
Name resolves, connection refused/time outService has no endpoints.Fix the selector/labels; Pods must be Ready.
External names fail, internal workCoreDNS 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 queriesndots:5 + search domains.Trailing dot or lower ndots; NodeLocal DNSCache.

199.5

Go further

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.

Alle commando's in dit hoofdstuk