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.
  • The disk controller is installed in the target Kubernetes cluster. Note that a managed Kubernetes cluster is embedded with the disk controller, whereas you need to manually install the component in a serverless Kubernetes cluster.

Step 1: Install the disk controller

If you have not installed the disk controller in your Kubernetes cluster, install it by using the following YAML file.

  • special.keyid: the ID of the key used for installing the disk controller.
  • special.keysecret: the secret of the key used for installing the disk controller.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: alicloud-disk-essd
provisioner: alicloud/disk
parameters:
  type: cloud_essd
---
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: alicloud-disk-efficiency
provisioner: alicloud/disk
parameters:
  type: cloud_efficiency

---
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: alicloud-disk-ssd
provisioner: alicloud/disk
parameters:
  type: cloud_ssd

---
kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
  name: alicloud-disk-available
provisioner: alicloud/disk
parameters:
  type: available

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: alicloud-disk-controller-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["list", "watch", "create", "update", "patch"]

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: alicloud-disk-controller
  namespace: kube-system

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: run-alicloud-disk-controller
subjects:
  - kind: ServiceAccount
    name: alicloud-disk-controller
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: alicloud-disk-controller-runner
  apiGroup: rbac.authorization.k8s.io

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: cloud-config
  namespace: kube-system
data:
  special.keyid: "xxx"
  special.keysecret: "xxx"

---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: alicloud-disk-controller
  namespace: kube-system
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: alicloud-disk-controller
    spec:
      serviceAccount: alicloud-disk-controller
      containers:
        - name: alicloud-disk-controller
          image: registry.cn-hangzhou.aliyuncs.com/acs/alicloud-disk-controller:v1.12.6.21-54d91d6-aliyun
          env:
          - name: ACCESS_KEY_ID
            valueFrom:
              configMapKeyRef:
                name: cloud-config
                key: special.keyid
          - name: ACCESS_KEY_SECRET
            valueFrom:
              configMapKeyRef:
                name: cloud-config
                key: special.keysecret
          volumeMounts:
            - name: cloud-config
              mountPath: /etc/kubernetes/
            - name: logdir
              mountPath: /var/log/alicloud/
      volumes:
        - name: cloud-config
          emptyDir: {}
        - name: logdir
          emptyDir: {}

Step 2: Create a PVC

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

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

Use the kubectl client to create a persistent volume claim (PVC) based on the preceding configuration file.

# kubectl create -f pvc_disk.yaml
persistentvolumeclaim/essd-disk-pvc created
# kubectl get pvc essd-disk-pvc
NAME           STATUS   VOLUME                   CAPACITY   ACCESS MODES   STORAGECLASS                     AGE
essd-disk-pvc   Bound    d-2ze19bgda4tmpl9wywzt   20Gi       RWX            alicloud-disk-ssd-cn-beijing-h   13s

Step 3: Create an ECI

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

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

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 created
# kubectl get pod test-pvc-disk
NAME            READY   STATUS    RESTARTS   AGE
test-pvc-disk   1/1     Running   0          38s
# kubectl exec -it test-pvc-disk bash
root@test-pvc-disk:/# ls /data/
lost+found

In this way, a disk can be dynamically created and mounted to the ECI in the Kubernetes cluster.