Static cloud disk volume for persistent storage - Flexvolume
When a container crashes, business data in stateful workloads can be lost or become unreliable. Persistent storage addresses this risk. This topic explains how to use a statically provisioned cloud disk volume for persistent storage with Flexvolume.
Prerequisites
Complete the following tasks:
Background
Cloud disk use cases:
-
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.
Manually create a Persistent Volume (PV) and Persistent Volume Claim (PVC) to use a statically provisioned cloud disk volume.
Limitations
-
A cloud disk 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 PV name. Must match your cloud disk ID. -
failure-domain.beta.kubernetes.io/zone: <your-zone>: The cloud disk zone, for example, cn-hangzhou-b. -
failure-domain.beta.kubernetes.io/region: <your-region>: The cloud disk region, for example, cn-hangzhou.
For multi-zone clusters, specify
failure-domain.beta.kubernetes.io/zoneandfailure-domain.beta.kubernetes.io/regionto ensure pods are scheduled in the same zone as the cloud disk. -
-
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 target cluster, or click Details in the Actions column.
-
In the left-side navigation pane of the cluster management page, choose . The PV appears with capacity 20Gi, access mode ReadWriteOnce, reclaim policy Retain, and status Available.
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> -
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 target cluster, or click Details in the Actions column.
-
In the left-side navigation pane of the cluster management page, choose .
-
The PVC appears on the Persistent Volume Claims page.
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 -
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 target cluster, or click Details in the Actions column.
-
In the left-side navigation pane of the cluster management page, choose .
-
The application appears on the Deployments page.
Verify data persistence
-
Get the pod name.
kubectl get pod | grep staticExpected output:
nginx-static-78c7dcb9d7-g**** 2/2 Running 0 32s -
Verify that the cloud disk is mounted to /data.
kubectl exec nginx-static-78c7dcb9d7-g**** -- df | grep dataExpected output:
/dev/vdf 20511312 45080 20449848 1% /data -
List the files in /data.
kubectl exec nginx-static-78c7dcb9d7-g**** -- ls /dataExpected output:
lost+found -
Create a file named static in /data.
kubectl exec nginx-static-78c7dcb9d7-g**** -- touch /data/static -
List the files in /data.
kubectl exec nginx-static-78c7dcb9d7-g**** -- ls /dataExpected output:
static lost+found -
Delete the pod
nginx-static-78c7dcb9d7-g****.kubectl delete pod nginx-static-78c7dcb9d7-g****Expected output:
pod "nginx-static-78c7dcb9d7-g****" deleted -
In a separate terminal, 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 -
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 -
List the files in /data. The static file still exists, confirming that data on the cloud disk persists across pod restarts.
kubectl exec nginx-static-78c7dcb9d7-h**** -- ls /dataExpected output:
static lost+found