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.
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 unique PVC using the volumeClaimTemplates field, and the PVCs are bound to their corresponding PVs. Therefore, after the pod is rescheduled, the original data can still be mounted thanks to the PVC.
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 Service 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, an 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