您的位置:首页 > 汽车 > 新车 > Kubernetes 之 StatefulSet基本原理

Kubernetes 之 StatefulSet基本原理

2024/12/26 10:33:06 来源:https://blog.csdn.net/baidu_34688878/article/details/139313808  浏览:    关键词:Kubernetes 之 StatefulSet基本原理

Kubernetes 之 StatefulSet

StatefulSet 定义

StatefulSet 是一个有状态的集合,不同于一般 Pod 的一致性,不同的 Stateful Set 创建的 Pod 之间存在数据上的差异。同时它们是有序的,这样它们在故障或删除后恢复,自身的名字也不会变化,以达到维护整个分布式系统的目的。例如在 Redis 主从集群中,我们不能用 Pod 模式来部署,以为主从容器之间的配置信息是不一样的,这时候我们就需要 Stateful Set 来实现我们的需求。

StatefulSet 的使用

apiVersion: v1
kind: Service
metadata:name: nginxlabels:app: nginx
spec:ports:- port: 80name: webclusterIP: Noneselector:app: pod-nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: stateful-set-web
spec:serviceName: "nginx"replicas: 2selector:matchLabels:app: pod-nginxtemplate:metadata:labels:app: pod-nginxspec:containers:- name: nginximage: registry.k8s.io/nginx-slim:0.8ports:- containerPort: 80name: webvolumeMounts:- name: wwwmountPath: /usr/share/nginx/htmlvolumeClaimTemplates:- metadata:name: wwwspec:storageClassName: nfs-clientaccessModes: [ "ReadWriteOnce" ]resources:requests:storage: 100Mi
---
kind: Pod
apiVersion: v1
metadata:name: stateful-set-test
spec:containers:- name: stateful-set-testimage: docker.io/k8s-test:v1.0restartPolicy: "Never"
root@k8s-master1:~# kubectl get pods -owide
NAME                                     READY   STATUS    RESTARTS      AGE   IP               NODE          NOMINATED NODE   READINESS GATES
nfs-client-provisioner-d5bf6698d-mxjw2   1/1     Running   1 (61m ago)   23h   10.244.194.109   k8s-worker1   <none>           <none>
stateful-set-test                        1/1     Running   0             62s   10.244.126.56    k8s-worker2   <none>           <none>
stateful-set-web-0                       1/1     Running   0             62s   10.244.194.114   k8s-worker1   <none>           <none>
stateful-set-web-1                       1/1     Running   0             61s   10.244.126.57    k8s-worker2   <none>           <none>root@k8s-master1:~# kubectl get pvc
NAME                     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
www-stateful-set-web-0   Bound    pvc-81e64b41-f60d-488f-8862-90c91f821641   100Mi      RWO            nfs-client     <unset>                 5m50s
www-stateful-set-web-1   Bound    pvc-2d269bcd-297f-4bd9-9e5d-f24c41fc5c6b   100Mi      RWO            nfs-client     <unset>                 5m40sroot@k8s-master1:~# kubectl exec -it stateful-set-test -- /bin/sh
/ # curl nginx
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Nginx Test</title>
</head>
<body>
Hello Web1
</body>
</html>
/ # curl nginx
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Nginx Test</title>
</head>
<body>
Hello Web2
</body>
</html>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com