Vishanth
Vishanth

Reputation: 1370

Kubernetes Volume Mount with Replication Controllers

Found this example for Kubernetes EmptyDir volume

apiVersion: v1
kind: Pod
metadata:
  name: www
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - mountPath: /srv/www
      name: www-data
      readOnly: true
  - name: git-monitor
    image: kubernetes/git-monitor
    env:
    - name: GIT_REPO
      value: http://github.com/some/repo.git
    volumeMounts:
    - mountPath: /data
      name: www-data
  volumes:
  - name: www-data
    emptyDir: {}

I want to volume mount between 2 pods. I am creating these pods using 2 different Replication Controllers. The replication controllers looks like this

Replication Controller 1:

apiVersion: v1
kind: ReplicationController
metadata:
  name: node-worker
  labels:
    name: node-worker
spec:
  replicas: 1
  selector:
    name: node-worker
  template:
    metadata:
      labels:
        name: node-worker
    spec:
      containers:
      -
        name: node-worker
        image: image/node-worker
        volumeMounts:
          - mountPath: /mnt/test
            name: deployment-volume
      volumes:
        - name: deployment-volume
          emptyDir: {}

Replication Controller 2:

apiVersion: v1
    kind: ReplicationController
    metadata:
      name: node-manager
      labels:
        name: node-manager
    spec:
      replicas: 1
      selector:
        name: node-manager
      template:
        metadata:
          labels:
            name: node-manager
        spec:
          containers:
          -
            name: node-manager
            image: image/node-manager
            volumeMounts:
              - mountPath: /mnt/test
                name: deployment-volume
          volumes:
            - name: deployment-volume
              emptyDir: {}

Can Kubernetes emptyDir volume be used for this scenario?

Upvotes: 2

Views: 3922

Answers (3)

rjdkolb
rjdkolb

Reputation: 11848

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: v1
kind: PersistentVolume
metadata:
  name: mynfssharename
spec:
  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: v1
kind: PersistentVolumeClaim
metadata:
  name: mynfssharename
spec:
  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

Upvotes: 0

Paul Morie
Paul Morie

Reputation: 15778

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

Upvotes: 1

Robert Bailey
Robert Bailey

Reputation: 18210

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.

Upvotes: 0

Related Questions