All Products
Search
Document Center

Mount an NFS volume

Last Updated: Sep 27, 2021

This topic describes how to mount a Network File System (NFS) volume to an elastic container instance.

Prerequisites

  • Virtual Kubelet is deployed in a Kubernetes cluster.

    Alibaba Cloud Serverless Kubernetes (ASK) clusters are integrated with Virtual Kubelet. For other types of clusters, you must deploy Virtual Kubelet in the clusters. For more information, see Connect Kubernetes to Elastic Container Instance.

  • A mount point for the NFS volume is created.

    The default mount directory for NFS volumes is /. If you want to specify a mount directory, create a subdirectory in NFS.

Configuration examples

You can mount an NFS volume to an elastic container instance in the same way that you mount an NFS volume to a Kubernetes cluster. The following examples show how to mount an NFS volume:

Example 1: Mount an NFS volume to an elastic container instance without specifying a subdirectory

  1. Prepare a YAML configuration file.

    vim nfs.yaml

    The following example shows the content of the nfs.yaml file:

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-nfs
    spec:
      containers:
      - image: nginx:latest
        name: test-container
        volumeMounts:
        - mountPath: /cache-test
          name: cache-volume
      volumes:
      - name: cache-volume
        nfs:
          server: 1c5684****-suv0.cn-beijing.nas.aliyuncs.com # The mount point of the NFS volume
          path: /  
          readOnly: false
  2. Deploy a pod.

    kubectl create -f nfs.yaml
  3. View the mount result.

    Run the ls command to view the file directory in the pod. The mount directory /cache-test of the NFS directory / is generated in the pod. You can create subdirectories and write data to the files in the cache-test directory, as shown in the following figure.

    nfs1

Example 2: Mount an NFS volume to an elastic container instance by specifying a subdirectory

When you mount an NFS volume to an elastic container instance, you can specify a subdirectory as the mount directory.

  1. Prepare a YAML configuration file.

    vim nfs_subpath.yaml

    The following example shows the content of the nfs_subpath.yaml file:

    Notice

    If you want to specify a subdirectory as the mount directory, make sure that the subdirectory exists in NFS. The NFS volume that is used in the following example is the NFS volume in Example 1. In this NFS volume, a subdirectory subpath is created, and the content hello is written to the file named test in subpath.

    apiVersion: v1
    kind: Pod
    metadata:
      name: test-nfs-subpath
    spec:
      containers:
      - image: nginx:latest
        name: test-container
        volumeMounts:
        - mountPath: /cache-subtest
          name: cache-volume
          subPath: subpath # The subdirectory in NFS
      volumes:
      - name: cache-volume
        nfs:
          server: 1c568****-suv0.cn-beijing.nas.aliyuncs.com # The mount point of the NFS volume
          path: /
          readOnly: false
  2. Deploy a pod.

    kubectl create -f nfs_subpath.yaml
  3. View the mount result.

    Run the ls command to view the file directory in the pod. The mount directory /cache-subtest of the NFS subdirectory /subpath is generated in the pod. Because the file named test with the content of hello exists in the subpath subdirectory, you can find the file named test in the cache-subtest directory, as shown in the following figure.

    nfs2