KodeCloud CKA DNS
Links: 111 KodeCloud Index
DNS in k8s¶
- Whenever a service is created k8s DNS service creates a record for the service.
- It maps the service name to the IP of the service.
- FQDN format:
<hostname>.<namespace>.<type>.<root> - FQDN for service resolution:
<service-name>.<namespace>.svc.cluster.local- Diagram:
- Records for pods are NOT created by default.
- Once enabled records are created for pods as well.
- For the host name it uses the pod IP with
.replaced with-. - The
<type>ispod. - Diagram:
- The DNS server in k8s is known as CoreDNS.
- It is deployed as a pod in the kube-system namespace.
- CoreDNS pod reads the configuration settings from a file known as
Corefilewhich can be found at/etc/coredns/Corefileinside the pod. - The
Corefileis passed to the pod as a config map object.- If we have to modify the settings we can edit the config map object.
- When we deploy CoreDNS it also creates a service to to make it available to other components within the cluster.
- The service is named
kube-dnsby default and it has a static IP. - IP address of CoreDNS service (
kube-dns) is configured as the nameserver on pods by the kubelet. - The
/etc/resolv.conffile also has a search entry which allows us to find the service usingservice-nameorservice-name.namespaceorservice-name.namespace.svcorservice-name.namespace.svc.cluster.local.- NOTE: that the search entries are only for services and we WON'T be able to reach the pod the same way.
- We need to provide the FQDN to reach the pods.
10-244-2-5wont work but10-244-2-5.namespace.pod.cluster.localwill work.
- The service is named
- The DNS configurations on pods is done automatically by k8s when pods are created.
- Kubelet is responsible for this.
- We can view this at
/var/lib/kubelet/config.yaml(can be found usingps -ef | grep kubeletsince kubelet is running as a service)
- Kubernetes operators often want to customize how their pods and containers resolve certain custom domains, or need to adjust the upstream nameservers or search domain suffixes configured in
resolv.conf.- You can do this with the
dnsConfigoption of your pod’s spec. - Updating this config will rewrite a pod’s
resolv.confto enable the changes. - The configuration maps directly to the standard
resolv.confoptions, so the above config would create a file withnameserver 203.0.113.44andsearch custom.dns.locallines.
- You can do this with the
DNS is an add on in k8s so it might not be present in all the clusters.
If we cannot do name resolution (like nslookup kubernetes) of services then that means DNS is not installed.
Last updated: 2023-06-01