Help Center/ Cloud Container Engine/ User Guide/ Old Console/ Storage (CSI)/ Deployment Examples/ OBS Volumes/ Creating a StatefulSet Mounted with an OBS Volume
Updated on 2024-11-11 GMT+08:00

Creating a StatefulSet Mounted with an OBS Volume

Scenario

CCE allows you to use an existing OBS volume to create a StatefulSet through 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 OBS volume by referring to PersistentVolumeClaims (PVCs) and obtain the PVC 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 obs-statefulset-example.yaml.

    touch obs-statefulset-example.yaml

    vi obs-statefulset-example.yaml

    Configuration example:

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

    Parameter

    Description

    replicas

    Number of pods.

    name

    Name of the new workload.

    image

    Image used by the workload.

    mountPath

    Mount path of a container.

    serviceName

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

    claimName

    Name of an existing PVC.

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

    Example YAML:

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: obs-statefulset-example
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: obs-statefulset-example
      template:
        metadata:
          labels:
            app: obs-statefulset-example
        spec:
          containers:
            - name: container-0
              image: 'nginx:latest'
              volumeMounts:
                - name: pvc-obs-auto-example
                  mountPath: /tmp
          restartPolicy: Always
          imagePullSecrets:
            - name: default-secret
      volumeClaimTemplates:
        - metadata:
            name: pvc-obs-auto-example
            namespace: default
            annotations:
              everest.io/obs-volume-type: STANDARD
          spec:
            accessModes:
              - ReadWriteMany
            resources:
              requests:
                storage: 1Gi
            storageClassName: csi-obs  
      serviceName: obs-statefulset-example-headless

  4. Create a StatefulSet.

    kubectl create -f obs-statefulset-example.yaml

Verifying Persistent Storage of an OBS Volume

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

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

      Expected outputs:

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

      Expected outputs:

      s3fs on /tmp type fuse.s3fs (rw,nosuid,nodev,relatime,user_id=0,group_id=0,allow_other)

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

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

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

    kubectl exec obs-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 obs-statefulset-example-0:

    kubectl delete po obs-statefulset-example-0

  5. Check whether the file 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:

      obs-statefulset-example-0   1/1     Running   0          2m
    2. Run the following command to view the file in the /tmp directory:
      kubectl exec obs-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 OBS volume can be persistently stored.