Kubernetes Pod 无法解析外部主机

发布于 2025-01-09 10:22:53 字数 536 浏览 0 评论 0原文

我正在运行一个 3 节点 Kubernetes 集群,并使用 Flannel 作为 CNI。我使用 kubeadm 设置集群,版本是 1.23。

我的 Pod 需要使用 DNS 地址与外部主机通信,但这些主机没有 DNS 服务器。为此,我已将它们的条目添加到集群中每个节点的 /etc/hosts 中。节点可以从 DNS 解析主机,但 Pod 无法解析它们。

我尝试通过互联网搜索这个问题,有建议使用 HostAlias 或更新容器内的 /etc/hosts 文件。我的问题是主机列表很大,并且在 yaml 文件中维护列表是不可行的。

我还查看了 Kubernetes 是否有一些内置标志来让 Pod 在 Node 的 /etc/hosts 中查找条目,但找不到它。

所以我的问题是 -

  1. 为什么节点上运行的 Pod 无法解析 /etc/hosts 文件中存在的主机。
  2. 有没有办法设置本地 DNS 服务器并要求所有 Pod 查询该 DNS 服务器以获取特定主机解析?

也欢迎任何其他建议或解决方法。

I am running a 3 Node Kubernetes cluster with Flannel as CNI. I used kubeadm to setup the cluster and the version is 1.23.

My pods need to talk to external hosts using DNS addresses but there is no DNS server for those hosts. For that, I have added their entries in /etc/hosts on each node in cluster. The nodes can resolve the host from DNS but Pods are not able to resolve them.

I tried to search this problem over internet and there are suggestions to use HostAlias or update /etc/hosts file inside container. My problem is that the list of hosts is large and it's not feasible to maintain the list in the yaml file.

I also looked if Kubernetes has some inbuilt flag to make Pod look for entries in Node's /etc/hosts but couldn't find it.

So My question is -

  1. Why the pods running on the node cannot resolve hosts present in /etc/hosts file.
  2. Is there a way to setup a local DNS server and asks all the Pods to query this DNS server for specific hosts resolutions?

Any other suggestions or workarounds are also welcomed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞳孔里扚悲伤 2025-01-16 10:22:53

容器中的环境应与其他容器和机器(包括其宿主机)分开,/etc/hosts 也是如此。

如果您使用 coreDNS(默认内部 DNS),您可以通过修改其 configMap 轻松添加额外的主机信息。

打开 configMap kubectl edit configmap coredns -n kube-system 并编辑它,使其包含 hosts 部分:

apiVersion: v1
data:
  Corefile: |
    .:53 {
        ...
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }

        ### Add the following section ###
        hosts {
          {ip1} {hostname1}
          {ip2} {hostname2}
          ...
          fallthrough
        }

        prometheus :9153
        ...
    }

该设置将在几分钟内加载,然后所有 pod 都可以解析 configMap 中描述的主机。

Environments in the container should be separated from other containers and machines (including its host machine), and the same goes for /etc/hosts.

If you are using coreDNS (the default internal DNS), you can easily add extra hosts information by modifying its configMap.

Open the configMap kubectl edit configmap coredns -n kube-system and edit it so that it includes hosts section:

apiVersion: v1
data:
  Corefile: |
    .:53 {
        ...
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           fallthrough in-addr.arpa ip6.arpa
           ttl 30
        }

        ### Add the following section ###
        hosts {
          {ip1} {hostname1}
          {ip2} {hostname2}
          ...
          fallthrough
        }

        prometheus :9153
        ...
    }

The setting will be loaded in a few minutes then all the pods can resolve the hosts described in the configMap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文