Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of K8S statefulset

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Overview

StatefulSet

RC, Deployment, and DaemonSet are all stateless services. The IP, name, start and stop order of the Pod they manage are all random. What is StatefulSet? As the name implies, stateful collections manage all stateful services, such as MySQL, MongoDB clusters, and so on.

StatefulSet is essentially a variant of Deployment, which has become the GA version in v1.9. In order to solve the problem of stateful service, the Pod it manages has a fixed Pod name, start and stop order. In StatefulSet, the Pod name is Network identity (hostname), and shared storage must be used.

In Deployment, the corresponding service is service, while the corresponding headless service,headless service in StatefulSet, that is, headless service, is different from service in that it does not have Cluster IP. Parsing its name will return the Endpoint list of all Pod corresponding to that Headless Service.

In addition, StatefulSet creates a DNS domain name for each copy of Pod controlled by StatefulSet on top of Headless Service, which is in the format

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

StorageClass

Storageclass is an extension based on pv and pvc, which can help applications create pv and pvc dynamically, but currently does not support automatic expansion.

Take the deployment of a nginx as an example, and the rbd disk of Ceph is used as the backend column.

one。 Deploy a statefulset application

Rbac configuration file, which is directly bound to admin to prevent permission problems.

Kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata: name: rbd-provision namespace: xuyong-test-storagesubjects:- kind: ServiceAccount name: rbd-provisioner namespace: xuyong-test-storageroleRef: kind: ClusterRole name: cluster-admin apiGroup: rbac.authorization.k8s.io---kind: ServiceAccountapiVersion: v1metadata: name: rbd-provisioner namespace: xuyong-test-storage

Storage-class profile

ApiVersion: v1kind: Secretmetadata: name: ceph-secret namespace: xuyong-test-storagetype: kubernetes.io/rbd#type: kubernetes.io/rbddata: key:. # enter the key above, and the key of the real ceph after base64 encoding Otherwise, an error will be reported-apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: rbd namespace: xuyong-test-storageparameters:## the user who filled in ceph here adminId: kubernetes-online adminSecretName: ceph-secret adminSecretNamespace: xuyong-test-storage imageFormat: "2" imagefeatures: layering monitors: saas-ceph.internal.weimob.com:6789 pool: kubernetes-online userId: kubernetes-online userSecretName: ceph-secret userSecretNamespace: xuyong-test-storage# type default provisioner: kubernetes.io/rbdreclaimPolicy: Delete

Statefulset profile

ApiVersion: v1kind: Servicemetadata: name: nginx namespace: xuyong-test-storage labels: app: ports:-port: 80 name: web clusterIP: None selector: app: nginx---apiVersion: apps/v1kind: StatefulSetmetadata: name: web namespace: xuyong-test-storagespec: selector: matchLabels: app: nginx # has to match .spec.template.metadata.labels serviceName: "nginx" # declare which Headless Service it belongs to. Replicas: 3 # by default is 1 template: metadata: labels: app: nginx # has to match .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers:-name: nginx image: ccr.ccs.tencentyun.com/weimob-public/nginx:1.16-centos7 ports:-containerPort: 80 name: web volumeMounts:-name: www mountPath: / usr/share/nginx/html volumeClaimTemplates: # can be seen as a template for pvc-metadata: name: www spec: accessModes: ["ReadWriteOnce"] storageClassName: "rbd" # Storage class name Change to resources: requests: storage: 50Gi that already exists in the cluster

Note: rbd mode accessModes only supports ReadWriteOnce and ReadonlyMany, and the above service must specify clusterip:none, otherwise there will be a problem.

View the results:

Pit drainage:

The key of 1.ecret must be base64 encoded

two。 If there is a rbac problem, execute the rbac file above

3. If an error such as cpeh is not in $PATH occurs, this is because an image cannot be pulled and can be deployed locally, using a locally deployed image, or * * pull an image.

The version of 4.ceph-common should be the same as the version you use on the server side.

two。 Expand disk capacity

Reference document: https://www.520mwx.com/view/21456

Since statefulset does not support disk expansion by default, you need to use the API of ceph to expand disk capacity. The steps are as follows:

1. Check the rbd image corresponding to pv

[root@sh-saas-k8s1-master-pl-01 ceph-statefulset] # kubectl get pv pvc-ccb2ce5f-97e6-11e9-ac4b-52540006de2d-n=xuyong-test-storage-o yaml | grep image

Image: kubernetes-dynamic-pvc-ccb7c9b1-97e6-11e9-be31-0a580afb018c

two。 To connect ceph directly, you need to change the size of rbd block.

Rbd resize-image kubernetes-dynamic-pvc-ccb7c9b1-97e6-11e9-be31-0a580afb018c-size 100000

3. Find the node node where the pod is located, and then reprobe the disk

Blockdev-getsize64 / dev/rbd0

Resize2fs / dev/rbd0

4. Then log in to pod to see that the corresponding disk size has been changed.

Note: this method can change the disk size, but the data of pv, pvc and storage-class are fake, and when there are multiple instances, it can only be changed one by one, which is troublesome.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report