Hello, readers! This article talks about customizing DNS resolution for Kubernetes with some detailed examples and explanations around the same.
So, let us begin!! 🙂
What is the importance of DNS in a Kubernetes Cluster?
DNS is an in-built Kubernetes service as a feature to us by the addon manager. The DNS server enables us to have the concept of DNS and service resolving the web requests initiated by the Pods/application.
Depending upon the Kubernetes version, we can make use of different DNS addon provided such as –
- Coredns
- kubedns, etc.
These addons support forward lookups (A and AAAA records), SRV records or port records, reverse IP lookups, CNAME lookups, host record lookups, etc.
Each and every Service associated with the cluster has a DNS name assigned to it.
The below Kubernetes objects get the DNS records on configuring in within the cluster-
- Service
- Pod
Using Kubernetes DNS, we instruct and schedule a DNS service as well as a pod in the cluster that configures the kubelet to instruct the containers to use the DNS service IP to resolve the host records.
Service records – A/AAAA
Usually, a Normal service is assigned a DNS A or AAAA record in cognition with the IP of the family. The name is in the form of a service-name.my-namespace.svc.cluster-domain.example. This helps to resolve the Cluster IP of the service.
And, a Headless service does resolve to the set of IPs of the pods which are selected by the Service at the backend.
Pod record – A/AAAA
The default DNS resolution of the pod is as follows-
pod-ip-address.my-namespace.pod.cluster-domain.example
Kubernetes CoreDNS solution
CoreDNS is the actual DNS server for the Kubernetes cluster which serves like a Cluster DNS in cognition with the DNS specifications.
It is modular in nature and comes with the feature of adding functions to it based on plugins. Usually, the configuration file helps us set up CoreDNS to a cluster.
Being a Kubernetes Admin, one can modify the config file to add domains for the cluster to be reachable through services and pods.
Installing CoreDNS in a Kubernetes Cluster-
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health {
lameduck 5s
}
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
ttl 30
}
prometheus :9153
forward . /etc/resolv.conf
cache 30
loop
reload
loadbalance
}
Below are the plugins available in CoreDNS-
- health : By default, the health of the plugin is available at http://localhost:8080/health.
- errors : The usual errors is available in the form of stdout.
- kubernetes : The DNS queries are answered by CoreDNS based on the IP of the pods and services available in Kubernetes cluster.
- ready : This plugin states the readiness of the signals.
- forward : If any query is not valid within the cluster domain, then that is given to the predefined resolvers, etc.
Configure Stub-domain using CoreDNS
One of the core features of CoreDNS is to configure stub domains and upstream nameservers for the Kubernetes Cluster.
Example: Consider you have an xyz domain server at 10.1.2.3 and every xyz server has a suffix .xyz.local. In this scenario, we will have to configure it to the CoreDNS file in the below manner-
xyz.local:53 {
errors
cache 30
forward . 10.1.2.3
}
The final Config Map file will look like-
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
data:
Corefile: |
.:53 {
errors
health
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
prometheus :9153
forward . 172.16.0.1
cache 30
loop
reload
loadbalance
}
xyz.local:53 {
errors
cache 30
forward . 10.1.2.3
}
To make the work easier, the kubeadm tool supports auto-translation from kube-dns to CoreDNS.
Conclusion
By this, we have approached the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Docker and Kubernetes, Stay tuned with us.
Till then, Happy Learning!! 🙂