<p>I am running a <strong>3 Node Kubernetes cluster with Flannel as CNI</strong>. I used kubeadm to setup the cluster and the version is 1.23.</p>
<p>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.</p>
<p>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.</p>
<p>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.</p>
<p>So My question is -</p>
<ol>
<li>Why the pods running on the node cannot resolve hosts present in /etc/hosts file.</li>
<li>Is there a way to setup a local DNS server and asks all the Pods to query this DNS server for specific hosts resolutions?</li>
</ol>
<p>Any other suggestions or workarounds are also welcomed.</p>

Krishnom
<p>Environments in the container should be separated from other containers and machines (including its host machine), and the same goes for /etc/hosts.</p>
<p>If you are using coreDNS (the default internal DNS), you can easily add extra hosts information by modifying its configMap.</p>
<p>Open the configMap <code>kubectl edit configmap coredns -n kube-system</code> and edit it so that it includes <code>hosts</code> section:</p>
<pre class="lang-yaml prettyprint-override"><code>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
        ...
    }
</code></pre>
<p>The setting will be loaded in a few minutes then all the pods can resolve the hosts described in the configMap.</p>
