• Cloud Container Engine

cce
  1. Help Center
  2. Cloud Container Engine
  3. API Reference 2.0
  4. Kubernetes API
  5. StatefulSet
  6. Creating a StatefulSet

Creating a StatefulSet

Function

This API is used to create a StatefulSet resource object.

URI

POST /apis/apps/v1/namespaces/{namespace}/statefulsets

POST /apis/apps/v1beta1/namespaces/{namespace}/statefulsets

Table 1 describes the parameters of this API.

Table 1 Parameter description

Parameter

Mandatory

Description

namespace

Yes

Object name and auth scope, such as for teams and projects.

pretty

No

If 'true', then the output is pretty printed.

Request

Request parameters:

Table 2 Request parameter

Parameter

Mandatory

Type

Description

apiVersion

Yes

String

APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values.

kind

Yes

String

Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated.

metadata

Yes

3.1 Table11 Data structure of the metadata field

Standard list metadata.

spec

Yes

Table 4

Spec defines the desired identities of pods in this set.

status

No

Table 3

Status is the current status of Pods in this StatefulSet. This data may be out of date by some window of time.

Table 3 Data structure of the status field

Parameter

Mandatory

Type

Description

observedGeneration

No

Integer

Most recent generation observed by this autoscaler.

replicas

No

Integer

Replicas is the number of actual replicas.

currentReplicas

No

Integer

currentReplicas is the number of Pods created by the StatefulSet controller from the StatefulSet version indicated by currentRevision.

currentRevision

No

String

currentRevision, if not empty, indicates the version of the StatefulSet used to generate Pods in the sequence [0,currentReplicas).

readyReplicas

No

Integer

readyReplicas is the number of Pods created by the StatefulSet controller that have a Ready Condition.

updateRevision

No

String

updateRevision, if not empty, indicates the version of the StatefulSet used to generate Pods in the sequence [replicas-updatedReplicas,replicas)

updatedReplicas

No

Integer

updatedReplicas is the number of Pods created by the StatefulSet controller from the StatefulSet version indicated by updateRevision.

Table 4 Data structure of the spec field

Parameter

Mandatory

Type

Description

replicas

No

Integer

Replicas is the desired number of replicas of the given Template. These are replicas in the sense that they are instantiations of the same Template, but individual replicas also have a consistent identity. If unspecified, defaults to 1.

podManagementPolicy

No

String

podManagementPolicy controls how pods are created during initial scale up, when replacing pods on nodes, or when scaling down. The default policy is OrderedReady, where pods are created in increasing order (pod-0, then pod-1, etc) and the controller will wait until each pod is ready before continuing. When scaling down, the pods are removed in the opposite order. The alternative policy is Parallel which will create pods in parallel to match the desired scale without waiting, and on scale down will delete all pods at once.

revisionHistoryLimit

No

Integer

revisionHistoryLimit is the maximum number of revisions that will be maintained in the StatefulSet's revision history. The revision history consists of all revisions not represented by a currently applied StatefulSetSpec version. The default value is 10.

updateStrategy

No

Table 8

updateStrategy indicates the StatefulSetUpdateStrategy that will be employed to update Pods in the StatefulSet when a revision is made to Template.

serviceName

Yes

String

ServiceName is the name of the service that governs this StatefulSet. This service must exist before the StatefulSet, and is responsible for the network identity of the set. Pods get DNS/hostnames that follow the pattern: pod-specific-string.serviceName.default.svc.cluster.local where "pod-specific-string" is managed by the StatefulSet controller.

volumeClaimTemplates

No

Table 2

VolumeClaimTemplates is a list of claims that pods are allowed to reference. The StatefulSet controller is responsible for mapping network identities to claims in a way that maintains the identity of a pod. Every claim in this list must have at least one matching (by name) volumeMount in one container in the template. A claim in this list takes precedence over any volumes in the template, with the same name.

selector

Yes

Table 5

Selector is a label query over pods that should match the replica count. If empty, defaulted to labels on the pod template.

template

Yes

Table 7

Template is the object that describes the pod that will be created if insufficient replicas are detected. Each pod stamped out by the StatefulSet will fulfill this Template, but have a unique identity from the rest of the StatefulSet.

Table 5 Data structure of the selector field

Parameter

Mandatory

Type

Description

matchExpressions

No

Table 6

matchExpressions is a list of label selector requirements. The requirements are ANDed.

matchLabels

No

Object

MatchLabels is a map of {key,value} pairs. A single {key,value} in the matchLabels map is equivalent to an element of matchExpressions, whose key field is "key", the operator is "In", and the values array contains only "value". The requirements are ANDed.

Table 6 Data structure of the matchExpressions field

Parameter

Mandatory

Type

Description

key

No

String

key is the label key that the selector applies to.

operator

No

String

operator represents a key's relationship to a set of values. Valid operators are In, NotIn, Exists and DoesNotExist.

values

No

String

values is an array of string values. If the operator is In or NotIn, the values array must be non-empty. If the operator is Exists or DoesNotExist, the values array must be empty. This array is replaced during a strategic merge patch.

Table 7 Data structure of the PodTemplateSpec field

Parameter

Mandatory

Type

Description

metadata

Yes

Table 11

Standard object's metadata.

spec

Yes

Table 23

Specification of the desired behavior of the pod.

Table 8 Data structure of the updateStrategy field

Parameter

Mandatory

Type

Description

rollingUpdate

No

Table 9

RollingUpdate is used to communicate parameters when Type is RollingUpdateStatefulSetStrategyType.

type

No

String

Type indicates the type of the StatefulSetUpdateStrategy.

Table 9 Data structure of the rollingUpdate field

Parameter

Mandatory

Type

Description

partition

No

Integer

Partition indicates the ordinal at which the StatefulSet should be partitioned.

Example request:

{
    "apiVersion": "apps/v1beta1",
    "kind": "StatefulSet",
    "metadata": {
        "labels": {
            "name": "statefulset-test"
        },
        "name": "statefulset-test"
    },
    "spec": {
        "replicas": 1,
        "selector": {
            "matchLabels": {
                "name": "statefulset-test"
            }
        },
        "serviceName": "service-test",
        "template": {
            "metadata": {
                "labels": {
                    "name": "statefulset-test"
                }
            },
            "spec": {
                "containers": [
                    {
                        "image": "172.16.5.235:20202/test/nginx",
                        "name": "statefulset-test"
                    }
                ],
                "imagePullSecrets": [{
                    "name": "default-secret"
                }]
            }
        }
    }
}

Response

Response parameters:

For the description about response parameters, see 13.2 Table2 ResponseRequest parameter

Example response:

{
    "kind": "StatefulSet",
    "apiVersion": "apps/v1beta1",
    "metadata": {
        "name": "statefulset-test",
        "namespace": "default",
        "selfLink": "/apis/apps/v1beta1/namespaces/default/statefulsets/statefulset-test",
        "uid": "d9e1118c-fc20-11e7-9c3c-fa163eb8ad1a",
        "resourceVersion": "486277",
        "generation": 1,
        "creationTimestamp": "2018-01-18T07:26:08Z",
        "labels": {
            "name": "statefulset-test"
        },
        "enable": true
    },
    "spec": {
        "replicas": 1,
        "selector": {
            "matchLabels": {
                "name": "statefulset-test"
            }
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "name": "statefulset-test"
                },
                "enable": true
            },
            "spec": {
                "containers": [
                    {
                        "name": "statefulset-test",
                        "image": "172.16.5.235:20202/test/nginx",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File",
                        "imagePullPolicy": "Always"
                    }
                ],
                "restartPolicy": "Always",
                "terminationGracePeriodSeconds": 30,
                "dnsPolicy": "ClusterFirst",
                "securityContext": {},
                "imagePullSecrets": [
                    {
                        "name": "default-secret"
                    }
                ],
                "schedulerName": "default-scheduler"
            }
        },
        "serviceName": "service-test",
        "podManagementPolicy": "OrderedReady",
        "updateStrategy": {
            "type": "OnDelete"
        },
        "revisionHistoryLimit": 10
    },
    "status": {
        "replicas": 0
    }
}

Status Code

Table 10 describes the status code of this API.

Table 10 Status code

Status Code

Description

201

The request has been fulfilled, resulting in the creation of a new resource.

For the description about status codes, see Status Codes.