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.
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.
Parameter |
Description |
---|---|
Container Name |
Name the container. |
Pull Policy |
Image update or pull policy. If you select Always, the image is pulled from the image repository each time. If you do not select Always, the existing image of the node is preferentially used. If the image does not exist, the image is pulled from the image repository. |
Image Name |
Click Select Image and select the image used by the container. To use a third-party image, see Using Third-Party Images. |
Image Tag |
Select the image tag to be deployed. |
CPU Quota |
If Request and Limit are not specified, the quota is not limited. For more information and suggestions about Request and Limit, see Configuring Container Specifications. |
Memory Quota |
If Request and Limit are not specified, the quota is not limited. For more information and suggestions about Request and Limit, see Configuring Container Specifications. |
(Optional) GPU Quota |
Configurable only when the cluster contains GPU nodes and the CCE AI Suite (NVIDIA GPU) add-on is installed.
For details about how to use GPUs in the cluster, see Default GPU Scheduling in Kubernetes. |
(Optional) Privileged Container |
Programs in a privileged container have certain privileges. If Privileged Container is enabled, the container is assigned privileges. For example, privileged containers can manipulate network devices on the host machine and modify kernel parameters. |
(Optional) Init Container |
Whether to use the container as an init container. An init container does not support health check. An init container is a special container that runs before other app containers in a pod are started. Each pod can contain multiple containers. In addition, a pod can contain one or more init containers. Application containers in a pod are started and run only after the running of all init containers completes. For details, see Init Containers. |
Dynamic mounting is achieved by using the volumeClaimTemplates field and depends on the dynamic creation capability of StorageClass. A StatefulSet associates each pod with a PVC using the volumeClaimTemplates field, and the PVC is bound to the corresponding PV. Therefore, after the pod is rescheduled, the original data can still be mounted based on the PVC name.
To disable the standard output of the current workload, add the annotation kubernetes.AOM.log.stdout: [] in Labels and Annotations. For details about how to use this annotation, see Table 1.
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 Services.
(Optional) Service Settings
A Service provides external access for pods. With a static IP address, a Service forwards access traffic to pods and automatically balances load for these pods.
You can also create a Service after creating a workload. For details about Services of different types, see Overview.
For some distributed systems, the StatefulSet sequence is unnecessary and/or should not occur. These systems require only uniqueness and identifiers.
In this example, a Nginx workload is used and the EVS volume is dynamically mounted to it using the volumeClaimTemplates field.
nginx-statefulset.yaml is an example file name, and you can change it as required.
vi nginx-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: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: container-1
image: nginx:latest
imagePullPolicy: IfNotPresent
resources:
requests:
cpu: 250m
memory: 512Mi
limits:
cpu: 250m
memory: 512Mi
volumeMounts:
- name: test
readOnly: false
mountPath: /usr/share/nginx/html
subPath: ''
imagePullSecrets:
- name: default-secret
dnsPolicy: ClusterFirst
volumes: []
serviceName: nginx-svc
replicas: 2
volumeClaimTemplates: # Dynamically mounts the EVS volume to the workload.
- apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test
namespace: default
annotations:
everest.io/disk-volume-type: SAS # SAS EVS volume type.
labels:
failure-domain.beta.kubernetes.io/region: eu-de # region where the EVS volume is created.
failure-domain.beta.kubernetes.io/zone: # AZ where the EVS volume is created. It must be the same as the AZ of the node.
spec:
accessModes:
- ReadWriteOnce # The value must be ReadWriteOnce for the EVS volume.
resources:
requests:
storage: 10Gi
storageClassName: csi-disk # Storage class name. The value is csi-disk for the EVS volume.
updateStrategy:
type: RollingUpdate
vi nginx-headless.yaml
apiVersion: v1 kind: Service metadata: name: nginx-svc namespace: default labels: app: nginx spec: selector: app: nginx version: v1 clusterIP: None ports: - name: nginx targetPort: 80 nodePort: 0 port: 80 protocol: TCP type: ClusterIP
kubectl create -f nginx-statefulset.yaml
If the following information is displayed, the StatefulSet has been successfully created.
statefulset.apps/nginx created
kubectl create -f nginx-headless.yaml
If the following information is displayed, the headless service has been successfully created.
service/nginx-svc created