Mounting a PersistentVolumeClaim to a Container
Once you have created a Persistent Volume Claim (PVC), you can mount it to a container in your pod. This is done by adding a volume to the pod specification and referencing the PVC.
Here's an example of a pod manifest that mounts a PVC to a container:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
volumeMounts:
- name: my-storage
mountPath: /data
volumes:
- name: my-storage
persistentVolumeClaim:
claimName: my-pvc
In this example, the pod has a single container that runs the Nginx web server. The container mounts the PVC named my-pvc
to the /data
directory inside the container.
The volumes
section of the pod specification defines the volume that will be mounted to the container. The persistentVolumeClaim
field references the PVC by its name, my-pvc
.
The volumeMounts
section of the container specification defines where the volume will be mounted inside the container. In this case, the volume is mounted to the /data
directory.
When the pod is created, Kubernetes will automatically provision a Persistent Volume (PV) that matches the requirements of the PVC and mount it to the container.
You can create the pod using the following command:
kubectl apply -f my-pod.yaml
Once the pod is running, you can verify that the PVC is mounted to the container by exec'ing into the container and checking the contents of the /data
directory.
By mounting a PVC to a container, you can ensure that your application has durable and reliable storage, even if the pod is rescheduled or restarted.