使用前提:

  • K8S集群中正确部署了virtual-kubelet(serverless Kubernetes 默认集成)

ECI可以使用FlexVolume挂载云盘,既支持已有的待挂载状态的云盘,也支持指定云盘大小来自动创建并挂载。

以下yaml展示挂载已有的待挂载状态的云盘,将以下内容保存为flexvolume_disk1.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: test-flexvolume-disk1
spec:
  nodeName: virtual-kubelet
  containers:
  - image: nginx:latest
    name: test-container
    volumeMounts:
    - mountPath: /cache-test
      name: cache-volume
  volumes:
  - name: cache-volume
    flexVolume:
      driver: alicloud/disk
      fsType: ext4
      options:
        volumeId: "d-2zehdahrwoa7srga8eqw"

然后使用kubectl创建实例:

# kubectl create -f flexvolume_disk1.yaml
pod/test-flexvolume-disk1 created
# kubectl get pod test-flexvolume-disk1
NAME                   READY   STATUS    RESTARTS   AGE
test-flexvolume-disk1   1/1     Running   0          45s
# kubectl exec -it test-flexvolume-disk1 bash
root@default-test-flexvolume-disk1:/# ls
bin  boot  cache-test  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@default-test-flexvolume-disk1:/# ls cache-test/
lost+found

以下yaml展示指定云盘大小来自动创建并挂载,将以下内容保存为flexvolume_disk2.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: test-flexvolume-disk2
spec:
  nodeName: virtual-kubelet
  containers:
  - image: nginx:latest
    name: test-container
    volumeMounts:
    - mountPath: /cache-test
      name: cache-volume
  volumes:
  - name: cache-volume
    flexVolume:
      driver: alicloud/disk
      fsType: ext4
      options:
          volumeType: "alicloud-disk-essd"
          volumeSize: "100Gi"

然后使用kubectl创建实例:

# kubectl create -f flexvolume_disk2.yaml
pod/test-flexvolume-disk2 created
# kubectl get pod test-flexvolume-disk2
NAME                   READY   STATUS    RESTARTS   AGE
test-flexvolume-disk2   1/1     Running   0          45s
# kubectl exec -it test-flexvolume-disk2 bash
root@default-test-flexvolume-disk2:/# ls
bin  boot  cache-test  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@default-test-flexvolume-disk2:/# ls cache-test/
lost+found