Kubernetes DNS

repository·master·Indexed 22 days ago

https://github.com/kubernetes/dns

Core DNS services for Kubernetes clusters, including kube-dns and the sidecar daemon for DNS system monitoring, metrics exporting, and healthchecking. Includes documentation for building binaries and containers, release processes, and the integration of SkyDNS for service discovery via etcd.

Tokens
10.7K
Snippets
34
Records
52
Agent score
77%

What's inside kubernetes-dns

  1. Understand Kubernetes DNS versioning

    master
    Note that version numbers in this repository are not related to Kubernetes versions. There are no specific version compatibility requirements between this project and Kubernetes releases.
  2. Understand Kubernetes DNS-Based Service Discovery Schema

    master

    This specification defines the required DNS resource records (RRs) for Kubernetes service discovery. It provides a baseline for DNS implementations to ensure compatibility with Kubernetes services, including ClusterIP services, Headless services, and ExternalName services.

    Key concepts:

    • <zone>: The configured cluster domain (e.g., cluster.local).
    • <ns>: A Kubernetes Namespace.
    • _ready_: An endpoint is ready if its address is in the addresses field of the EndpointSubset object, or if the service has the service.alpha.kubernetes.io/tolerate-unready-endpoints annotation set to true.
    • _hostname_: The precedence for an endpoint's hostname is: 1. The endpoint's hostname field, 2. A unique, system-assigned identifier.
    • All comparisons are case-insensitive.
  3. Query SRV, A/AAAA, MX, and TXT records in SkyDNS

    master

    SkyDNS supports several standard DNS record types based on the JSON data stored in etcd:

    • SRV Records: Used for service discovery. If the host value is an IP address, SkyDNS synthesizes a domain name for the SRV target and puts the IP in the ADDITIONAL SECTION. You can control the target name length using TargetStrip.
    • A/AAAA Records: Returns IP addresses. Useful when ports are known in advance.
    • MX Records: If a service is registered with "mail": true, it is also returned as an MX record. The port value doubles as the MX Preference.
    • TXT Records: Returned if the registered JSON contains a text field.
    • CNAME Records: SkyDNS can create CNAME chains (up to 8 deep) if a host value is another name within the domain. It also supports External Names; if a CNAME points outside the local domain, SkyDNS will attempt to resolve it using configured nameservers.
  4. How SRV record weights are calculated

    master

    SkyDNS calculates the SRV.Weight for service discovery based on the weight provided in the service announcement. The weight is treated as a percentage.

    If multiple services have the same priority, SkyDNS distributes the total weight (100%) among them.

    Example 1: Equal weights If services a, b, and c all have weight 100, each receives an SRV.Weight of 33.

    Example 2: Custom weights If weights are adjusted:

    • Service a (weight 120) $\rightarrow$ SRV.Weight 34
    • Service b (weight 100) $\rightarrow$ SRV.Weight 28
    • Service c (weight 130) $\rightarrow$ SRV.Weight 37

    Note: All calculations are rounded down, so the sum total might be lower than 100.

  5. Configure Stub Zones

    master

    Stub Zones allow SkyDNS to point to a specific set of authoritative servers for a particular domain, rather than using standard recursion. When enabled, SkyDNS will consult the stub configuration before checking local records.

    Requirements:

    • SkyDNS must be started with the -stubzones flag.
    • Configuration is managed via Etcd under the path stub.dns.skydns.local..

    Configuration Format: Register services under the pattern: <domain>.stub.dns.skydns.local/<nameserver_id>. The value for each key must be a JSON object containing host and an optional port.

    Example: To point skydns.com to nameservers 172.16.1.1:54 and 10.10.244.1:53 (default), register:

    # Nameserver 1
    curl -XPUT http://127.0.0.1:4001/v2/keys/skydns/local/skydns/dns/stub/com/skydns/ns1 \
        -d '{"host":"172.16.1.1", "port":54}'
    
    # Nameserver 2 (port 53 is default)
    curl -XPUT http://127.0.0.1:4001/v2/keys/skydns/local/skydns/dns/stub/com/skydns/ns2 \
        -d '{"host":"10.10.244.1"}'
  6. Use wildcards and subdomains for DNS queries

    master

    SkyDNS supports wildcard queries and subdomain lookups to return multiple matching services.

    • Subdomain queries: Omitting the rightmost labels returns all services under that path.
    • Wildcards: You can use * or any in the middle of a name (e.g., staging.*.skydns.local) to match all names in a specific segment.
    • Additional Section: When querying for services that only have IP records, SkyDNS uses the etcd path to construct a target name and places the actual IP address in the ADDITIONAL SECTION of the DNS response.

    Example: Querying for all services in the east region:

    % dig @localhost SRV east.skydns.local
  7. Set up DNS Forwarding in SkyDNS

    master
    SkyDNS can act as a DNS forwarding proxy. By specifying nameservers in the SkyDNS configuration (e.g., 8.8.8.8:53,8.8.4.4:53), SkyDNS will round-robin between them for any requests it is not authoritative for. This allows you to use SkyDNS as your primary DNS server in /etc/resolv.conf for both local service discovery and external resolution.
  8. Use Groups to limit DNS recursion

    master

    Groups allow you to partition services so that queries only return a subset of records.

    • Services registered without a group field are always included in results.
    • Services with a group field are only returned if the query matches that group.

    This is useful for limiting the scope of recursion. For example, if domain.local/a and domain.local/b are in group: g1, and domain.local/subdom/c is in group: g2, a query for domain.local will return a and b, but not c.

  9. Configure NS records for SkyDNS

    master

    To ensure proper DNS operation, SkyDNS must declare its nameservers in etcd. These must be IP addresses. They are stored under the key local/skydns/dns/ns/{name}.

    Example: Registering ns1.ns.dns.skydns.local with IP 172.16.0.1:

    curl -XPUT http://127.0.0.1:4001/v2/keys/skydns/local/skydns/dns/ns/ns1 \
        -d value='{"host":"172.16.0.1"}'