Prerequisites:

  • The virtual-kubelet node is deployed in the target Kubernetes cluster. Note that a serverless Kubernetes cluster is embedded with the virtual-kubelet node.

Step 1: Create a PV

Save the following sample code in a YAML file named pv.yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: d-2zehdahrwoa7srga8eqw
  labels:
    failure-domain.beta.kubernetes.io/zone: cn-beijing-h
    failure-domain.beta.kubernetes.io/region: cn-beijing
spec:
  capacity:
    storage: 20Gi
  storageClassName: disk
  accessModes:
    - ReadWriteOnce
  flexVolume:
    driver: "alicloud/disk"
    fsType: "ext4"
    options:
      volumeId: "d-2zehdahrwoa7srga8eqw"
Use the kubectl client to create a persistent volume (PV) based on the preceding configuration file.
# kubectl create -f pv.yaml
persistentvolume/d-2zehdahrwoa7srga8eqw created
# kubectl get pv d-2zehdahrwoa7srga8eqw
NAME                     CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
d-2zehdahrwoa7srga8eqw   20Gi       RWO            Retain           Available           disk

Step 2: Create a PVC

Save the following sample code in a YAML file named pvc.yaml:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-disk
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: disk
  resources:
    requests:
      storage: 20Gi

Use the kubectl client to create a persistent volume claim (PVC) based on the preceding configuration file and bind it to the created PV.

# kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-disk created
# kubectl get pvc pvc-disk
NAME       STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-disk   Bound    d-2zehdahrwoa7srga8eqw   20Gi       RWO            disk           8s

Step 3: Create an ECI

Save the following sample code in a YAML file named pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: test-pvc-disk-static
spec:
  nodeName: virtual-kubelet
  containers:
  - image: nginx:latest
    name: test-container
    volumeMounts:
    - mountPath: /cache-test
      name: cache-volume
  volumes:
  - name: cache-volume
    persistentVolumeClaim:
      claimName: pvc-disk

Use the kubectl client to create an Elastic Container Instance (ECI) based on the preceding configuration file.

# kubectl create -f pod.yaml
pod/test-pvc-disk-static created
# kubectl get pod test-pvc-disk-static
NAME                   READY   STATUS    RESTARTS   AGE
test-pvc-disk-static   1/1     Running   0          50s
# kubectl exec -it test-pvc-disk-static bash
root@test-pvc-disk-static:/# ls /cache-test/
lost+found