When a container in a stateful service fails, its data can be lost. Persistent storage prevents this data loss. This topic explains how to use a statically provisioned cloud disk volume for persistent storage.
Prerequisites
Complete the following tasks:
Background information
Common use cases for cloud disks include:
-
Data storage services that require high disk I/O and do not require data sharing, such as MySQL and Redis.
-
High-speed log writes.
-
Persisting data beyond a pod's lifecycle.
If you have an existing cloud disk, use a statically provisioned volume.
To use a statically provisioned cloud disk volume, you must manually create a Persistent Volume (PV) and a Persistent Volume Claim (PVC). For more information, see Use a statically provisioned cloud disk volume.
Limits
-
Cloud disks are not shared and can be mounted to only one pod at a time.
-
A cloud disk can be mounted only to a node in the same zone.
Create a PV
-
Create a file named pv-static.yaml.
apiVersion: v1 kind: PersistentVolume metadata: name: <your-disk-id> labels: alicloud-pvname: <your-disk-id> failure-domain.beta.kubernetes.io/zone: <your-zone> failure-domain.beta.kubernetes.io/region: <your-region> spec: capacity: storage: 20Gi accessModes: - ReadWriteOnce flexVolume: driver: "alicloud/disk" fsType: "ext4" options: volumeId: "<your-disk-id>"Note-
alicloud-pvname: <your-disk-id>: The name of the PV, which must be the same as your cloud disk ID. -
failure-domain.beta.kubernetes.io/zone: <your-zone>: The zone where the cloud disk is located. For example, cn-hangzhou-b. -
failure-domain.beta.kubernetes.io/region: <your-region>: The region where the cloud disk is located. For example, cn-hangzhou.
If your cluster spans multiple zones, you must specify
failure-domain.beta.kubernetes.io/zoneandfailure-domain.beta.kubernetes.io/region. This ensures pods are scheduled to the same zone as the cloud disk. -
-
Run the following command to create the PV.
kubectl create -f pv-static.yamlVerification Result
Log on to the Container Service Management Console.
-
In the left-side navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster, or click Details in the Actions column.
-
In the left-side navigation pane of the cluster management page, choose . You can view the newly created PV in the list. The capacity is 20Gi, the access mode is ReadWriteOnce, the reclaim policy is Retain, and the status is Available. The PV will be bound to the Persistent Volume Claim pvc-disk in the default namespace. The Actions column provides options to Manage Tags, View YAML, and Delete.
Create a PVC
-
Create a file named pvc-static.yaml.
kind: PersistentVolumeClaim apiVersion: v1 metadata: name: pvc-disk spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi selector: matchLabels: alicloud-pvname: <your-disk-id> -
Run the following command to create the PVC.
kubectl create -f pvc-static.yamlVerification Result
Log on to the Container Service Management Console.
-
In the left-side navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster, or click Details in the Actions column.
-
In the left-side navigation pane of the cluster management page, choose .
-
On the Persistent Volume Claims page, you can view the PVC you created.
Create an application
-
Create a file named static.yaml.
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-static labels: app: nginx spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx volumeMounts: - name: disk-pvc mountPath: "/data" volumes: - name: disk-pvc persistentVolumeClaim: claimName: pvc-disk -
Run the following command to create the application.
kubectl create -f static.yamlVerification Result
Log on to the Container Service Management Console.
-
In the left-side navigation pane, click Clusters.
-
On the Clusters page, click the name of the target cluster, or click Details in the Actions column.
-
In the left-side navigation pane of the cluster management page, choose .
-
On the Deployments page, you can view the application you created.
Verify data persistence
-
Run the following command to get the application pod's name.
kubectl get pod | grep staticExpected output:
nginx-static-78c7dcb9d7-g**** 2/2 Running 0 32s -
Run the following command to verify that the cloud disk is mounted to the /data path.
kubectl exec nginx-static-78c7dcb9d7-g**** -- df | grep dataExpected output:
/dev/vdf 20511312 45080 20449848 1% /data -
Run the following command to list the files in the /data path.
kubectl exec nginx-static-78c7dcb9d7-g**** -- ls /dataExpected output:
lost+found -
Run the following command to create a file named static in the /data path.
kubectl exec nginx-static-78c7dcb9d7-g**** -- touch /data/static -
Run the following command to list the files in the /data path.
kubectl exec nginx-static-78c7dcb9d7-g**** -- ls /dataExpected output:
static lost+found -
Run the following command to delete the pod named
nginx-static-78c7dcb9d7-g****.kubectl delete pod nginx-static-78c7dcb9d7-g****Expected output:
pod "nginx-static-78c7dcb9d7-g****" deleted -
In a separate terminal window, run the following command to watch Kubernetes delete and recreate the pod.
kubectl get pod -w -l app=nginxExpected output:
NAME READY STATUS RESTARTS AGE nginx-static-78c7dcb9d7-g**** 2/2 Running 0 50s nginx-static-78c7dcb9d7-g**** 2/2 Terminating 0 72s nginx-static-78c7dcb9d7-h**** 0/2 Pending 0 0s nginx-static-78c7dcb9d7-h**** 0/2 Pending 0 0s nginx-static-78c7dcb9d7-h**** 0/2 Init:0/1 0 0s nginx-static-78c7dcb9d7-g**** 0/2 Terminating 0 73s nginx-static-78c7dcb9d7-h**** 0/2 Init:0/1 0 5s nginx-static-78c7dcb9d7-g**** 0/2 Terminating 0 78s nginx-static-78c7dcb9d7-g**** 0/2 Terminating 0 78s nginx-static-78c7dcb9d7-h**** 0/2 PodInitializing 0 6s nginx-static-78c7dcb9d7-h**** 2/2 Running 0 8s -
Run the following command to get the name of the recreated pod.
kubectl get podExpected output:
NAME READY STATUS RESTARTS AGE nginx-static-78c7dcb9d7-h**** 2/2 Running 0 14s -
Run the following command to list the files in the /data path. The static file you created still exists, which confirms that the data on the statically provisioned cloud disk is persistent.
kubectl exec nginx-static-78c7dcb9d7-h**** -- ls /dataExpected output:
static lost+found