Kubernetes admin can use --cluster-domain
to customize cluster domain instead of using default one: cluster.local
Kubelet Configs.
So the question is, how does an application pod check this domain in runtime?
You can do a DNS query from any pod and you would get the FQDN. cluster-domain.
Kubernetes service names are resolved to ClusterIP s representing one or more pods that match a label selector. The cluster is assigned a cluster domain that is specified at installation time by using cluster_domain to distinguish between names local to the cluster and external names.
To view the entire configuration of the pod, just run kubectl describe pod nginx in your terminal. The terminal will now display the YAML for the pod, starting with the name nginx, its location, the Minikube node, start time and current status.
It needs to be configured on the DNS server.
Either kube-dns or coredns (Favored on newer K8s versions)
kube-dns: it's a cli option --domain
core-dns: you can configure the K8s ConfigMap
And you see here:
The kubelet passes DNS to each container with the --cluster-dns= flag.
If you'd like to know how a pod resolves cluster.local
it does it through the /etc/resolv.conf
that the kubelet mounts on every pod. The content is something like this:
$ cat /etc/resolv.conf
nameserver 10.96.0.10
search <namespace>.svc.cluster.local svc.cluster.local cluster.local <nod-domain>
options ndots:5
10.96.0.10
is your coredns
or kube-dns
cluster IP address.
Running a DNS query against service kubernetes.default is a possible solution. Here is a one-liner example in shell:
kubectl run -it --image=ubuntu --restart=Never shell -- \
sh -c 'apt-get update > /dev/null && apt-get install -y dnsutils > /dev/null && \
nslookup kubernetes.default | grep Name | sed "s/Name:\skubernetes.default//"'
This will returns as last line:
.svc.cluster.local
However, I think it would be more robust to implement this algorithm in a programming language like go which have a good DNS client implemented in net library, here is an example you can run in a pod:
package main
import (
"fmt"
"net"
"strings"
)
// GetClusterDomain returns Kubernetes cluster domain, default to "cluster.local"
func getClusterDomain() string {
apiSvc := "kubernetes.default.svc"
cname, err := net.LookupCNAME(apiSvc)
if err != nil {
defaultClusterDomain := "cluster.local"
return defaultClusterDomain
}
clusterDomain = strings.TrimPrefix(cname, apiSvc)
clusterDomain = strings.TrimSuffix(clusterDomain, ".")
return clusterDomain
}
func main() {
fmt.Println(getClusterDomain())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With