Create a StatefulSet in K8s

Source: Internet
Author: User
Keywords Cloud Computing StatefulSet Creation K8s Tutorials
Problems encountered in creating StatefulSet in K8s:

Pod created using Deployment is stateless, when hanging in the Volume, if the Pod is hung, Replication Controller will run a to ensure availability, but because it is stateless, when the Pod is hanging with the previous volume relationship It has been disconnected, the new Pod can not find the previous Pod. But for the user, they are insensitive to the underlying pods, but they can no longer use the previously mounted disks when the pods hang.

solution

StatefulSet, released with K8s v1.5, keeps the state of the pods.

Reference blog

Because Kubernetes1.5 release is not long before, the domestic data is quite small, except tonybai and other cattle blog some StatefulSet data, only to the Internet search. Through the Google search StatefulSet creation, eventually found an English blog, the link below. For this new framework, or to google more ah.

How to create

What needs to be prepared before creating StatefulSet, it is worth noting that the creation order is critical, the order of creation is as follows:

1, Volume2, Persistent Volume3, Persistent Volume Claim4, Service5, StatefulSet

Volume can have many types, such as nfs, glusterfs, ceph RBD we use here to create.

Create volume sudo rbd create {volume_name} - size 1024-m {ceph-monitor-ip} -k /etc/ceph/ceph.client.admin.keyring / / Disable some rdb feature, or mount will fail rbd feature disable volume101 exclusive-lock, object-map, fast-diff, deep-flatten Create a PersistentVolume

Create pv.yaml, as follows:

apiVersion: v1 kind: PersistentVolume metadata: name: {pv_name} labels: {label_key}: {label_value} spec: capacity: storage: 1Gi accessModes: - ReadWriteOnce rbd: monitors: - {ceph-monitor-ip} {volume_name} user: admin secretRef: name: ceph-secret fsType: ext4 readOnly: false persistentVolumeReclaimPolicy: Recycle

use

kubectl create -f pv.yaml

To create PV.

Create a PersistentVolumeClaim

This step is very, very crucial because if you create a PVC name that does not correspond to a name in the StatefulSet, then the Pod in the StatefulSet is definitely not created successfully. I was stuck for a day in this step, Foreign language blog articles, only to find the naming rules in PVC and StatefulSet. Next, let's take a closer look at where you need to be aware.

Create pvc.yaml as follows:

apiVersion: v1 kind: PersistentVolumeClaim metadata: name: db-mysql-0 spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi

We created a PVC here called db-mysql-0, is this name very strange, and in this yaml did not mention the name of the PV, so how PV and PVC bound up? Is through the labels label key: value key-value pairs to be matched, we create a PV specified label key-value pairs, in the PVC by selector can specify the label.

And then come back to the definition of the name of the PVC: db-mysql-0, why is it that such a seemingly regular name, you need to look at the next section to create the StatefulSet yaml, first of all, we see StatefulSet name mysql , Set the replicas to 2, the names of volumeMounts and volumeClaimTemplates must be the same, db, so the name of the first Pod created by StatefulSet should be mysql-0 and the second is mysql-1. In this case, the binding relationship between the pod and the PVC in the StatefulSet is matched by the name, that is,

PVC_name === volumeClaimTemplates_name + "-" + pod_name

So this question is a bit mean, we have to create a PVC, but the name of the PVC is actually determined by the StatefulSet. I really do not know what K8S designers think ... and the example of creating a StatefulSet in an official document is to give a yaml file directly without telling me what to create and what to create first.

Create Service and StatefulSet

In the previous step we have created a PVC named db-mysql-0, then create a service and statefulset, the name of the service can be arbitrary, but the name of the statefulset has been dead, mysql, and the statefulset volumeClaimTemplates_name must be db, volumeMounts_name must also be db. Only in this way, the pod in the statefulset can be matched to the PVC by name, otherwise it will create a failure.

statefulset.yaml

apiVersion: v1 kind: Service metadata: name: mysql-service labels: app: mysql spec: ports: - port: 80 name: my-port clusterIP: None selector: app: mysql --- apiVersion: apps / v1beta1 kind: StatefulSet metadata: name: mysql spec: serviceName: "mysql-service" replicas: 2 template: metadata: labels: app: mysql spec: terminationGracePeriodSeconds: 10 containers: - name: mysqlpod image: mysql: latest ports: - containerPort: 80 name: my-port volumeMounts: - name: db mountPath: / var / lib / mysql volumeClaimTemplates: - metadata: name: db spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 1Gi

Run the following command:

kubectl create -f statefulset.yaml

You can create a statefulset.


Related Article

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.