DNS does not resolve with NGINX in Kubernetes
Resolving the name fails because you need to use the Full Qualified Domain name. That is, you should use:
lead-api.<namespace>.svc.cluster.local
not just
lead-api
Using just the hostname will usually work because in kubernetes the resolv.conf
is configured with search domains so that you don't usually need to provide a service's FQDN. e.g:
search default.svc.cluster.local svc.cluster.local cluster.localnameserver 10.3.240.10options ndots:5
However, specifying the FQDN is necessary when you tell nginx to use a custom resolver because it does not get the benefit of these domain search specs.
You need to use a Service
http://kubernetes.io/docs/user-guide/services/
A kubernetes Service
proxies traffic to your Pods
(i.e. what you call 'service', which is your application)
I guess you use Kubernetes for the ability to deploy and scale your applications (Pods
) so traffic will need to be load balanced to them once you scale and you have multiple Pods to talk to. This is what a Service
does.
A Service
has its own IP address. As long as the Service
exists, a Nginx Pod
referencing this Service
in upstream will work fine.
Nginx (free version) dies when it can't resolve the upstream, but if the Service
is defined, it has its own IP and it gets resolved.
If the Pods
behind the Service
are not running, Nginx will not see that, and will try to forward the traffic but will return a 502 (bad gateway)
So, just defined the Service
and then bring up your Pods
with the proper label so the Service
will pick them up. You can delete, scale, replace those Pods
without affecting the Nginx Pod
. As long as there is at least one Pod running behind the Service
, Nginx will always be able to connect to your API.