⎈ k8s knowledge compiler

DNS for Services and Pods [page]deterministic

Your workload can discover Services within your cluster using DNS; this page explains how that works.

conceptsnetworking

Kubernetes creates DNS records for Services and Pods. You can contact Services with consistent DNS names instead of IP addresses.

Kubernetes publishes information about Pods and Services which is used to program DNS. kubelet configures Pods' DNS so that running containers can look up Services by name rather than IP.

Services defined in the cluster are assigned DNS names. By default, a client Pod's DNS search list includes the Pod's own namespace and the cluster's default domain.

### Namespaces of Services

A DNS query may return different results based on the namespace of the Pod making it. DNS queries that don't specify a namespace are limited to the Pod's namespace. Access Services in other namespaces by specifying it in the DNS query.

For example, consider a Pod in a `test` namespace. A `data` Service is in the `prod` namespace.

A query for `data` returns no results, because it uses the Pod's `test` namespace.

A query for `data.prod` returns the intended result, because it specifies the namespace.

DNS queries may be expanded using the Pod's `/etc/resolv.conf`. kubelet configures this file for each Pod. For example, a query for just `data` may be expanded to `data.test.svc.cluster.local`. The values of the `search` option are used to expand queries. To learn more about DNS queries, see [the `resolv.conf` manual page](https://www.man7.org/linux/man-pages/man5/resolv.conf.5.html).

``` nameserver 10.32.0.10 search <namespace>.svc.cluster.local svc.cluster.local cluster.local options ndots:5 ```

In summary, a Pod in the _test_ namespace can successfully resolve either `data.prod` or `data.prod.svc.cluster.local`.

### DNS Records

What objects get DNS records?

1. Services 1. Pods

The following sections detail the supported DNS record types and layout that is supported. Any other layout or names or queries that happen to work are considered implementation details and are subject to change without warning. For more up-to-date specification, see [Kubernetes DNS-Based Service Discovery](https://github.com/kubernetes/dns/blob/master/docs/specification.md).

## Services

### A/AAAA records

"Normal" (not headless) Services are assigned DNS A and/or AAAA records, depending on the IP family or families of the Service, with a name of the form `my-svc.my-namespace.svc.cluster-domain.example`. This resolves to the cluster IP of the Service.

[Headless Services](/docs/concepts/services-networking/service/#headless-services) (without a cluster IP) are also assigned DNS A and/or AAAA records, with a name of the form `my-svc.my-namespace.svc.cluster-domain.example`. Unlike normal Services, this resolves to the set of IPs of all of the Pods selected by the Service. Clients are expected to consume the set or else use standard round-robin selection from the set.

### SRV records

SRV Records are created for named ports that are part of normal or headless services.

  • For each named port, the SRV record has the form `_port-name._port-protocol.my-svc.my-namespace.svc.cluster-domain.example`.
  • For a regular Service, this resolves to the port number and the domain name: `my-svc.my-namespace.svc.cluster-domain.example`.
  • For a headless Service, this resolves to multiple answers, one for each Pod that is backing the Service, and contains the port number and the domain name of the Pod of the form `hostname.my-svc.my-namespace.svc.cluster-domain.example`.

## Pods

### A/AAAA records

Kube-DNS versions, prior to the implementation of the [DNS specification](https://github.com/kubernetes/dns/blob/master/docs/specification.md), had the following DNS resolution:

``` <pod-IPv4-address>.<namespace>.pod.<cluster-domain> ```

For example, if a Pod in the `default` namespace has the IP address 172.17.0.3, and the domain name for your cluster is `cluster.local`, then the Pod has a DNS name:

``` 172-17-0-3.default.pod.cluster.local ```

Some cluster DNS mechanisms, like [CoreDNS](https://coredns.io/), also provide `A` records for:

``` < …(trimmed)

Sources

concepts/services-networking/dns-pod-service.md · docDNS for Services and Pods

Related (15)

references EndpointSliceEndpointSlice conf=1
part_of Servicesdescribes conf=1
part_of Podsdescribes conf=1
part_of DNS search domain list limitsdescribes conf=1
part_of {{% heading "whatsnext" %}}describes conf=1
part_of Namespaces of Servicesdescribes conf=1
part_of DNS Recordsdescribes conf=1
part_of A/AAAA recordsdescribes conf=1
part_of SRV recordsdescribes conf=1
part_of Pod's DNS Policydescribes conf=1
part_of Pod's DNS Config {#pod-dns-config}describes conf=1
api_for Poddocuments API object conf=1

← all Docs