Kubernetes Volume Mount with Replication Controllers Kubernetes Volume Mount with Replication Controllers kubernetes kubernetes

Kubernetes Volume Mount with Replication Controllers


EmptyDir volumes are inherently bound to the lifecycle of a single pod and can't be shared amongst pods in replication controllers or otherwise. If you want to share volumes amongst pods, the best choices right now are NFS or gluster, in a persistent volume. See an example here: https://github.com/kubernetes/examples/blob/master/staging/volumes/nfs/README.md


Why do you want to share the volume mount between pods? This will not work reliably because you aren't guaranteed to have a 1:1 mapping between where pods in replication controller 1 and replication controller 2 are scheduled in your cluster.

If you want to share local storage between containers, you should put both of the containers into the same pod, and have each container mount the emptyDir volume.


You require three things to get this working. More info can be found here and some documentation here, but it's a little confusing at first.

This example mounts a NFS volume.

1. Create a PersistentVolume pointing to your NFS server

file : mynfssharename-pv.yaml

(update server to point to your server)

apiVersion: v1kind: PersistentVolumemetadata:  name: mynfssharenamespec:  capacity:    storage: 1Gi  accessModes:    - ReadWriteMany  nfs:    server: yourservernotmine.yourcompany.com    path: "/yournfspath"

kubectl create -f mynfssharename-pv.yaml

2. Create a PersistentVolumeClaim to points to PersistentVolume mynfssharename

file : mynfssharename-pvc.yaml

apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: mynfssharenamespec:  accessModes:    - ReadWriteMany  resources:    requests:      storage: 1Gi

kubectl create -f mynfssharename-pvc.yaml

3. Add the claim to your ReplicationController or Deployment

spec:  containers:  - name: sample-pipeline    image: yourimage    imagePullPolicy: Always    ports:    - containerPort: 8080      name: http    volumeMounts:      # name must match the volume name below      - name: mynfssharename        mountPath: "/mnt"  volumes:  - name: mynfssharename    persistentVolumeClaim:      claimName: mynfssharename