How to config coredns with multi namespaces in kubernetes, to achive pods in each namespace gets it own dns resolution

4/3/2020

Kubernetes version 1.13.5, installed by kubeadm, in bare metal.

I'v created 2 namespaces, which are 'dev' & 'test'. Add some self-created dns record(like 'mysql.a.com') into coredns which default in 'kube-system' ns.

What I want to achive is to have pods in each namespace got it own dns record resolution, like pod in 'dev' ns lookup for 'mysql.a.com' gets 1.1.1.1, pod in 'test' ns lookup for 'mysql.a.com' gets 2.2.2.2. And all these service I want to map a DNS record to, is outside of the cluster.

Is this possible? or there're something I miss.

-- se681268
coredns
kubernetes
namespaces

1 Answer

4/3/2020

Stay away from configuring cluster wide defaults in CoreDNS for this. Instead use Kubernetes services.

The service type ExternalName can be used to create an entry in DNS. Since ExternalName services, just like other services, are namespaced resources you can have the same DNS entry resolving differently in the respective namespace.

Example for dev:

apiVersion: v1
kind: Service
metadata:
  name: database
  namespace: dev
spec:
  type: ExternalName
  externalName: dev-database.example.com

Example for test:

apiVersion: v1
kind: Service
metadata:
  name: database
  namespace: test
spec:
  type: ExternalName
  externalName: test-database.example.com

Inside each namespace, a DNS query for database.svc.cluster.local will resolve either to dev-database.example.com or test-database.example.com.

-- pst
Source: StackOverflow