This is a post about Nginx’s DNS resolution behavior I didn’t know about but wish I did before I started using Kubernetes (K8s).
Nginx caches statically configured domains once
I moved a backend service
foo from running on a virtual
machine to K8s. Foo’s clients include an Nginx instance configured with this
1 2 3 4 5 6 7 8 9 10 11 12
K8s Pods can be rescheduled anytime so their IPs aren’t stable. I’m supposed to use K8s Services
to avoid caching these ephemeral Pod IPs. But in my case because of interoperability reasons I was
registering Pod IPs directly as A records for
foo.example.com.. I started noticing that after my Pod
IPs changed either because of rescheduling or updating the Deployment, Nginx started throwing
502 Bad Gateway errors.
Nginx resolves statically configured domain names only once at startup or configuration
reload time. So Nginx resolved
foo.example.com. once at startup to several Pod IPs and cached