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
- Log in to the CCE console.
- 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.
- 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.
- Basic Info: See Setting Basic Container Information.
- Lifecycle: See Setting Container Lifecycle Parameters.
- Health Check: See Setting Health Check for a Container.
- Environment Variables: See Setting an Environment Variable.
- Data Storage: See Overview.
- Security Context: Set container permissions to protect the system and other containers from being affected. Enter the user ID to set container permissions and prevent systems and other containers from being affected.
- Logging: See Container Logs.
- 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- Upgrade: See Configuring the Workload Upgrade Policy.
- Scheduling: See Scheduling Policy (Affinity/Anti-affinity).
- Instances Management Policies
For some distributed systems, the StatefulSet sequence is unnecessary and/or should not occur. These systems require only uniqueness and identifiers.
- OrderedReady: The StatefulSet will deploy, delete, or scale pods in order and one by one. (The StatefulSet continues only after the previous pod is ready or deleted.) This is the default policy.
- Parallel: The StatefulSet will create pods in parallel to match the desired scale without waiting, and will delete all pods at once.
- Labels and Annotations: See Pod Labels and Annotations.
- DNS: See DNS Configuration.
- APM Settings: See Configuring APM Settings for Performance Bottleneck Analysis.
- 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.
- Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
- 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: RollingUpdatevi 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 - 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
- If the workload will be accessed through a ClusterIP or NodePort Service, set the corresponding workload access type. For details, see Networking.
Last Article: Creating a Deployment
Next Article: Creating a DaemonSet
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.