Shared file as persistent volume between Kubernetes pods

8/25/2020

Is there a way to share a persistent volume as a file between pods? For my App, I need common /etc/hosts among the pods.

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.

-- drifter
kubernetes

1 Answer

8/25/2020

You can share it with volumes if they are mounted with an access mode as ReadWriteMany or ReadOnlyMany and NFS supports that.

But I believe a nicer approach to sharing /etc/hosts is just to use HostAliases in your pods.

Example from the K8s docs:

apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "127.0.0.1"
    hostnames:
    - "foo.local"
    - "bar.local"
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - "/etc/hosts"

✌️

-- Rico
Source: StackOverflow