Cluster DNS in Kubernetes

First, each node has a NodeName and IP address assigned to it. The node names and IP addresses of the cluster are probably registered in a DNS server in the organization. The Cluster DNS resolution within the cluster between the different components in the cluster such as pods and services. Kubernetes deploys a built-in DNS server by default when you set up a cluster. If you set up Kubernetes manually, then you do it by yourself.

So, How it helps pods resolve other pods and services within the cluster? We do not really care about nodes. We focus purely on Pods and services within the cluster. As long as our cluster networking is set up correctly, and all pods and services can get their own IP address and can reach each other.

Cluster DNS

  • All pods and services can reach others using their IP addresses.

Connection Test by DNS

1. Check DNS pod is running in the Kubernetes Cluster

$ kubectl get pods -n kube-system
NAME                                        READY   STATUS      RESTARTS   AGE
coredns-74ff55c5b-vjvpb                     1/1     Running     4          4d14h
  • coredns is running in the K8s Cluster

2. Create Nginx Pod and Nginx Service

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
    - name: nginx-container
      image: nginx
      imagePullPolicy: IfNotPresent
      ports:
        - containerPort: 80
          protocol: TCP
apiVersion: v1
kind: Service
metadata:
  name: nginx-clusterip-service

spec:
  type: ClusterIP
  ports:
    - targetPort: 80
      port: 80
  selector:
    app: nginx
$ kubectl create -f nginx-pod.yaml 
pod/nginx-pod created

$ kubectl create -f nginx-clusterip-service.yaml 
service/nginx-clusterip-service created

3. Create web Pod for connecting to Nginx Service (Nginx-Pod) through Cluster DNS

apiVersion: v1
kind: Pod
metadata:
  name: web-app-pod
spec:
  containers:
    - image: radial/busyboxplus:curl
      command:
        - sleep
        - "60000"
      imagePullPolicy: IfNotPresent
      name: web-app-container
  restartPolicy: Always
$ kubectl create -f web-app-pod.yaml 
pod/web-app-pod created

4. In the web Container in the Web Pod, curl Nginx Service or Nginx Pod

// Service Information
$ kubectl get service 
NAME                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
nginx-clusterip-service   ClusterIP   10.98.112.241    <none>        80/TCP         5m29s

// Pod Information
$ kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP            NODE       NOMINATED NODE   READINESS GATES
nginx-pod                          1/1     Running   0          6m14s   172.17.0.2    minikube   <none>           <none>
web-app-pod                        1/1     Running   0          3m40s   172.17.0.10   minikube   <none>           <none>

$ kubectl exec -it web-app-pod -- sh

// Connection Test to Nginx service with DNS

[ root@web-app-pod:/ ]$ curl nginx-clusterip-service
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl nginx-clusterip-service:80
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl nginx-clusterip-service.default.svc.cluster.local
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl http://nginx-clusterip-service.default.svc.cluster.local:80
Welcome to nginx!

// Connection Test to Nginx Service with Service IP

[ root@web-app-pod:/ ]$ curl 10.98.112.241
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl 10.98.112.241:80
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl http://10.98.112.241:80
Welcome to nginx!

// Connection Test to Nginx Pod with DNS

$ curl 172-17-0-2.default.pod.cluster.local
Welcome to nginx!

// Connection Test to Nginx Service with Pod IP

[ root@web-app-pod:/ ]$ curl 172.17.0.2
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl 172.17.0.2:80
Welcome to nginx!
[ root@web-app-pod:/ ]$ curl http://172.17.0.2:80
Welcome to nginx!
  • Cluster DNS only can be used in pods not node.
$ minikube ssh

// Nginx Service
$ curl 10.98.112.241
Welcome to nginx!

$ curl http://nginx-clusterip-service.default.svc.cluster.local:80
curl: (6) Could not resolve host: nginx-clusterip-service.default.svc.cluster.local

// Nginx Pod

$ curl 172.17.0.2
Welcome to nginx!

$ curl 172-17-0-2.default.pod.cluster.local
curl: (6) Could not resolve host: 172-17-0-2.default.pod.cluster.local

To make the nginx-pod accessible to a web pod, to create an nginx-service. Whenever the service is created, the Kubernetes DNS service creates a record for the service. It maps the service name to the IP address.

HostnameNamespaceTypeRootIP Address
nginx-clusterip-servicedefaultsvccluster.local10.98.112.241
172-1172-17-0-2defaultpodcluster.local172.17.0.2
Nginx Service and Nginx Pod with Kube DNS
Cluster DNS Rule

Leave a Reply

Your email address will not be published.

ANOTE.DEV