[kubernetes]10-6 StatefulSet --- 有状态应用的守护者

 

10-6 StatefulSet --- 有状态应用的守护者

Deployment不能胜任所有的场景,之前的服务管理的场景,适合所有的编排工作

1 无状态

2 pod无差别

3 没有顺序性

 

StatefulSet

1 顺序性

2 持久存储区分

 

创建headless-service.yaml 

 

apiVersion: v1
kind: Service
metadata:
  name: springboot-web-svc
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
  clusterIP: None
  selector:
    app: springboot-web

 

statefulset.yaml 

 

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: springboot-web
spec:
# 告诉statefulset用哪个headlessservice 去保证每个pod的解析
  serviceName: springboot-web-svc
  replicas: 2
  selector:
    matchLabels:
      app: springboot-web
  template:
    metadata:
      labels:
        app: springboot-web
    spec:
      containers:
      - name: springboot-web
        image: harbor.pdabc.com/kubernetes/springboot-web:v1
        ports:
        - containerPort: 8080
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /hello?name=test
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 1
          successThreshold: 1
          timeoutSeconds: 5

kubectl apply -f headless-service.yaml 

kubectl apply -f statefulset.yaml

 

springboot-web-0  前半部分的名字来自statefulset 后半部分是编号 从0开始

 

kubectl get pods -l app=springboot-web -o wide

 

进入指定容器 查看hostname是和容器名一样的

 

ping 容器名+svc名+namespace

默认的搜索范围如下

 

删掉pod 查看其重建的过程

kubectl delete pod -l app=springboot-web

启动过程依旧有顺序性

 

 

区分持久存储

删掉之前创建的statefulset.yaml

kubectl delete -f statefulset.yaml 

创建statefulset-volume.yaml 

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: springboot-web
spec:
  serviceName: springboot-web-svc
  replicas: 2
  selector:
    matchLabels:
      app: springboot-web
  template:
    metadata:
      labels:
        app: springboot-web
    spec:
      containers:
      - name: springboot-web
        image: harbor.pdabc.com/kubernetes/springboot-web:v1
        ports:
        - containerPort: 8080
        livenessProbe:
          tcpSocket:
            port: 8080
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 3
          successThreshold: 1
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /hello?name=test
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 10
          failureThreshold: 1
          successThreshold: 1
          timeoutSeconds: 5
        volumeMounts:
        - name: data
          mountPath: /mooc-data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes:
      - ReadWriteOnce
      storageClassName: glusterfs-storage-class
      resources:
        requests:
          storage: 1Gi

 

kubectl apply -f statefulset-volume.yaml 

pvc也会自动创建 根据volumeClaimTemplates自动创建

 

根据kubectl get pods -l app=springboot-web -o wide 查看pod启动在哪个node上

进入容器 验证文件是否共享 发现并不共享

验证文件是否持久化

删除pod

kubectl delete pod -l app=springboot-web

过一会pod重新起来了 发现web0和web1 换了一个node 之前web1是在gluster2上

查看文件是否可持久化

 

测试删除statefulset

kubectl delete -f statefulset-volume.yaml 

发现 pod不在了 但是pvc pv 都还在 需要手动删除 也保证了数据

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值