持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第 3 天,点击查看活动详情
对 Pod DNS 解析,我以为我懂了,实际理解的还不够深刻 ..
-- 来自张三的新认知
1. 异常描述
保护隐私需求,假定无法解析的域名为: gz.earth.mama
下午同事反馈,某个函数服务异常,日志提示无法解析域名。通过 k9s attach 到容器中执行排查命令。
$ ping gz.earth.mama
ping: bad address 'gz.earth.mama'
$ cat /etc/resolv.conf
nameserver 169.254.25.10
search openfaas-fn.svc.alpha.pm.k8s svc.alpha.pm.k8s alpha.pm.k8s gz.earth.mama
options ndots:5
$ nslookup gz.earth.mama
Server: 169.254.25.10
Address: 169.254.25.10#53
*** Can't find gz.earth.mama.gz.earth.mama: No answer
$ cat /etc/issue
Welcome to Alpine Linux 3.13 # 竟然是 Alpine
Kernel \r on an \m (\l)
2. 常规排查
既然涉及容器 DNS 排查,肯定是要检查 nodelocaldns 和 coredns,这两个组件的排查思路,已在另外一篇文章中详细介绍:一步一步排查 K8S 集群 DNS 解析异常。
排除上面两个组件的配置异常,再网上追溯就到 k8s 节点主机层,我们来看 resolv.conf 文件。如下命令所示,可见节点主机可正常解析域名。
# node5
$ cat /etc/resolv.conf
search gz.earth.mama
nameserver 1.1.1.1 # 企业内域名解析
$ nslookup gz.earth.mama
Server: 1.1.1.1
Address: 1.1.1.1#53
Name: gz.earth.mama
Address: 2.2.2.2
【小结】:
节点主机可正常解析域名,coredns 和 nodelocaldns 配置没问题,pod 却无法正常解析域名。
3. 运行测试容器
由于异常容器使用的是 Alpine 系统,且非 root 权限启动,导致无法基于此容器排查问题。所以另外启动调试容器。于是,习惯性启动了个 centos7,奇怪的是,centos7 系统竟然可以正常解析,现象越发迷惑。
$ kubectl run centos7 --image=centos:7 --namespace openfaas-fn --command -- sleep 999999
那再启动个 Alpine 容器试试,更神奇的事情出现了:域名依旧无法解析。
kubectl run alpine --image=alpine --namespace openfaas-fn --command -- sleep 999999
【小结】:
Centos7 系统可以,而 Alpine 系统不可以。
4. 抓包定位问题
抓包工具使用 tcpdump,首先安装这个工具,安装命令如下:
$ apk update && apk add tcpdump
首先在 Alpine 容器中抓包,分别打开两个命令窗口,Attach 进入容器命令行。
# 抓包命令
$ tcpdump -i any host 10.233.70.93 -nn -vvv
注意观察真实请求的域名地址: gz.chian.mama.gz.earth.mama,显然这个域名解析记录是没有的。为什么会这样呢?
点击此处展开 - 详细抓包数据
$ tcpdump -i any host 10.233.70.93 -nn -vvv
tcpdump: data link type LINUX_SLL2
tcpdump: listening on any, link-type LINUX_SLL2 (Linux cooked v2), snapshot length 262144 bytes
14:03:13.289986 eth0 Out IP (tos 0x0, ttl 64, id 185, offset 0, flags [DF], proto UDP (17), length 85)
10.233.70.93.38027 > 169.254.25.10.53: [bad udp cksum 0x14a1 -> 0xa86c!] 63613+ A? gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s. (57)
14:03:13.290210 eth0 In IP (tos 0x0, ttl 64, id 43309, offset 0, flags [DF], proto UDP (17), length 175)
169.254.25.10.53 > 10.233.70.93.38027: [bad udp cksum 0x14fb -> 0xbd74!] 63613 NXDomain*- q: A? gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [3s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665583375 7200 1800 86400 30 (147)
14:03:13.290283 eth0 Out IP (tos 0x0, ttl 64, id 186, offset 0, flags [DF], proto UDP (17), length 85)
10.233.70.93.38027 > 169.254.25.10.53: [bad udp cksum 0x14a1 -> 0x8c3f!] 63914+ AAAA? gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s. (57)
14:03:13.290959 eth0 In IP (tos 0x0, ttl 64, id 43310, offset 0, flags [DF], proto UDP (17), length 175)
169.254.25.10.53 > 10.233.70.93.38027: [bad udp cksum 0x14fb -> 0x8d47!] 63914 NXDomain*- q: AAAA? gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [5s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665583393 7200 1800 86400 30 (147)
14:03:13.291010 eth0 Out IP (tos 0x0, ttl 64, id 187, offset 0, flags [DF], proto UDP (17), length 73)
10.233.70.93.51603 > 169.254.25.10.53: [bad udp cksum 0x1495 -> 0x5d6d!] 18098+ A? gz.earth.mama.svc.alpha.pm.k8s. (45)
14:03:13.291089 eth0 In IP (tos 0x0, ttl 64, id 43311, offset 0, flags [DF], proto UDP (17), length 163)
169.254.25.10.53 > 10.233.70.93.51603: [bad udp cksum 0x14ef -> 0x7275!] 18098 NXDomain*- q: A? gz.earth.mama.svc.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [3s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665583375 7200 1800 86400 30 (135)
14:03:13.291120 eth0 Out IP (tos 0x0, ttl 64, id 188, offset 0, flags [DF], proto UDP (17), length 73)
10.233.70.93.51603 > 169.254.25.10.53: [bad udp cksum 0x1495 -> 0x41a5!] 18298+ AAAA? gz.earth.mama.svc.alpha.pm.k8s. (45)
14:03:13.291386 eth0 In IP (tos 0x0, ttl 64, id 43312, offset 0, flags [DF], proto UDP (17), length 163)
169.254.25.10.53 > 10.233.70.93.51603: [bad udp cksum 0x14ef -> 0x42ad!] 18298 NXDomain*- q: AAAA? gz.earth.mama.svc.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [5s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665583393 7200 1800 86400 30 (135)
14:03:13.291434 eth0 Out IP (tos 0x0, ttl 64, id 189, offset 0, flags [DF], proto UDP (17), length 69)
10.233.70.93.43887 > 169.254.25.10.53: [bad udp cksum 0x1491 -> 0xd65e!] 49766+ A? gz.earth.mama.alpha.pm.k8s. (41)
14:03:13.291506 eth0 In IP (tos 0x0, ttl 64, id 43313, offset 0, flags [DF], proto UDP (17), length 159)
169.254.25.10.53 > 10.233.70.93.43887: [bad udp cksum 0x14eb -> 0xeb66!] 49766 NXDomain*- q: A? gz.earth.mama.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [3s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665583375 7200 1800 86400 30 (131)
14:03:13.291537 eth0 Out IP (tos 0x0, ttl 64, id 190, offset 0, flags [DF], proto UDP (17), length 69)
10.233.70.93.43887 > 169.254.25.10.53: [bad udp cksum 0x1491 -> 0xba92!] 49970+ AAAA? gz.earth.mama.alpha.pm.k8s. (41)
14:03:13.291802 eth0 In IP (tos 0x0, ttl 64, id 43314, offset 0, flags [DF], proto UDP (17), length 159)
169.254.25.10.53 > 10.233.70.93.43887: [bad udp cksum 0x14eb -> 0xbb9a!] 49970 NXDomain*- q: AAAA? gz.earth.mama.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [5s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665583393 7200 1800 86400 30 (131)
14:03:13.291846 eth0 Out IP (tos 0x0, ttl 64, id 191, offset 0, flags [DF], proto UDP (17), length 67)
10.233.70.93.48630 > 169.254.25.10.53: [bad udp cksum 0x148f -> 0xad51!] 3412+ A? gz.earth.mama.gz.earth.mama. (39)
14:03:13.292164 eth0 Out IP (tos 0x0, ttl 64, id 192, offset 0, flags [DF], proto UDP (17), length 67)
10.233.70.93.48630 > 169.254.25.10.53: [bad udp cksum 0x148f -> 0x919b!] 3594+ AAAA? gz.earth.mama.gz.earth.mama. (39)
14:03:13.292755 eth0 In IP (tos 0x0, ttl 64, id 43315, offset 0, flags [DF], proto UDP (17), length 164)
169.254.25.10.53 > 10.233.70.93.48630: [bad udp cksum 0x14f0 -> 0xbf28!] 3412* q: A? gz.earth.mama.gz.earth.mama. 0/1/0 ns: gz.earth.mama. [30s] SOA bc-hidden-dns-yp-01.gz.earth.mama. hostmaster.gz.earth.mama. 670282759 900 600 86400 3600 (136)
14:03:13.293106 eth0 In IP (tos 0x0, ttl 64, id 43316, offset 0, flags [DF], proto UDP (17), length 164)
169.254.25.10.53 > 10.233.70.93.48630: [bad udp cksum 0x14f0 -> 0xa372!] 3594* q: AAAA? gz.earth.mama.gz.earth.mama. 0/1/0 ns: gz.earth.mama. [30s] SOA bc-hidden-dns-yp-01.gz.earth.mama. hostmaster.gz.earth.mama. 670282759 900 600 86400 3600 (136)
5. 深刻理解域名解析
$ cat /etc/resolv.conf
nameserver 169.254.25.10
search openfaas-fn.svc.alpha.pm.k8s svc.alpha.pm.k8s alpha.pm.k8s gz.earth.mama
options ndots:5
这是容器内部的 resolve.conf 配置,其中第三行的 options ndots:5,指我们查询的域名中,少于 5 个点的情况下,自动在查询时,拼接第二行 search 后的那些域名后缀。
也就是说,当我们查询查询 gz.earth.mama 时,如抓包数据所示,其实查询的是如下域名:
- gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s
- gz.earth.mama.svc.alpha.pm.k8s
- gz.earth.mama.alpha.pm.k8s
- gz.earth.mama.gz.earth.mama
显然这些域名解析记录不可能存在,所以报无法解析域名的异常。
6. 修复异常
知道了问题所在,修复异常其实变得非常简单,批量把主机节点 /etc/resolv.conf 文件中的 search 字段去掉即可。
7. 额外问题:为什么 centos7 可以解析呢?
抓包工具使用 tcpdump,首先安装这个工具,安装命令如下:
$ yum install -y tcpdump
首先在 Centos7 容器中抓包,分别打开两个命令窗口,Attach 进入容器命令行。
# 抓包命令
$ tcpdump -i any host 10.233.95.202 -nn -vvv
从抓包数据中可以看到,Centos7 中的域名解析,依然遵循上述结论过程,但最后一次请求时,不再拼接 search 域名后缀,而是直接请求解析。当然可以解析得通。
点击此处展开 - 详细抓包数据
[root@centos7 /]# tcpdump -i any host 10.233.95.202 -nn -vvv
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
14:30:12.654953 IP (tos 0x0, ttl 64, id 63131, offset 0, flags [DF], proto UDP (17), length 85)
10.233.95.202.34522 > 169.254.25.10.53: [bad udp cksum 0x2e0e -> 0xb8d3!] 56410+ A? gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s. (57)
14:30:12.656043 IP (tos 0x0, ttl 64, id 40070, offset 0, flags [DF], proto UDP (17), length 175)
169.254.25.10.53 > 10.233.95.202.34522: [bad udp cksum 0x2e68 -> 0x66d5!] 56410 NXDomain*- q: A? gz.earth.mama.openfaas-fn.svc.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [5s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665585012 7200 1800 86400 30 (147)
14:30:12.656171 IP (tos 0x0, ttl 64, id 63133, offset 0, flags [DF], proto UDP (17), length 73)
10.233.95.202.60328 > 169.254.25.10.53: [bad udp cksum 0x2e02 -> 0x507c!] 6177+ A? gz.earth.mama.svc.alpha.pm.k8s. (45)
14:30:12.657136 IP (tos 0x0, ttl 64, id 40072, offset 0, flags [DF], proto UDP (17), length 163)
169.254.25.10.53 > 10.233.95.202.60328: [bad udp cksum 0x2e5c -> 0xfe7d!] 6177 NXDomain*- q: A? gz.earth.mama.svc.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [5s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665585012 7200 1800 86400 30 (135)
14:30:12.657244 IP (tos 0x0, ttl 64, id 63134, offset 0, flags [DF], proto UDP (17), length 69)
10.233.95.202.48988 > 169.254.25.10.53: [bad udp cksum 0x2dfe -> 0xcc40!] 40746+ A? gz.earth.mama.alpha.pm.k8s. (41)
14:30:12.659073 IP (tos 0x0, ttl 64, id 40074, offset 0, flags [DF], proto UDP (17), length 159)
169.254.25.10.53 > 10.233.95.202.48988: [bad udp cksum 0x2e58 -> 0x7a42!] 40746 NXDomain*- q: A? gz.earth.mama.alpha.pm.k8s. 0/1/0 ns: alpha.pm.k8s. [5s] SOA ns.dns.alpha.pm.k8s. hostmaster.alpha.pm.k8s. 1665585012 7200 1800 86400 30 (131)
14:30:12.659141 IP (tos 0x0, ttl 64, id 63136, offset 0, flags [DF], proto UDP (17), length 67)
10.233.95.202.45142 > 169.254.25.10.53: [bad udp cksum 0x2dfc -> 0x09cd!] 42251+ A? gz.earth.mama.gz.earth.mama. (39)
14:30:12.660741 IP (tos 0x0, ttl 64, id 40075, offset 0, flags [DF], proto UDP (17), length 164)
169.254.25.10.53 > 10.233.95.202.45142: [bad udp cksum 0x2e5d -> 0x1ba4!] 42251* q: A? gz.earth.mama.gz.earth.mama. 0/1/0 ns: gz.earth.mama. [30s] SOA bc-hidden-dns-yp-01.gz.earth.mama. hostmaster.gz.earth.mama. 670282759 900 600 86400 3600 (136)
14:30:12.660806 IP (tos 0x0, ttl 64, id 63137, offset 0, flags [DF], proto UDP (17), length 56)
10.233.95.202.35029 > 169.254.25.10.53: [bad udp cksum 0x2df1 -> 0xf107!] 37418+ A? gz.earth.mama. (28)
14:30:12.661699 IP (tos 0x0, ttl 64, id 40076, offset 0, flags [DF], proto UDP (17), length 330)
169.254.25.10.53 > 10.233.95.202.35029: [bad udp cksum 0x2f03 -> 0xaf55!] 37418* q: A? gz.earth.mama. 3/2/2 gz.earth.mama. [30s] A 10.10.16.2, gz.earth.mama. [30s] A 10.21.41.2, gz.earth.mama. [30s] A 10.21.41.3 ns: gz.earth.mama. [30s] NS bc-slave-dns-af-01.gz.earth.mama., gz.earth.mama. [30s] NS bc-slave-dns-yp-01.gz.earth.mama. ar: bc-slave-dns-af-01.gz.earth.mama. [30s] A 10.21.40.55, bc-slave-dns-yp-01.gz.earth.mama. [30s] A 172.17.251.5 (302)
8. 结论及思考
很多时候我们知道某个知识点,潜意识中以为自己理解了,真遇到问题时,往往会忽略曾经的结论。只有经历过踩坑,爬坑才会有更深刻的理解,这或许就是很多人活大半辈子都没有成长的原因。
在工作中,生产环境最好不要使用 alpine 或者 busybox 这类超轻量级的容器基础镜像,虽然说容器越小越好,但生产环境的稳定性更重要。而且这类精简系统,往往缺乏调试工具,一旦生产出现问题,想调试都非常困难。切记不要为了追求那么一点点的包大小,而把自己陷入未知的风险当中。
这是来自一位老运维的忠告,致张三!