备战CKA每日一题——第14天 | Service和Pod的DNS记录

上次考题

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

最终的结果,servicedns保存在/opt/service.dnspoddns保存在/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

作者简洁

作者:小碗汤,一位热爱、认真写作的小伙,目前维护原创公众号:『我的小碗汤』,专注于写go语言、docker、kubernetes、java等开发、运维知识等提升硬实力的文章,期待你的关注。转载说明:务必注明来源(注明:来源于公众号:我的小碗汤,作者:小碗汤)

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、介绍: Kubernetes DNS在集群中会启动dns pod(coredns)和service(kube-...
    zds426阅读 3,240评论 0 0
  • Deployment 实际上并不足以覆盖所有的应用编排问题。 造成这个问题的根本原因,在于 Deployment ...
    理想枫林晚阅读 3,086评论 0 2
  • kubernetes Service 参考文献:https://blog.csdn.net/watermelonb...
    码二哥阅读 2,723评论 1 5
  • Kubernetes是Google开源的容器集群管理系统,其提供应用部署、维护、 扩展机制等功能,利用Kubern...
    devabel阅读 6,526评论 0 13
  • 今天中午放学回来,我去窗台前看我的七条小锦鲤,当我站在不远处看的时候(因为我不想吓到他们,所以就站在不远处...
    张瑾瑞阅读 164评论 0 2

友情链接更多精彩内容