Kubernetes's Statefulset detailed

Source: Internet
Author: User
Tags gluster

Overview

RC, Deployment, Daemonset are non-state-oriented services, they manage the pod IP, name, start and stop order is random, and what is Statefulset? As the name implies, a stateful collection that manages all stateful services, such as MySQL, MongoDB clusters, and so on.
Statefulset is essentially a variant of the deployment, in order to solve the problem of stateful services, it manages the pod has a fixed pod name, start-stop order, in Statefulset, pod name is called network identity, Because it is communicating through pod names instead of pod IPs, you must also use shared storage, which has become a GA version in the v1.9 version.
In deployment, the service that corresponds to it is services, and in Statefulset the corresponding headless service,headless service, namely headless service, differs from service is that it does not have cluster IP, when resolving its name, returns the endpoint list of all pods corresponding to the headless service.
In addition, Statefulset created a DNS domain name for each pod copy controlled by statefulset on headless service, in the form of:

$(podname).(headless server name)   FQDN: $(podname).(headless server name).namespace.svc.cluster.local
Statefulset Example

Let's look at some examples to illustrate the features described above to deepen your understanding.

  ApiVersion:v1kind:Servicemetadata:name:nginx labels:app:nginxspec:ports:-port:80 Name:web Clusterip:none Selector:app:nginx---Apiversion:apps/v1kind:statefulsetmetadata:name:webspec:selector:ma  TchLabels:app:nginx # have to match. Spec.template.metadata.labels ServiceName: "Nginx" #声明它属于哪个Headless Service. Replicas:3 # By default is 1 Template:metadata:labels:app:nginx # have to match. Spec.selector.matchL Abels spec:terminationgraceperiodseconds:10 Containers:-Name:nginx image:k8s.gcr.io/nginx-s lim:0.8 ports:-containerport:80 name:web volumemounts:-Name:www Mount Path:/usr/share/nginx/html volumeclaimtemplates: #可看作pvc的模板-metadata:name:www spec:accessmodes: [" Readwriteonce "] Storageclassname:" Gluster-heketi "#存储类名, changed to Resources:requests:storag that already exists in the cluster E:1gi  

With this configuration file, you can see the components of Statefulset:

    • Headless Service: Named Nginx, used to define the pod network identity (DNS domain).
    • Statefulset: Defines a specific application, named Nginx, has three pod copies, and defines a domain name for each pod.
    • Volumeclaimtemplates: Creates a PVC, specifies the PVC name size, the PVC is automatically created, and the PVC must be supplied by the storage class.

Create:

$ kubectl create -f nginx.yaml service "nginx" createdstatefulset "web" created

Take a look at these three pod creation processes:

#第一个是创建web-0$ kubectl get podweb-0                     1/1       ContainerCreating   0          51s#待web-0 running且ready时,创建web-1$ kubectl get podweb-0                     1/1       Running             0          51sweb-1                     0/1       ContainerCreating   0          42s#待web-1 running且ready时,创建web-2$ kubectl get podweb-0                     1/1       Running             0          1mweb-1                     1/1       Running             0          45sweb-2                     1/1       ContainerCreating   0          36s#最后三个Pod全部running且ready$ kubectl get podNAME                      READY     STATUS    RESTARTS   AGEweb-0                     1/1       Running   0          4mweb-1                     1/1       Running   0          3mweb-2                     1/1       Running   0          1m

Auto-created PVC based on volumeclaimtemplates

$ kubectl get pvcNAME              STATUS    VOLUME                                  CAPACITY   ACCESS MODES   STORAGECLASS     AGEwww-web-0         Bound     pvc-ecf003f3-828d-11e8-8815-000c29774d39   2G        RWO          gluster-heketi   7mwww-web-1         Bound     pvc-0615e33e-828e-11e8-8815-000c29774d39   2G        RWO          gluster-heketi   6mwww-web-2         Bound     pvc-43a97acf-828e-11e8-8815-000c29774d39   2G        RWO          gluster-heketi   4m

Statefulset name is called Web three pod copy: web-0,web-1,web-2,volumeclaimtemplates name is: www, then the automatically created PVC name is called Www-web[0-2], Create a PVC for each pod.
Rule summary:

    • matches the pod name (network identity) in the following mode: $ (statefulset name)-$ (ordinal) , such as the example above: Web-0,web-1,web-2. The
    • statefulset creates a DNS domain name (host name) for each pod copy, which is in the format: $ (podname). Headless server name) means that the service is communicating through pod domain names rather than pod IPs.
    • Statefulset uses the Headless service to control the domain name of the pod, which is the FQDN of the domain name: $ (service name). $ (namespace). svc.cluster.local , where "cluster.local" refers to the domain name of the cluster.
    • creates a PVC,PVC naming rule matching pattern for each pod according to Volumeclaimtemplates: (volume_name)-(Pod_name) ,
      For example, the above volumemounts.name=www, Pod name=web-[0-2], so the PVC is created www-web-0, Www-web-1, Www-web-2.
    • Removing the pod does not remove its PVC, and removing the PVC automatically releases the PV.
      Some examples of DNS domain names for cluster domain, headless service name, how Statefulset name affects Statefulset pods
Cluster Domain Service (ns/name) Statefulset (ns/name) Statefulset Domain Pod DNS Pod Hostname
Cluster.local Default/nginx Default/web Nginx.default.svc.cluster.local Web-{0..n-1}.nginx.default.svc.cluster.local Web-{0..n-1}
Cluster.local Foo/nginx Foo/web Nginx.foo.svc.cluster.local Web-{0..n-1}.nginx.foo.svc.cluster.local Web-{0..n-1}
Kube.local Foo/nginx Foo/web Nginx.foo.svc.kube.local Web-{0..n-1}.nginx.foo.svc.kube.local Web-{0..n-1}

Statefulset Start-Stop order:

  • Ordered deployment: When deploying Statefulset, if there are multiple pod replicas, they are created sequentially (from 0 to N-1) and all previous pods must be running and ready state before the next pod runs.
  • Sequential deletion: When pods are deleted, they are terminated from N-1 to 0.
  • Sequential expansion: When performing an extended operation on the pod, the pod before it must be in the running and ready state, as is the case with the deployment.
     
    Statefulset Pod Management strategy:
    After v1.7, ensure that the identity is unique through the. Spec.podmanagementpolicy field by allowing you to modify the pod ordering policy.
  • Orderedready: The start and stop order above, the default setting.
  • Parallel: Tells the Statefulset controller to start or terminate all pods in parallel and not wait until the previous pod changes to running and ready or completely terminates before starting or terminating another pod.
    Statefulset Usage Scenarios
  • Stable persistent storage, that is, when the pod is re-dispatched or it can access the same persisted data, based on the PVC.
  • A stable network identifier, which is podname and hostname, after the pod is re-dispatched.
  • Orderly deployment, orderly expansion, based on the init containers to achieve.
  • Orderly contraction. Update policy

    In Kubernetes 1.7 and later, the. Spec.updatestrategy field allows you to configure or disable the pod, labels, esource request/limits, annotations Auto-rollover feature.
    OnDelete: The pod in Statefulset is not automatically updated by setting the. Spec.updateStrategy.type field to Ondelete,statefulset controller. The user must manually delete the pod so that the controller creates a new pod
    rollingupdate: with the. Spec.updateStrategy.type field set to Rollingupdate, the automatic rolling update of the pod is implemented, If the. Spec.updatestrategy is not specified, this is the default policy.
    The Statefulset controller deletes and re-creates each pod in the Statefulset. It will be done in the order of pod termination (from the largest ordinal to the smallest ordinal), updating each pod at once. Before you update the next pod, you must wait for the pod Running and ready.
    partitions: partitions The rollingupdate update policy by specifying. Spec.updateStrategy.rollingUpdate.partition, if a partition is specified, when Statefulset. spec.template update, all pods with greater than or equal to the partition ordinal will be updated.
    All pods with an ordinal smaller than the partition will not be updated, even if they are deleted. If the Statefulset. Spec.updateStrategy.rollingUpdate.partition is greater than its. Spec.replicas, updates to its. Spec.template will not propagate to the Pod. In most cases, you do not need to use partitions.

Kubernetes's Statefulset detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.