On Kubernetes, I can create a Service
, and then an Endpoint
which points to a set of external IP addresses, so I can have that service
available inside the cluster. For example:
kind: Service
apiVersion: v1
metadata:
name: elasticsearch
spec:
type: ClusterIP
ports:
- port: 9200
targetPort: 9200
---
kind: Endpoints
apiVersion: v1
metadata:
name: elasticsearch
subsets:
- addresses:
- ip: 10.128.1.2
- ip: 10.128.1.3
- ip: 10.128.1.4
- ip: 10.128.1.5
ports:
- port: 9200
This is nice, but it is a pain to keep it updated.
I thought maybe I could create a Service
with an ExternalName
, but I'm not sure if it round robin or just cache the DNS
result and use it directly:
kind: Service
apiVersion: v1
metadata:
name: elasticsearch-dns
spec:
type: ExternalName
externalName: elasticsearch.foo.bar
Is there a way to create a service on Kubernetes, from an external DNS, which round robin the requests to the DNS multiple IP addresses?
When any application looks up the name elasticsearch-dns
, the Cluster DNS returns a CNAME record with value elasticsearch.foo.bar
. So there is no caching of IP or anything by kubernetes.
apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: prod
spec:
type: ExternalName
externalName: my.database.example.com
When looking up the host my-service.prod.svc.cluster.local, the cluster DNS Service returns a CNAME record with the value my.database.example.com. Accessing my-service works in the same way as other Services but with the crucial difference that redirection happens at the DNS level rather than via proxying or forwarding.