Static cloud disk volume for persistent storage - Flexvolume

Updated at:

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

  1. 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/zone and failure-domain.beta.kubernetes.io/region to ensure pods are scheduled in the same zone as the cloud disk.

  2. Create the PV.

    kubectl create -f pv-static.yaml

    Verification Result

    1. Log on to the Container Service Management Console.

    2. In the left-side navigation pane, click Clusters.

    3. On the Clusters page, click the target cluster, or click Details in the Actions column.

    4. In the left-side navigation pane of the cluster management page, choose Volumes > Persistent Volumes. The PV appears with capacity 20Gi, access mode ReadWriteOnce, reclaim policy Retain, and status Available.

Create a PVC

  1. 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>
  2. Create the PVC.

    kubectl create -f pvc-static.yaml

    Verification Result

    1. Log on to the Container Service Management Console.

    2. In the left-side navigation pane, click Clusters.

    3. On the Clusters page, click the target cluster, or click Details in the Actions column.

    4. In the left-side navigation pane of the cluster management page, choose Volumes > Persistent Volume Claims.

    5. The PVC appears on the Persistent Volume Claims page.

Create an application

  1. 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
  2. Create the application.

    kubectl create -f static.yaml

    Verification Result

    1. Log on to the Container Service Management Console.

    2. In the left-side navigation pane, click Clusters.

    3. On the Clusters page, click the target cluster, or click Details in the Actions column.

    4. In the left-side navigation pane of the cluster management page, choose Workloads > Deployments.

    5. The application appears on the Deployments page.

Verify data persistence

  1. Get the pod name.

    kubectl get pod | grep static

    Expected output:

    nginx-static-78c7dcb9d7-g****   2/2     Running     0          32s
  2. Verify that the cloud disk is mounted to /data.

    kubectl exec nginx-static-78c7dcb9d7-g**** -- df | grep data

    Expected output:

    /dev/vdf        20511312    45080  20449848   1% /data
  3. List the files in /data.

    kubectl exec nginx-static-78c7dcb9d7-g**** -- ls /data

    Expected output:

    lost+found
  4. Create a file named static in /data.

    kubectl exec nginx-static-78c7dcb9d7-g**** -- touch /data/static
  5. List the files in /data.

    kubectl exec nginx-static-78c7dcb9d7-g**** -- ls /data

    Expected output:

    static
    lost+found
  6. Delete the pod nginx-static-78c7dcb9d7-g****.

    kubectl delete pod nginx-static-78c7dcb9d7-g****

    Expected output:

    pod "nginx-static-78c7dcb9d7-g****" deleted
  7. In a separate terminal, watch Kubernetes delete and recreate the pod.

    kubectl get pod -w -l app=nginx

    Expected 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
  8. Get the name of the recreated pod.

    kubectl get pod

    Expected output:

    NAME                            READY   STATUS      RESTARTS   AGE
    nginx-static-78c7dcb9d7-h****   2/2     Running     0          14s
  9. 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 /data

    Expected output:

    static
    lost+found