上次考题


Set configuration context $ kubectl config use-context k8s;

Create a deployment as follows;

Name: nginx-dns;

Exposed via a service: nginx-dns;

Ensure that the service & pod are accessible via their respective DNS records;

The container(s) within any pod(s) running as a part of this deployment should use the nginx image;

Next,use the utiliity nslookup to look up the DNS records of the service & pod and write the output to /opt/service.dns and /opt/pod.dns respectively;

Ensure you use the busybox:1.28 image (or earliser) for any testing, an the latest release has an unpstream bug which impacts the use of nslookup;


分析题目


先设置上下文:kubectl config use-context k8s;
按照以下条件创建deployment:
名称:nginx-dns;
暴露service:nginx-dns;
确保可以通过各自的DNS记录访问Service和Pod;
运行的任何Pod中的容器应使用nginx镜像;
接下来,使用nslookup查找service和pod的DNS记录,并将输出分别写入/opt/service.dns和/opt/pod.dns;
确保使用busybox:1.28镜像(或更早的版本)进行测试,最新版本具有unpstream错误,这会影响nslookup的使用;


答案

创建名为nginx-dns的deployment,和一个service,端口80

kubectl run nginx-dns --expose=true --port=80 --image=nginx

利用:​​kubectl get pods - o wide​​获取pod的IP:192.168.155.100

启动一个可以运行 nslookup的busybox pod:

kubectl run busybox -it --rm --generator=run-pod/v1 --image=busybox:1.28  sh

执行:

nslookup nginx-dns

nslookup 192.168.155.100

最终的结果,​​service​​​的​​dns​​​保存在​​/opt/service.dns​​​;​​pod​​​的​​dns​​​保存在​​/opt/pod.dns​

/ # mkdir opt
/ # nslookup nginx-dns > /opt/service.dns
/ # cat /opt/service.dns
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: nginx-dns
Address 1: 10.99.28.72 nginx-dns.default.svc.cluster.local
/ # nslookup 192.168.155.100 > /opt/pod.dns
/ # cat /opt/pod.dns
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name: 192.168.155.100
Address 1: 192.168.155.100 192-168-155-100.nginx-dns.default.svc.cluster.local
/ #

解析

官网中Pod与Service的DNS解析说明文档:

https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

Service A 记录

Service(headless Service 除外)将被分配一个 DNS A 记录,格式为 ​​my-svc.my-namespace.svc.cluster-domain.example​​。该 DNS 记录解析到 Service 的 ClusterIP。

Headless Service(ClusterIP为None)也将被分配一个 DNS A 记录,格式为 ​​my-svc.my-namespace.svc.cluster-domain.example​​。该 DNS 记录解析到 Service 所选中的一组 Pod 的 IP 地址的集合。

Service SRV 记录

Service(含 headless Service)的命名端口(有 name 的端口)将被分配一个 SRV 记录,其格式为 _my-port-name._my-port-protocol.my-svc.my-namespace.svc.cluster-domain.example:

对于一个普通 Service(非 headless Service),该 SRV 记录解析到其端口号和域名 my-svc.my-namespace.svc.cluster-domain.example

对于一个 Headless Service,该 SRV 记录解析到多个结果:每一个结果都对应该 Service 的一个后端 Pod,包含其端口号和 Pod 的域名 auto-generated-pod-name.my-svc.my-namespace.svc.cluster-domain.example

Pod A 记录

Pod 会以 ​​{podIIP}.my-namespace.pod.cluster.local​​这种形式被指派一个 DNS A 记录。

例如,default Namespace 具有 DNS 名字​​cluster.local​​​,在该 Namespace 中一个 IP 为 ​​1.2.3.4​​​ 的 Pod 将具有一个条目:​​1-2-3-4.default.pod.cluster.local​​。