Creating a StatefulSet

Scenario

StatefulSets are a type of workloads whose data or status is stored while they are running. For example, MySQL is a StatefulSet because it needs to store new data.

A container can be migrated between different hosts, but data is not stored on the hosts. To store StatefulSet data persistently, attach HA storage volumes provided by CCE to the container.

Prerequisites

  • Before creating a workload, you must have an available cluster. For details on how to create a cluster, see Buying a CCE Cluster.
  • To enable access to a workload from a public network, ensure that an elastic IP address (EIP) has been bound to or a load balancer has been configured for at least one node in the cluster.

    If a pod has multiple containers, ensure that the ports used by the containers do not conflict with each other. Otherwise, creating the StatefulSet will fail.

Using the CCE Console

  1. Log in to the CCE console.
  2. Click the cluster name to access its details page, choose Workloads on the left, and click the Create from Image tab in the upper right corner.
  3. Set basic information about the workload.

    Basic Info
    • Workload Type: Select StatefulSet. For details about workload types, see Overview.
    • Workload Name: Enter the name of the workload.
    • Namespace: Select the namespace of the workload. The default value is default. You can also click Create Namespace to create one. For details, see Creating a Namespace.
    • Pods: Enter the number of pods.
    • Container Runtime: A CCE cluster uses runC by default, whereas a CCE Turbo cluster supports both runC and Kata. For details about the differences between runC and Kata, see Secure Containers and Common Containers.
    • Time Zone Synchronization: Specify whether to enable time zone synchronization. After time zone synchronization is enabled, the container and node use the same time zone. The time zone synchronization function depends on the local disk mounted to the container. Do not modify or delete the time zone. For details, see Configuring Time Zone Synchronization.
    Container Settings
    • Container Information
      Multiple containers can be configured in a pod. You can click Add Container on the right to configure multiple containers for the pod.
    • Image Access Credential: Select the credential used for accessing the image repository. The default value is default-secret. You can use default-secret to access images in SWR. For details about default-secret, see default-secret.

    Headless Service Parameters

    A headless Service is used to solve the problem of mutual access between pods in a StatefulSet. The headless Service provides a fixed access domain name for each pod. For details, see Headless Service.

    Service Settings

    A Service is used for pod access. With a fixed IP address, a Service forwards access traffic to pods and performs load balancing for these pods.

    You can also create a Service after creating a workload. For details about the Service, see Overview.

    Advanced Settings

  4. Click Create Workload in the lower right corner.

Using kubectl

The following procedure uses an etcd workload as an example to describe how to create a workload using kubectl.

  1. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create and edit the etcd-statefulset.yaml file.

    etcd-statefulset.yaml is an example file name, and you can change it as required.

    vi etcd-statefulset.yaml

    The following provides an example of the file contents. For more information on StatefulSet, see the Kubernetes documentation.

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: etcd
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: etcd
      serviceName: etcd-svc
      template:
        metadata:
          labels:
            app: etcd
        spec:
          containers:
          - env:
            - name: PAAS_APP_NAME
              value: tesyhhj
            - name: PAAS_NAMESPACE
              value: default
            - name: PAAS_PROJECT_ID
              value: 9632fae707ce4416a0ab1e3e121fe555
            image: etcd # If you use an image from an open-source image registry, enter the image name. If you use an image in My Images, obtain the image path from SWR.
            imagePullPolicy: IfNotPresent
            name: container-0
      updateStrategy:
        type: RollingUpdate

    vi etcd-headless.yaml

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: etcd
      name: etcd-svc
    spec:
      clusterIP: None
      ports:
      - name: etcd-svc
        port: 3120
        protocol: TCP
        targetPort: 3120
      selector:
        app: etcd
      sessionAffinity: None
      type: ClusterIP

  3. Create a workload and the corresponding headless service.

    kubectl create -f etcd-statefulset.yaml

    If the following information is displayed, the StatefulSet has been successfully created.

    statefulset.apps/etcd created

    kubectl create -f etcd-headless.yaml

    If the following information is displayed, the headless service has been successfully created.

    service/etcd-svc created

  4. If the workload will be accessed through a ClusterIP or NodePort Service, set the corresponding workload access type. For details, see Networking.