Kubernetes nfs define path in claim

11/11/2016

I want to create common persistence volume with nfs.

PV(nfs):

common-data-pv       1500Gi       RWO           Retain
192.168.0.24 /home/common-data-pv

I want a claim or pod(mount the claim) subscribed common-data-pv can define path example :

/home/common-data-pv/www-site-1(50GI)
/home/common-data-pv/www-site-2(50GI)

But i not found in documentation how i can define this.

My actual conf for pv :

kind: PersistentVolume
apiVersion: v1
metadata:
  name: common-data-pv
  labels:
    type: common
spec:
  capacity:
    storage: 1500Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 192.168.122.1
    path: "/home/pv/common-data-pv"



kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: common-data-pvc
  namespace: kube-system
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      type: common

Example use:

apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-web-1
  namespace: kube-system
spec:
  replicas: 2
  selector:
    role: web-frontend
  template:
    metadata:
      labels:
        role: web-frontend
    spec:
      containers:
      - name: web
        image: nginx:alpine
        ports:
          - name: web
            containerPort: 80
        volumeMounts:
            # name must match the volume name below
            - name: nfs
              mountPath: "/usr/share/nginx/html"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: common-data-pvc

apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-web-2
  namespace: kube-system
spec:
  replicas: 2
  selector:
    role: web-frontend
  template:
    metadata:
      labels:
        role: web-frontend
    spec:
      containers:
      - name: web
        image: nginx:alpine
        ports:
          - name: web
            containerPort: 80
        volumeMounts:
            # name must match the volume name below
            - name: nfs
              mountPath: "/usr/share/nginx/html"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: common-data-pvc
-- timactive
kubernetes

1 Answer

11/16/2016

To use the claim you just need to add a volumeMounts section and volumes to your manifest. Here's an example replication controller for nginx that would use your claim. Note the very last line that uses the same PVC name.

apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-web
  namespace: kube-system
spec:
  replicas: 2
  selector:
    role: web-frontend
  template:
    metadata:
      labels:
        role: web-frontend
    spec:
      containers:
      - name: web
        image: nginx:alpine
        ports:
          - name: web
            containerPort: 80
        volumeMounts:
            # name must match the volume name below
            - name: nfs
              mountPath: "/usr/share/nginx/html"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: common-data-pvc

More examples can be found in the kubernetes repo under examples

-- Justin Garrison
Source: StackOverflow