Kubernetes - how to check current domain set by --cluster-domain from pod?
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())
}
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.