Sub topic 10
ExternalName Services
This chapter assumes you know ClusterIP Services and cluster DNS names such as web.default.svc.cluster.local. An ExternalName Service is the one Service type that does no proxying at all. It is a DNS alias.
What an ExternalName Service does
An ExternalName Service maps a Service name inside the cluster to an external DNS name. It has no selector, no ClusterIP and no EndpointSlices. When a Pod looks up the Service name, CoreDNS answers with a CNAME record that points to the external name. The Pod then resolves that name and connects straight to the result.
apiVersion: v1
kind: Service
metadata:
name: orders-db
namespace: default
spec:
type: ExternalName
externalName: orders.db.example.comPods can now connect to orders-db or orders-db.default.svc.cluster.local. Both names resolve to orders.db.example.com.
Why use one
The main benefit is a stable in-cluster name for something outside the cluster. Application code and manifests refer to orders-db. You can later point the alias at a different host without changing the applications. A common case is migrating a database into the cluster. You replace the ExternalName Service with a ClusterIP Service of the same name, and clients need no changes.
Checking the result
Query the Service name from a temporary Pod and look for the CNAME.
kubectl get service orders-db
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- nslookup orders-db.default.svc.cluster.localThe first command shows TYPE as ExternalName and CLUSTER-IP as <none>. The second shows the alias and the address it resolves to.
Gotchas
- No proxying. kube-proxy does nothing for this Service, so traffic never passes through a virtual IP. Session affinity and traffic policies do not apply.
- Ports are not remapped. The Service does not translate ports. The client must use the port the external host actually listens on.
- Host header and TLS. The client connects to the external name but may send the Service name in the HTTP
Hostheader. TLS certificate checks can then fail, because the certificate names the external host, notorders-db. Configure the client to use the external hostname for these checks. - Names only, not IPs. The
externalNamevalue must be a DNS name. A value that looks like an IPv4 address is not handled as an IP by CoreDNS. For a fixed IP, use a Service without a selector and create the EndpointSlice yourself. - No readiness. Kubernetes does not check whether the external host is healthy.
- Name resolution depends on the cluster. The lookup is only as reliable as CoreDNS and its upstream resolvers.
Exercise
A Pod calls https://orders-db and gets a TLS certificate name mismatch, though the connection succeeds. The Service is type ExternalName pointing at orders.db.example.com. What causes this, and what is one fix?
Show answer
The client connects to the external host but validates the certificate against the name it requested, orders-db. The certificate was issued for orders.db.example.com. Fix it by having the client use orders.db.example.com as the hostname for TLS verification, or by setting that name as the TLS server name in the client configuration.