k8s 中不同命名空间的服务的 DNS 名称是什么?

What's the DNS name for services in different namespaces in k8s?

在命名空间 A 中,我有一个服务 nginx 运行。在命名空间 B 中,我可以使用 nginx.Anginx.A.svc.cluster.local 来访问命名空间 A 中的 nginx

那么这两者有什么区别呢?更推荐哪一款?为什么?

这些是等价的。如果您查看注入到每个容器中的默认值 resolv.conf:

search mynamespace.svc.cluster.local svc.cluster.local cluster.local mydomain
options ndots:5

你可以看到它会自动帮你查询上链。这假定您使用默认的 Linux DNS 客户端行为。如果您有自己的自定义解析器,那么它可能无法理解部分查找语法,但大多数会检查 resolv.conf.

两种形式都被认为是正确的(与this文章相比)并且在大多数情况下工作正常但是我可以在[=28上找到一些问题=]github 当人们遇到一些仅与短名称解析相关的问题时,例如:

https://github.com/kubernetes/dns/issues/109

https://github.com/kubernetes/kubernetes/issues/10014

正如您在官方 Kubernetes 文档 (ref1, ref2) 中所读,它建议在跨命名空间访问服务时使用长格式:

When you create a Service, it creates a corresponding DNS entry. This entry is of the form <service-name>.<namespace-name>.svc.cluster.local, which means that if a container just uses <service-name>, it will resolve to the service which is local to a namespace. This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN).

在我看来,坚持 FQDN (fully qualified domain name) 标准要好得多,而且通常明确的做法被认为比含蓄的做法更好。