![header](card.jpg)

## dnsPolicy: Default is not the default

Kubernetes has 4 DNS policies, and the one named `Default` is not the default. `dnsPolicy: Default` makes a pod copy the node's `/etc/resolv.conf`, which may not resolve cluster Service names even when public DNS works. The real default, the one you get when you leave the field out, is `ClusterFirst`. If you find `Default` in a pod template you own, the fix is usually to delete the line.

## The note

The Kubernetes docs page on DNS for Services and Pods has a note under the list of policies. Word for word:

> "Default" is not the default DNS policy. If `dnsPolicy` is not explicitly specified, then "ClusterFirst" is used.

Somebody on the docs saw this trap coming and wrote one sentence to stop it. I read that sentence the day I deleted the word from a pod template. Until then I had read `dnsPolicy: Default` the way most people would: as "leave it as is".

## What each policy does

| Policy | Where queries go | Resolves `my-svc.my-ns.svc.cluster.local`? |
|---|---|---|
| `ClusterFirst` (the real default) | cluster DNS (CoreDNS), which forwards anything outside the cluster domain upstream | yes |
| `Default` | whatever the node uses | depends on the node resolver |
| `ClusterFirstWithHostNet` | cluster DNS, for pods on `hostNetwork: true` | yes |
| `None` | only what you put in `dnsConfig` | only if you configure it |

With `Default` the pod inherits the node's resolver config. If the node's resolver does not know your cluster domain, `my-service.my-namespace.svc.cluster.local` can fail even when public names work. A check that only tests the internet can miss the problem. Some node resolvers are configured to resolve cluster names, so check your setup.

```mermaid
flowchart TB
  subgraph cf [ClusterFirst]
    direction LR
    A[pod] --> C[CoreDNS] --> S[Service IP]
  end
  subgraph df [Default]
    direction LR
    B[pod] --> N[node resolver] --> X[may not resolve]
  end
  cf ~~~ df
  class X bug
```

_Same name, same pod. The resolver changes; the answer depends on the node DNS configuration._

## Who Default is for

If it breaks Service names, why does it exist? Look at CoreDNS. The upstream manifest in the Kubernetes repo sets `dnsPolicy: Default` on the CoreDNS pods. The DNS server can't ask itself where to find upstream DNS, so it takes the node's resolver. It is useful for pods that need the node resolver rather than cluster DNS.

So `Default` in a system add-on is normal. In an application pod that needs Service names, check whether the node resolver can resolve them.

## The host network twist

There is a second way to end up on the node's resolver without writing `Default` at all. A pod with `hostNetwork: true` and the default `ClusterFirst` falls back to the `Default` behavior. The docs say you should set `ClusterFirstWithHostNet` explicitly for those pods. So of the 4 values, the one named Default and the one you get without asking on host network both use the node resolver unless other DNS settings change it.

## How to spot it

Inside the pod, read the resolver config:

```bash
kubectl exec -n my-ns my-pod -- cat /etc/resolv.conf
```

A `ClusterFirst` pod looks like the docs example: the cluster DNS Service IP as `nameserver`, a `search` line with `<namespace>.svc.cluster.local svc.cluster.local cluster.local`, and `options ndots:5`. A missing cluster search suffix is a clue that short Service names may fail, not proof of which resolver is in use. Cluster domains and custom DNS settings can differ. Check the nameserver too, and query a fully qualified Service name.

To find every pod that sets it across a cluster:

```bash
kubectl get pods -A -o json \
  | jq -r '.items[] | select(.spec.dnsPolicy=="Default") | "\(.metadata.namespace)/\(.metadata.name)"'
```

Expect CoreDNS in the list. Anything else deserves a second look, and so does any `hostNetwork: true` pod still on `ClusterFirst`:

```bash
kubectl get pods -A -o json \
  | jq -r '.items[] | select(.spec.hostNetwork==true and .spec.dnsPolicy=="ClusterFirst") | "\(.metadata.namespace)/\(.metadata.name)"'
```

## The fix

1. Application pod that needs cluster DNS and does not intentionally use the node resolver: remove `dnsPolicy: Default`. A non-host-network pod gets `ClusterFirst`.
2. Pod on `hostNetwork: true` that calls Services: set `dnsPolicy: ClusterFirstWithHostNet`.
3. Pod that really needs custom resolvers: use `None` with an explicit `dnsConfig`, so the next reader sees exactly what it gets.

## What I kept

I now read any field whose value is literally "Default" as a specific mode, not as "the default". The one-line note in the docs was right there the whole time. Deleting one word took less time than reading it.

## Credits and links

- [Kubernetes docs: DNS for Services and Pods, Pod's DNS Policy](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy)
- [Kubernetes docs: Customizing DNS Service](https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/)
- [CoreDNS manifest in kubernetes/kubernetes, `dnsPolicy: Default`](https://github.com/kubernetes/kubernetes/blob/master/cluster/addons/dns/coredns/coredns.yaml.base)

_Nahum Litvin, Lead Platform Engineer._