StatefulSet Management

Last updated: 2020-02-25 11:04:55

PDF

Overview

A StatefulSet is primarily used to manage stateful applications, and a created Pod has a persistent identifier for creation according to the specification. The identifier will remain after the Pod is migrated, terminated, or restarted. When you need persistent storage, you can match the storage volumes one to one based on the identifier. If your application does not require a persistent identifier, you are recommended to use a Deployment to deploy the application.

Operation Guide for StatefulSets in the Console

Create StatefulSet

  1. Log in to the TKE console and select Clusters In the left sidebar.

  2. Click the ID of the cluster where StatefulSet needs to be created to enter the cluster management page.

  3. Select * * workload * *-> * * StatefulSet * * to go to the StatefulSet management page.

  4. Click CREATE To go to Create Workload Page。
    Set the StatefulSet parameters based on actual needs.

    • Workload Name Enter a custom name.

    • Namespace : To be selected based on your requirements.

    • Type: Select "StatefulSet (run the Pod in a stateful manner)".

    • In-Pod containers: Set one or more different containers for a Pod of the StatefulSet based on actual needs.

      • Name: user-defined name.

      • Image: Select based on actual needs.

      • Image Tag Enter will be carried out according to the actual demand.

      • Image's pull strategy The following three strategies are provided, please choose as needed.
        If Image's pull strategy is not set, when Image Tag is empty or latest The Always policy is used, otherwise the IfNotPresent policy is used.

        • ALWAYS Always pull the Image remotely.
        • IfNotPresent Local Image is used by default. If there is no local Image, the Image will be pulled remotely.
        • NEVER Only use local Image. If you don't have the local Image, you will report an exception.
      • CPU/memory limits: Set the CPU and memory limit according to Kubernetes' resource limits to improve the robustness of the business.

      • Advanced settings: Parameters such as "working directory", "run commands", "run parameters", "container health check", and "privilege level" can be set.

    • Pod quantity: Select the adjustment method and set the Pod quantity based on actual needs.

  5. Click Create a workload to complete the creation.

Updating a StatefulSet

Updating YAML

  1. Log in to the TKE console.
  2. In the left sidebar, click Clusters to go to the cluster management page.
  3. Click the ID of the cluster where YAML needs to be updated to enter the cluster management page.
  4. Select "Workload" > "StatefulSet" to go to the StatefulSet information page. See the figure below:
    StatefulSet
  5. In the row of the StatefulSet for which to update the YAML, click Edit YAML to go to the StatefulSet updating page.
  6. On the "Update a StatefulSet" page, edit the YAML and click Finish to update the YAML.

Update Pod configuration

  1. On the cluster management page, click the ID of the cluster whose StatefulSet images need to be updated to enter the StatefulSet cluster management page.
  2. In the StatefulSet line where you need to update the Pod configuration, click "Update Pod configuration".
  3. On the Roll update an image Page, modify the update method and set the parameters based on actual needs.
  4. Click "finish" to update the Pod configuration.

Using kubectl to Manipulate StatefulSets

YAML Sample

apiVersion: v1
kind: Service  ## Create a Headless Service to control the network domain
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet ### Create a Nginx StatefulSet
metadata:
  name: web
  namespace: default
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: "nginx"
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "cbs"
      resources:
        requests:
          storage: 10Gi
  • kind: This identifies the StatefulSet resource type.
  • metadata: Basic information such as StatefulSet name and Label.
  • metadata.annotations: An additional description of the StatefulSet. You can set additional enhancements to TKE through this parameter.
  • spec.template: Detailed template configuration of the Pod managed by the StatefulSet.
  • spec.volumeClaimTemplates: Provide a templates for creating PVCs and PVs.

For more details about the parameters, see Kubernetes' official document about StatefulSet.

Creating a StatefulSet

  1. See the YAML sample to prepare the StatefulSet YAML file.
  2. Install kubectl and connect to a cluster.
  3. Run the following command to create the StatefulSet YAML file.
    kubectl create -f StatefulSet YAML filename
    For example, to create a StatefulSet YAML file named web.yaml, run the following command:
    kubectl create -f web.yaml
  4. Run the following command to verify whether the creation is successful.
    kubectl get StatefulSet
    If a message similar to the one below is returned, the creation is successful.
    NAME      DESIRED   CURRENT   AGE
    test      1         1         10s

Updating a StatefulSet

Run the following command to view the update policy type of the StatefulSet.

kubectl get ds/<daemonset-name> -o go-template='{{.spec.updateStrategy.type}}{{"\n"}}'

StatefulSet has the following two update policy types:

  • OnDelete: This is the default update policy. With this policy, after the StatefulSet is updated, you have to manually delete the old StatefulSet Pod to create a new one.
  • RollingUpdate: This update policy is supported in Kubernetes v1.7 or higher. With this policy, after the StatefulSet template is updated, the old StatefulSet Pod will be terminated, and a new StatefulSet Pod will be created in a rolling update manner (only for Kubernetes v1.7 or higher).

Method 1

Run the following command to update a StatefulSet.

kubectl edit StatefulSet/[name]

This method is good for testing and verification, but not production.. Also, You can update StatefulSet parameters using this method.

Method 2

Run the following command to update the image of the specified container.

kubectl patch statefulset <NAME> --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"<newImage>"}]'

It is recommended to keep other StatefulSet parameters unchanged and only update the container image when the business is updated.

If the StatefulSet is roll updated, you can view the update status by running the following command:

kubectl rollout status sts/<StatefulSet-name>

Deleting a StatefulSet

Run the following command to delete a StatefulSet.

kubectl delete  StatefulSet [NAME] --cascade=false

The --cascade=false parameter indicates that Kubernetes only deletes the StatefulSet but not the Pods. To delete a Pod, run the following command:

kubectl delete  StatefulSet [NAME]

For more information about StatefulSet operations, see Kubernetes' official guide.