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.
  • A Network File System (NFS) mount target is available.

Elastic Container Instance (ECI) provides native support for nfs volumes. You can use an nfs volume in an ECI in the same way as that in native Kubernetes. Save the following sample code in a YAML file named nfs.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: test-nfs
spec:
  nodeName: virtual-kubelet
  containers:
  - image: nginx:latest
    name: test-container
    volumeMounts:
    - mountPath: /cache-test
      name: cache-volume
  volumes:
  - name: cache-volume
    nfs:
      server: 133aa489f0-rvn26.cn-beijing.nas.aliyuncs.com
      path: /
      readOnly: false

Use the kubectl client to create an ECI based on the preceding configuration file.

# kubectl create -f nfs.yaml
pod/test-nfs created
# kubectl get pod test-nfs
NAME       READY   STATUS    RESTARTS   AGE
test-nfs   1/1     Running   0          50s
# kubectl exec -it test-nfs bash
root@default-test-nfs:/# 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-nfs:/# mkdir cache-test/subpath
root@default-test-nfs:/# echo hello > cache-test/subpath/test
root@default-test-nfs:/# cat cache-test/subpath/test
hello

In addition, ECI allows you to specify a subpath in an nfs volume. Save the following sample code in a YAML file named nfs_subpath.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: test-nfs-subpath
spec:
  nodeName: virtual-kubelet
  containers:
  - image: nginx:latest
    name: test-container
    volumeMounts:
    - mountPath: /cache-test
      name: cache-volume
      subPath: subpath
  volumes:
  - name: cache-volume
    nfs:
      server: 1a93e496ef-fuu9.cn-beijing.nas.aliyuncs.com
      path: /
      readOnly: false

Use the kubectl client to create an ECI based on the preceding configuration file.

# kubectl create -f nfs_subpath.yaml
pod/test-nfs-subpath created
# kubectl get pod test-nfs-subpath
NAME               READY   STATUS    RESTARTS   AGE
test-nfs-subpath   1/1     Running   0          2m26s
# kubectl exec -it test-nfs-subpath bash
root@test-nfs-subpath:/# ls
bin  boot  cache-test  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@test-nfs-subpath:/# ls cache-test/
test
root@test-nfs-subpath:/# cat cache-test/test
hello