Volume Mounts for main directories of containers like /mnt, /dev, /var to volumes

Viewed 34

Can we mount directly the main dirs of containers to volumes as part of kubernetes podspec. For ex:

  • /mnt
  • /dev
  • /var

All files and subdir of /mnt, /dev, /var should be mounted to volumes as part of podspec.

How can we do this?

1 Answers

For development purposes, you can create a hostPath Persistent Volume, but if you want to implement this for production, I strongly recommend you to use some NFS.

Here you have an example on how to use a NFS in a Pod definition:

kind: Pod
apiVersion: v1
metadata:
  name: nfs-in-a-pod
spec:
  containers:
    - name: app
      image: alpine
      volumeMounts:
        - name: nfs-volume
          mountPath: /var/nfs # change the destination you like the share to be mounted to
  volumes:
    - name: nfs-volume
      nfs:
        server: nfs.example.com # change this to your NFS server
        path: /share1 # change this to the relevant share

And here an example of a hostPath Persistent Volume:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

In the hostPath example, all files inside /mnt/data will be mounted as /usr/share/nginx/html in the Pod.

Related