π Custom domain names using Kubernetes CoreDNS

β¨ Introduction
π― CoreDNS is the DNS service discovery plugin for Kubernetes. It replaces the older kube-dns and is pre-installed in the kube-system namespace.
π‘ The objective of this guide is to use CoreDNS to provide custom domain names inside the cluster. For example, replacing the service name nginx.default.svc.cluster.local with something like nginx.default.devops.com.
π Getting Started
π Check the current CoreDNS setup:
kubectl get pods,service,configmap -n kube-system -l=k8s-app=kube-dns

CoreDNS configuration is saved into a configmap.
kubectl describe configmap coredns -n kube-system
To provide a custom confiuration, we can use the coredns custom confgmap.
π οΈ Customizing CoreDNS
1) Save the following as
create custome-configmap.yamlapiVersion: v1 kind: ConfigMap metadata: name: coredns-custom namespace: kube-system labels: k8s-app: kube-dns data: internal-custom.override: | rewrite stop { name regex (.*)\.aks\.com\.$ {1}.svc.cluster.local. answer name (.*)\.svc\.cluster\.local\.$ {1}.devops.com. }
Apply the custom ConfigMap:
kubectl apply -f custom-configmap.yaml
2) Import the Custom ConfigMap
Edit the default CoreDNS ConfigMap to include the custom configuration: πΈ Reference screenshot:
kubectl edit configmap coredns -n kube-system
import custom/*.override
3) βοΈ Update CoreDNS Deployment To enable the custom configuration, add a volume and volume mount to the CoreDNS deployment: Modify the deployment YAML:
volumes:
- configMap:
defaultMode: 420
items:
- key: internal-custom.override
path: internal-custom.override
name: coredns-custom
volumeMounts:
- mountPath: /etc/coredns/custom
name: custom-config
readOnly: true
πΈ Reference screenshot: 
4) π¦ Now letβs start deploying some services & expose them. Create Services in Different Namespaces
π’ Create deployments and expose services in the
defaultandkongnamespaces:
kubectl create deployment nginx --image=nginx --replicas=1 -n default
kubectl expose deployment nginx --name nginx --port=80 -n default
kubectl get deploy,svc
kubectl create deployment apache2 --image=httpd --replicas=1 -n kong
kubectl expose deployment apache2 --name apache2 --port=80 -n kong
kubectl get deploy,svc -n kong
π§© Explanation:
π Incoming domain
apache2.kong.devops.comwill be rewritten asapache2.kong.svc.cluster.local.
π Incoming domain
nginx.default.devops.comwill be rewritten asnginx.default.svc.cluster.local.
π§ͺ Testing:
Let us try resolving with
.devops.com. Andunderstand how that works.
β
nginx.default.devops.comβ should resolve tonginx.default.svc.cluster.local.
β
apache2.kong.devops.comβ should resolve toapache2.kong.svc.cluster.local.
kubectl exec -it deploy/apache2 -n kong -- bash
apt update && apt install dnsutils curl -y
nslookup nginx.default.devops.com
nslookup apache2.kong.devops.com
curl nginx.default.devops.com
curl apach2.kong.devops.com
πΈ Reference screenshot of final output:

