kubernetes DNS resolution

New test-svc-dns directory, create two files busybox.yaml in the test-svc-dns directory, myapp-pod-service.yaml

busybox.yaml

apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: default
spec:
  containers:
  - name: busybox
    image: busybox
    command:
      - sleep
      - "3600"

myapp-pod-service.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-myapp
  namespace: default
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: service-myapp
  namespace: default
spec:
  selector:
    app: myapp
  ports:
  - name: http
    port: 80
    targetPort: 80

In test-svc-dns run the deployment command

kubectl apply -f .

Busybox into the pod in

kubectl exec -it busybox -- /bin/sh

Service access by domain name

wget -O - -q http://service-myapp

Through the service name to get the data in a pod.

 

Following is a brief pod at the network, the DNS parsing process

Cat /etc/resolv.conf executed in the busybox

busybox in the DNS server address is 10.96.0.10

Xshell re-open a terminal, connected to master node, Run kubectl get svc -n kube-system

Seen from the figure, 10.96.0.10 is the IP address of kube-dns, that is NDS servers within the pod is kube-dns

 

Now that you know what DNS servers are using the pod, now you look at, pod resolution process of DNS.

To understand some concepts

service_name: name of the service
namespace: where resource object name space
domain: to provide domain name suffix, such as the default cluster.local

In the pod kubernetes to access the cluster service through service_name.namespace.svc.domain, if the pod and service in the same namespace, you can directly use service_name.

busybox, service-myapp are in default under the default namespace.

So when running wget -O - -q http: when // service-myapp, kubernetes domain name will be modified to service-myapp.default.svc.cluster.local

Performed busybox nslookup service-myapp view the DNS resolution (busybox the nslookup command seems to have problems, good times and bad, if not resolved, can run more than a few times)

corresponding to the domain name service-myapp.default.svc.cluster.local ip is 10.108.217.164.

10.108.217.164 is service-myapp ip address.

 

Under summary: pod DNS server using a kube-dns, pod can be accessed through a cluster of Service service_name.namespace.svc.domain.

 

You can also view DNS kube-dns on the node

dig -t A service-myapp.default.svc.cluster.local. @10.96.0.10

 

Published 51 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/u010606397/article/details/90756816