Help Center> Cloud Container Engine> User Guide> Old Console> Storage (CSI)> SFS Volumes> (kubectl) Creating a StatefulSet Mounted with an SFS Volume

(kubectl) Creating a StatefulSet Mounted with an SFS Volume

Scenario

CCE allows you to use an existing SGS volume to create a StatefulSet (by using a PVC).

Prerequisites

You have created a CCE cluster and installed the CSI plug-in (everest) in the cluster.

Notes and Constraints

The following configuration example applies to clusters of Kubernetes 1.15 or later.

Procedure

  1. Create an SFS volume by referring to Creating an SFS Volume and record the volume name.
  2. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  3. Create a YAML file for creating the workload. Assume that the file name is sfs-statefulset-example.yaml.

    touch sfs-statefulset-example.yaml

    vi sfs-statefulset-example.yaml

    Configuration example:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: sfs-statefulset-example
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: sfs-statefulset-example
      template:
        metadata:
          labels:
            app: sfs-statefulset-example
        spec:
          volumes: 
          - name: pvc-sfs-example 
            persistentVolumeClaim:
              claimName: pvc-sfs-example     
          containers:
          - name: container-0
            image: 'nginx:latest'
            volumeMounts:
              - name: pvc-sfs-example
                mountPath: /tmp
          restartPolicy: Always
          imagePullSecrets:
          - name: default-secret 
      serviceName: sfs-statefulset-example-headless
      updateStrategy:
        type: RollingUpdate
    Table 1 Key parameters

    Parent Parameter

    Parameter

    Description

    spec

    replicas

    Number of pods.

    metadata

    name

    Name of the new workload.

    spec.template.spec.containers

    image

    Image used by the workload.

    spec.template.spec.containers.volumeMounts

    mountPath

    Mount path of a container.

    spec

    serviceName

    Service corresponding to the workload. For details about how to create a Service, see Creating a StatefulSet.

    spec.template.spec.volumes.persistentVolumeClaim

    claimName

    Name of an existing PVC.

    Example of mounting an SFS volume to a StatefulSet (PVC template-based, dedicated volume):

    Example YAML file:
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: sfs-statefulset-example
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: sfs-statefulset-example
      template:
        metadata:
          labels:
            app: sfs-statefulset-example
        spec:
          containers:
            - name: container-0
              image: 'nginx:latest'
              volumeMounts:
                - name: pvc-sfs-auto-example
                  mountPath: /tmp
          restartPolicy: Always
          imagePullSecrets:
            - name: default-secret
      volumeClaimTemplates:
        - metadata:
            name: pvc-sfs-auto-example
            namespace: default
          spec:
            accessModes:
              - ReadWriteMany
            resources:
              requests:
                storage: 10Gi
            storageClassName: csi-nas
      serviceName: sfs-statefulset-example-headless
      updateStrategy:
        type: RollingUpdate

    spec.template.spec.containers.volumeMounts.name and spec.template.spec.volumes.name must be consistent because they have a mapping relationship.

  4. Create a StatefulSet.

    kubectl create -f sfs-statefulset-example.yaml

Verifying Persistent Storage of an SFS Volume

  1. Query the pod and SFS volume of the deployed workload (for example, sfs-statefulset-example).

    1. Run the following command to query the pod name of the workload:
      kubectl get po | grep sfs-statefulset-example

      Expected outputs:

      sfs-statefulset-example-0   1/1     Running   0          2m5s
    2. Run the following command to check whether an SFS volume is mounted to the /tmp directory:
      kubectl exec sfs-statefulset-example-0 -- mount|grep /tmp

      Expected outputs:

      sfs-nas01.cn-north-4.myhuaweicloud.com:/share-c56b9aa4 on /tmp type nfs (rw,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,nolock,noresvport,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=10.79.96.32,mountvers=3,mountport=2050,mountproto=tcp,local_lock=all,addr=10.79.96.32)

  2. Run the following command to create a file named test in the /tmp directory:

    kubectl exec sfs-statefulset-example-0 -- touch /tmp/test

  3. Run the following command to view the file in the /tmp directory:

    kubectl exec sfs-statefulset-example-0 -- ls -l /tmp

    Expected outputs:

    -rw-r--r-- 1 root root     0 Jun  1 02:50 test

  4. Run the following command to delete the pod named sfs-statefulset-example-0:

    kubectl delete po sfs-statefulset-example-0

  5. Check whether the data file in the SFS volume still exists after the pod is rebuilt.

    1. Run the following command to query the name of the rebuilt pod:
      kubectl get po

      Expected outputs:

      sfs-statefulset-example-0   1/1     Running   0          2m
    2. Run the following command to view the file in the /tmp directory:
      kubectl exec sfs-statefulset-example-0 -- ls -l /tmp

      Expected outputs:

      -rw-r--r-- 1 root root     0 Jun  1 02:50 test
    3. The test file still exists after the pod is rebuilt, indicating that the data in the SFS volume can be persistently stored.