<p>I'm trying to deploy a postgres service to google cloud kubernetes with a persistent volume and persistent volume claim to provide storage for my application.</p>

<p>When I deploy, the pod gets stuck in a <code>CrashLoopBackOff</code>.</p>

<p>One of the pod's events fails with the message:</p>

<p><code>Error: failed to start container "postgres": Error response from daemon: error while creating mount source path '/data/postgres-pv': mkdir /data: read-only file system</code></p>

<p>This is the yaml I am trying to deploy using kubectl:</p>

<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  labels:
    type: local
    app: postgres
spec:
  capacity:
    storage: 5Gi
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /data/postgres-pv
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pvc
  labels:
    type: local
    app: postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: postgres-pv
---
apiVersion: v1
kind: Secret
metadata:
  name: postgres-credentials
type: Opaque
data: 
  user: YWRtaW4=
  password: password==
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres-container
          image: postgres:9.6.6
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-credentials
                  key: password   
            - name: POSTGRES_DB
              value: kubernetes_django
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgres-volume-mount
      volumes:
        - name: postgres-volume-mount
          persistentVolumeClaim:
            claimName: postgres-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432
  selector:
   app: postgres
</code></pre>

<p>Nothing fails to deploy, but the pod gets stuck in a CrashLoopBackOff.</p>

<p>Thanks for the help!</p>

wc_coder
<p>I had the same problem. Following <a href="https://medium.com/@markgituma/kubernetes-local-to-production-with-django-1-introduction-d73adc9ce4b4" rel="nofollow noreferrer">this tutorial</a> I got it all to work with <code>minikube</code> but it gave the same error on GCP. </p>

<p>As mentioned by Patrick W, <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#types-of-persistent-volumes" rel="nofollow noreferrer">the docs</a> say: </p>

<blockquote>
  <h1>Types of Persistent Volumes</h1>
  
  <ul>
  <li>...</li>
  <li>HostPath (Single node testing only – local
  storage is not supported in any way and WILL NOT WORK in a multi-node
  cluster)</li>
  <li>...</li>
  </ul>
</blockquote>

<p>To solve this, I found a solution in the kubernetes <a href="https://docs.okd.io/latest/install_config/persistent_storage/persistent_storage_gce.html" rel="nofollow noreferrer">docs</a></p>

<p>You'll first have to create a <code>gcePersistentDisk</code>:</p>

<pre><code>gcloud compute disks create --size=[SIZE] --zone=[ZONE] [DISK_NAME]
</code></pre>

<p>and then the configuration as described in the link should do the trick:</p>

<pre><code>kind: PersistentVolume
apiVersion: v1
metadata:
  name: postgres-pv
  labels:
    type: local
spec:
  capacity:
    storage: 4Gi
  storageClassName: standard
  accessModes:
    - ReadWriteMany
  gcePersistentDisk:
    pdName:data-disk
    fsType: ext4
    readOnly: true
</code></pre>
