Container storage via a container's root file system is ephemeral, and can disappear upon container deletion and creation. To provide a durable location to store data and prevent it from being lost, you can create and use persistent volumes to store data outside of containers.
You can define and apply a persistent volume claim to your cluster, which in turn creates a persistent volume that's bound to the claim. A claim is a block storage volume in the underlying IaaS provider that's durable and offers persistent storage, enabling your data to remain intact, regardless of whether the containers that the storage is connected to are terminated.
A persistent volume claim (PVC) is a request for storage, similar to how a pod requests compute resources. A PVC provides an abstraction layer to underlying storage. For example, an administrator could create a number of static persistent volumes (PVs) that can later be bound to one or more persistent volume claims. This is analogous to an administrator creating cluster nodes to which pods are later assigned. If none of the static persistent volumes match the user's PVC request, the cluster may attempt to dynamically create a PV that matches the PVC request. This example uses the latter approach, and it assumes that the cluster administrator has not created any suitable PVs that match the PVC request—meaning that the PVCs will dynamically create the PVs for this example.
The minimum amount of persistent storage that a PVC can request is 50 gigabytes. If the request is for less than 50 gigabytes, the request is rounded up to 50 gigabytes.
The following YAML defines two PVCs that each request 50 gigabytes of persistent storage (
storage: 50Gi). You use names of the PVCs (for example, mysqlclaim) when defining which claims to use as the volumes of a deployment.
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysqlclaim spec: storageClassName: "oci" selector: matchLabels: failure-domain.beta.kubernetes.io/zone: "US-ASHBURN-AD-1" accessModes: - ReadWriteOnce resources: requests: storage: 50Gi --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: wordpressclaim spec: storageClassName: "oci" selector: matchLabels: failure-domain.beta.kubernetes.io/zone: "US-ASHBURN-AD-2" accessModes: - ReadWriteOnce resources: requests: storage: 50Gi
In the previous example, the PVCs request storage in availability domains in the Ashburn region using
matchLabels:failure-domain.beta.kubernetes.io/zone. Note that when you specify values for
matchLabels:failure-domain.beta.kubernetes.io/zone, you must use the shortened versions of availability domain names as follows:
Enter the following command to create the PVC from the YAML file:
$ kubectl create -f https://raw.githubusercontent.com/wercker/oke_examples/master/kubernetes_examples/persistent_volume_claims.yaml persistentvolumeclaim "mysqlclaim" created persistentvolumeclaim "wordpressclaim" created
You can verify that the PVCs have been created and bound to persistent volumes by calling
kubectl get pvc:
$ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE mysqlclaim Bound abyhqljrerxpanjto7b5zlxjesy4aedghc5c52f5v43xcrymo77ktdl6ibjq 50Gi RWO oci 4m wordpressclaim Bound abyhqljt3rzldcclootxn7yrfgv36s7rnggcobennjohevykqpitzkinspka 50Gi RWO oci 4m
You can use these persistent volumes when creating other objects, such as deployments. For example, the following deployment definition instructs the system to use the mysqlclaim PVC as the mysql-persistent-storage volume, which is mounted by pods hosting the deployment as /var/lib/mysql.
#MySQL Deployment apiVersion: extensions/v1beta1 kind: Deployment metadata: name: mysql labels: app: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - image: mysql:5.6 name: mysql env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql key: password ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysqlclaim