Search code examples
kubernetesgoogle-cloud-platformkubernetes-podpersistent-volumes

Pod crashes when applying attaching persistent volume to YAML file


Here is my persistent volume definition

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

Here is my persistent volume claim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

Here is the pod I'm trying to deploy

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

When I try to deploy the pod using kubectl create -f task-pv-pod.yaml, I get this error

failed to start container "52c5f707bb90d87b4178e8508d710ae0912d8ee7bdd7c4b9b802bd6b35f266de": Error response from daemon: error while creating mount source path '/mnt/data': mkdir /mnt/data: read-only file system: RunContainerError

I am following this guide https://kubernetes.io/docs/tasks/configure-pod-container/configure-persistent-volume-storage/, I have another pod running with a different application, and wanted to apply persistent storage to that pod once I had this one up and running.


Solution

  • While defining Persistent Volume you are using type: local . This means that you want to create directory in /mnt. Local do not support dynamic volume provisioning. For example when you will SSH to any of your nodes you will find that this folder is ReadOnly file system.

    /mnt $ mkdir something mkdir: cannot create directory ‘something’: Read-only file system
    

    You just could change in your PV YAML

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: task-pv-volume
      labels:
        type: local
    spec:
      storageClassName: manual
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteMany
      hostPath:
        path: "/var/lib/data"
    

    Notice changes in accessMode and path. Also in your PVC definition change:

      accessModes:
        - ReadWriteMany
    

    Remember that must delete old PV and PVC (if they wont vanish you will probably need redeploy nginx pod also) as in some resources you cannot change values after creation.

    Take a look: read-only-fs.

    Read: gke-dynamics-provisioning.