<p>Is there a way to share a persistent volume as a file between pods? For my App, I need common <code>/etc/hosts</code> among the pods.</p>
<p>I want to mount the hostfile placed on the NFS volume file among the pods under /etc/hosts. I cannot use statefulset here because of some limitation in statefulsets.</p>

<p>You can share it with volumes if they are mounted with an access mode as <code>ReadWriteMany</code> or <code>ReadOnlyMany</code> and <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes" rel="nofollow noreferrer">NFS supports that</a>.</p>
<p>But I believe a nicer approach to sharing <code>/etc/hosts</code> is just to use <a href="https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/" rel="nofollow noreferrer"><code>HostAliases</code></a> in your pods.</p>
<p>Example from the K8s docs:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: &quot;127.0.0.1&quot;
    hostnames:
    - &quot;foo.local&quot;
    - &quot;bar.local&quot;
  - ip: &quot;10.1.2.3&quot;
    hostnames:
    - &quot;foo.remote&quot;
    - &quot;bar.remote&quot;
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - &quot;/etc/hosts&quot;
</code></pre>
<p>✌️</p>
