RHEMS JapanJA

Name resolution that never reaches CoreDNS, in a 10,000-pod cluster

Kubernetes name resolution funnels into CoreDNS, so load and failure impact concentrate as pods grow. hosts creator, a sidecar pinning records in /etc/hosts, takes app lookups off DNS.

In a Kubernetes cluster, application name resolution funnels into CoreDNS. More pods mean more queries, and a slow CoreDNS slows the traffic of every pod. To avoid that single point, RHEMS Japan built hosts creator, a sidecar that pins resolved records in /etc/hosts, and runs it in a cluster with more than 10,000 pods.

This article covers why the design keeps DNS off the path rather than making it faster, where the approach breaks down, and what it gives up.

The conclusion first. Names that can be enumerated in advance are resolved from the OS hosts file without querying DNS. In exchange, the design gives up round-robin and immediate propagation of changes. Current practice is to install it only in pods where that trade-off fits.

Where name resolution breaks down

CoreDNS handles both in-cluster Service name resolution and external names. An application without a connection pool looks up the name on every request. The number of pods multiplied by requests per second funnels straight into one point.

What makes DNS difficult is that it does not fail in an obvious way when it is slow. It waits a few seconds and then fails, and the application treats it as a timeout. It takes time to realize that name resolution is the cause, and the log records only "cannot connect."

The trigger for building this tool was also a case where an application without a connection pool queried CoreDNS on every request and drove up load.

The usual remedies cited are NodeLocal DNSCache and single-request-reopen. Both work, but they solve different problems. What RHEMS Japan wanted to solve was not "reduce load" but "keep application name resolution working when CoreDNS stops."

The limits of NodeLocal

NodeLocal DNSCache places a cache on each node. Latency drops and queries to CoreDNS decrease. That is enough for many clusters, and it is the first thing RHEMS Japan recommends installing.

But it is a cache, so when it goes down, resolution goes back upstream. When the TTL expires, queries go out again. The dependency during a failure does not disappear.

There is also the fit with node size. NodeLocal runs per node, so as nodes grow larger and host more pods, the queries from that node grow as well. In the configuration RHEMS Japan compared, processing could not keep up as the pod count rose.

single-request-reopen avoids conntrack contention when glibc queries A and AAAA at the same time. It does not reduce the number of queries to CoreDNS itself. It has no effect on runtimes other than glibc, such as musl, Go, and Java.

A cache hides the failure

A cache assumes that the same question gets the same answer. Even if the IP changes, it returns the old answer for the length of the TTL. For this use, it fell short in two ways.

One is freshness. When a DB failover changes the IP, a long TTL delays follow-through. The other is behavior during a failure. It works while the cache is alive, but the moment it lapses, the same problem returns.

What was needed was for name resolution to continue even if DNS went down. Not to shrink the dependency, but to create a state where that path does not have to be used. Pushing the thinking that way, the answer was the hosts file.

Why pin records in hosts

/etc/hosts is the files source in NSS. It is consulted first in the resolution order, and resolution completes before any DNS query. hosts creator writes resolved records there.

No application changes are needed. resolv.conf is not touched either. nginx or PHP connects to the same name as before.

There are three costs.

  • It needs permission to rewrite /etc/hosts. This conflicts with read-only security policies.
  • DNS round-robin and weighting do not apply. The name is pinned to one IP.
  • Changes to the IP are delayed by the TTL.

The way to use it is to install it only in pods that can accept those three. It was not built as a tool to put on an entire cluster.

Rewritten every 10 seconds

At startup it backs up the current /etc/hosts as base_hosts and rebuilds from there on every cycle. This is so it does not break the lines kubelet wrote; base_hosts is created only once.

One cycle has three stages. Each line in hosts_list / rds_hosts_list / redis_hosts_list is resolved as an A record (eight in parallel), validated as IPv4 / IPv6 only if every line resolved, and replaced in place if it passes.

If even one record fails to resolve, nothing is written. A half-written hosts file would leave name resolution broken until the next success. On failure it exits with code 1, and Kubernetes restarts the container.

Results are not cached (the hickory cache is fixed at 0). Each cycle writes the current DNS answer. hosts freshness is set by the TTL and is not dragged along by the cache TTL on the DNS side.

# setting.ini
[DEFAULT]
TTL = 10
GET_RDS_ENDPOINT = False
GET_REDIS_ENDPOINT = False

# hosts_list (one host per line)
db.example.internal
cache.example.internal

The interval is the TTL in setting.ini, 10 seconds by default. It can also be set with the CREATE_HOST_TTL environment variable alone. Some customer environments run it at 20 seconds.

Sharing hosts with another container

Another container's /etc/hosts is a different file. To have nginx read the hosts file the sidecar writes, both must mount the same file. The hosts file kubelet generates is copied into an emptyDir by an initContainer and injected into each container's /etc/hosts via subPath.

A file mounted with subPath cannot be renamed. That is why writes are in place. Writing to a temporary file and replacing it fails in this setup.

initContainers:
  - name: seed-hosts
    image: busybox:1.36
    command: ["sh", "-c", "cp /etc/hosts /hosts-shared/hosts"]
    volumeMounts:
      - { name: hosts-shared, mountPath: /hosts-shared }

containers:
  - name: nginx
    volumeMounts:
      - { name: hosts-shared, mountPath: /etc/hosts, subPath: hosts }
  - name: create-hosts
    image: rhemsjapan/hosts-creator:rust-latest
    volumeMounts:
      - { name: hosts-shared, mountPath: /etc/hosts, subPath: hosts }

volumes:
  - name: hosts-shared
    emptyDir: {}

The trap is skipping the initContainer. Injecting /etc/hosts while the emptyDir is empty erases the pod's own entry and the lines kubelet wrote. In RHEMS Japan's testing too, nginx-side resolution and the pod's own name only coexisted once the backup was in place.

What changed at 10,000 pods

RHEMS Japan runs it in a cluster with more than 10,000 pods. What mattered at that scale was that queries do not scale with pod count. Names written in hosts_list are resolved from the OS hosts file, so more application requests do not reach CoreDNS.

Resource use stays small. These are measured values from running the arm64 container for two minutes at a TTL of 10 seconds and reading cgroup cpu.stat and memory.current. The figures compare 8 hostnames and 62 hostnames.

  • Resident memory (RSS): 7.3 MB (8 hostnames) / 7.4 MB (62 hostnames)
  • CPU (share of one core): 0.05% / 0.15%
  • Time per cycle: about 5 ms / about 15 ms
  • cgroup memory peak: 13.2 MB / 14.4 MB

More hostnames increase the resolution time per cycle, but memory barely changes. Because it holds no cache, it does not need storage proportional to the number of entries.

The recommended settings are requests cpu: 10m / memory: 32Mi and limits cpu: 100m / memory: 64Mi. The image is 172 MB expanded for arm64 (38.4 MB compressed on Docker Hub).

To be precise, CoreDNS load does not go to zero. In-cluster Service names and names not on hosts_list still query CoreDNS as before. Only names that can be enumerated in advance can be taken off it.

Cases where it does not fit

  • Reads need to be spread across multiple IPs. Neither round-robin nor weighting applies.
  • The IP changes every few seconds. Follow-through lags by the TTL.
  • /etc/hosts cannot be rewritten. A read-only root filesystem or policy constraints.
  • Targets change dynamically. AWS RDS / ElastiCache can be picked up by tag, but anything else requires updating hosts_list.

The failure mode is worth describing too. If hosts creator stops, /etc/hosts keeps the last content it wrote, so existing name resolution keeps working. The impact is limited to cases where following a new IP stops. That is a different failure shape from a DNS outage that halts traffic for every pod.

Setting the order of adoption

It is not a tool that replaces everything. First install NodeLocal DNSCache to cut cluster-wide latency and load, then add hosts creator only for pods that must not stop when DNS stops, or that need to follow changes faster than a record TTL. RHEMS Japan considers that order the practical one.

Before installing, you can validate the configuration and check one cycle. Neither rewrites /etc/hosts.

# Validate the configuration only
hosts-creator --check-config

# Run one cycle and inspect the result
hosts-creator --once --dry-run

To run it as a daemon, just drop --once. Exit code 0 is normal; 1 means invalid configuration, resolution failure, or write failure.

It is released as v0.1.0. The source is MIT licensed and the image is multi-arch for amd64 and arm64. Kubernetes samples, including emptyDir sharing and IRSA, are in the repository.

RHEMS Japan supports this kind of name resolution and DNS dependency design, from the design through post-adoption operations. For a consultation, use contact.

References

The figures in this article were measured in RHEMS Japan's test environment (arm64 container, TTL of 10 seconds, two minutes). Results vary with configuration and network conditions.

  • Kubernetes
  • CoreDNS
  • DNS
  • sidecar
  • Rust

Was this article useful?

Talk to us about operations for this topic.

Contact