DNS for Kubernetes Service

First of all, DNS for Kubernetes Service is most important from the service discovery point of view. This is because an application usually connects to Kubernetes Service instead of connecting to the pod. This is why the application looks up the DNS entry for Kubernetes Service more often than for the pod.

Secondly, the DNS entry for Kubernetes Service will use the name of Kubernetes Service instead of an IP address. For instance, it will look like this: <Service Name>.<Namespace name>.svc.cluster.local.

Lastly, Kubernetes Service has 2 different behaviors for DNS; either normal service or headless service. Normal service has its own IP address, while headless service uses the pod's IP address(es). Let's go through normal service first. 

Normal service is the default Kubernetes Service. It will assign an IP address. Perform the following steps to create a normal service and check how DNS works:

  1. Create a normal service for apache on chap8-domain1 and chap8-domain2:
$ kubectl expose deploy my-apache --namespace=chap8-domain1 --name=my-apache-svc --port=80 --type=ClusterIP
service "my-apache-svc" exposed

$ kubectl expose deploy my-apache --namespace=chap8-domain2 --name=my-apache-svc --port=80 --type=ClusterIP
service "my-apache-svc" exposed
  1. Check the IP address for those two services by running the following command:
$ kubectl get svc my-apache-svc --namespace=chap8-domain1 
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-apache-svc ClusterIP 10.96.117.206 <none> 80/TCP 32s

$ kubectl get svc my-apache-svc --namespace=chap8-domain2
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-apache-svc ClusterIP 10.105.27.49 <none> 80/TCP 49s
  1. In order to perform name resolution, use the busybox pod in the foreground:
$ kubectl run -it busybox --restart=Never --image=busybox 
  1. In the busybox pod, use the nslookup command to query the IP address of those two services:
//query Normal Service on chap8-domain1
# nslookup my-apache-svc.chap8-domain1.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: my-apache-svc.chap8-domain1.svc.cluster.local
Address 1: 10.96.117.206 my-apache-svc.chap8-domain1.svc.cluster.local


//query Normal Service on chap8-domain2
# nslookup my-apache-svc.chap8-domain2.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: my-apache-svc.chap8-domain2.svc.cluster.local
Address 1: 10.105.27.49 my-apache-svc.chap8-domain2.svc.cluster.local
  1. Access to service for apache whether traffic can dispatch to the backend apache pod:
# wget -q -O - my-apache-svc.chap8-domain1.svc.cluster.local
<html><body><h1>It works!</h1></body></html>

# wget -q -O - my-apache-svc.chap8-domain2.svc.cluster.local
<html><body><h1>It works!</h1></body></html>
  1. Quit the busybox pod and delete it:
# exit 
$ kubectl delete pod busybox
pod "busybox" deleted

DNS for a normal service behaves as a proxy; traffic goes to the normal service, then dispatches to the pod. What about the headless service? This will be discussed in the How it works... section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.108.175