Hosts for Pods that cannot resolve DNS.

A Rust sidecar that rewrites /etc/hosts from hosts_list and AWS endpoints.

Pod init: seed-hosts copies kubelet’s hosts /etc/hosts emptyDir: hosts-shared nginx :8800 create-hosts hosts-creator hosts_list 1 host / line DNS AWS API A records re-resolved every 10 s
Two containers, one file. nginx reads what create-hosts writes.

Rewritten every 10 seconds

  1. Rebuild from base_hosts

    kubelet’s hosts is kept as base_hosts and copied back each cycle.

  2. Resolve eight at a time

    Each line is resolved as an A record. Only answers are appended.

  3. Validate, then replace

    Every line is checked as IPv4 / IPv6, then written in place.

TTL in setting.ini sets the interval. Ten seconds by default.

A container’s hosts file is its own

nginx only reads the file the sidecar writes if both containers mount it. An initContainer copies kubelet’s hosts into an emptyDir; each container mounts that file over /etc/hosts with subPath.

CoreDNS is the one point every Pod queries. Start with NodeLocal DNSCache; add this for Pods that must survive DNS.

A cyanotype print of a patch panel and network cables
A hostname is pinned inside the Pod, not outside the cluster.

Not faster DNS. No DNS.

Both approaches still resolve names. hosts-creator does not.

10,000 Pods running. No query reaches CoreDNS.
167,000 pulls from Docker Hub.
AspectNodeLocal DNSCachesingle-request-reopenhosts-creator
PathNode cache, then CoreDNSglibc stub resolver/etc/hosts via NSS
When DNS failsFails after cacheFailsUnaffected
Load on CoreDNSLower on hitsUnchangedNone
RDS failoverRecord TTLRecord TTLNext cycle
App changesNoneresolv.confNone, privileged

CoreDNS is the one point every Pod queries. hosts-creator sends nothing there. Start with NodeLocal DNSCache; add this for Pods that must survive DNS.

Run one cycle and check

Validate the settings and read the resolution before production. Neither writes /etc/hosts.

hosts-creator --check-config
hosts-creator --once --dry-run
--once          one cycle, then exit
--dry-run       print, write nothing
--check-config  validate only

Drop --once to run as a daemon. Exit 0 is healthy; 1 means a setting, a lookup or a write failed.

What setting.ini decides

KeyValueDefault
TTLSeconds between rewrites10
GET_RDS_ENDPOINTList RDS endpointsFalse
GET_REDIS_ENDPOINTList ElastiCache endpointsFalse
CLOUD_ENVPer section. aws onlyaws
SELECT_TAG_KEYTag key. Empty is allempty
SELECT_TAG_VALUETag valueempty

Install with kustomize

One emptyDir, one initContainer to seed it, then the same file in both containers. To list RDS or ElastiCache, use a ServiceAccount with Describe permissions (IRSA).

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

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

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

What it costs to run

Measured, not estimated: the arm64 image ran in Docker at TTL 10 s for two minutes.

MetricMeasured, 8 hostnames, TTL 10 s, 2 min
cgroup memory9.4 MiB
Threads17
CPU0.00 %
Image, arm64 unpacked172 MB

Read from the cgroup after two minutes at TTL 10 s on arm64. There is no cache, so nothing is retained per name. Suggested requests cpu 10m / memory 32Mi, limits cpu 100m / memory 64Mi, for this container alone.

Inside, and when it fails

AspectBehaviour
Resolutionhickory, eight at a time. No cache.
WriteValidate, then replace in place.
ShutdownSIGTERM finishes the cycle in flight.
Settingssetting.ini, env, defaults. Flags win.
On failureExit 1. Kubernetes restarts it.
RecordsA records only.
TagsOne side only warns.