Use a Persistent Volume Claim#

In this lab you’ll configure the mysite pod to use the PersistentVolumeClaim that you made in the last lab, Make a Persistent Volume Claim. Using a PersistentVolumeClaim ensures that your valuable user data is kept separately from your pods.

Update the Pod Definition#

There are two fragments that you must add to your pod definition in deployment/postgres-pod.yaml. The first one goes under the spec key and ensures that the PersistentVolume is available to the machine running the pod:

- name: db
      claimName: db

The second fragment makes the volume available to a particular container. This one is siblings with image, ports and resources definition:

- name: db
  mountPath: /var/lib/postgresql

Redeploy the Pod#

You can’t simply update the pod with a volume mount. You have to delete and restart it:

$ kubectl delete pod/postgres
$ kubectl apply -f deployment/postgres-pod.yaml

Verify that the volume is present:

$ kubectl describe pod/postgres
      /var/lib/postgresql/data from db (rw)
      /var/run/secrets/ from default-token-vr2qm (ro)
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  db
    ReadOnly:   false
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-vr2qm
    Optional:    false