Kubernetes Services expose applications running inside a cluster to other components, inside or outside the cluster.
One kind of Service is ExternalName. It lets a Kubernetes Service stand in for a resource outside the cluster, such as a managed database, a messaging system or a third-party API. Pods call a stable in-cluster name, and cluster DNS points that name at the external host.
ExternalName is useful, but it can also cause a hidden pitfall for DNS resolution. This post shows how the pitfall happens and how to avoid it.
How an ExternalName Service works
An ExternalName Service has no selector, no pods and no cluster IP. Cluster DNS answers a lookup for the Service's name with a CNAME record that points to the external host:
12345678apiVersion: v1kind: Servicemetadata:name: orders-dbnamespace: defaultspec:type: ExternalNameexternalName: orders.example-db.internal
A pod that looks up orders-db.default.svc.cluster.local gets a CNAME to orders.example-db.internal, and resolves that host as usual.
The pitfall
Suppose your cluster has an ExternalName Service named "default" that points to www.google.com:
123456789# Risky: the Service has the same name as a namespaceapiVersion: v1kind: Servicemetadata:name: defaultnamespace: defaultspec:type: ExternalNameexternalName: www.google.com
Now take a service named "serviceA" in the "default" namespace. A pod that calls it by the short name "serviceA.default" expects to reach serviceA.default.svc.cluster.local. Instead, the lookup can resolve to www.google.com.
Any short lookup of the form <service>.default from that namespace can be redirected the same way. Your applications behave strangely, and the cause is hard to find, because nothing looks wrong in the Service that is failing.
Why it happens
A pod resolves short names through the search list in its /etc/resolv.conf. For a pod in the "default" namespace, the list starts with default.svc.cluster.local, then svc.cluster.local, then cluster.local, and Kubernetes sets ndots:5.
"serviceA.default" has fewer than five dots, so the resolver tries the search list first. The first name it tries is serviceA.default.default.svc.cluster.local.
That name sits under default.default.svc.cluster.local, which is the DNS name of the ExternalName Service called "default". Cluster DNS can answer the longer name with the same CNAME, so the lookup returns www.google.com before the resolver ever tries the correct name, serviceA.default.svc.cluster.local.
The exact behavior depends on your cluster DNS (CoreDNS in most clusters) and its version, so test it on your own cluster. The safe fixes below work either way.
The solution
Give the ExternalName Service a name that cannot collide with a namespace:
123456789# Safe: the name describes the external host, not a namespaceapiVersion: v1kind: Servicemetadata:name: google-externalnamespace: defaultspec:type: ExternalNameexternalName: www.google.com
The pitfall is not limited to the "default" namespace. A Service in any namespace that has the same name as another namespace can capture short lookups for that namespace from its own pods. When you create ExternalName Services, always choose a name that does not match a namespace.
You can also use fully qualified names, such as serviceA.default.svc.cluster.local. with a trailing dot, when one service calls another. A fully qualified name skips the search list, so no Service can capture it.
Conclusion
ExternalName is a useful way to reach resources outside the cluster. Name these Services carefully, and prefer fully qualified names between services, to keep DNS resolution predictable.
To read a pod's DNS settings without kubectl, K8s Inspector can read /etc/resolv.conf inside the pod.
