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 都还在 需要手动删除 也保证了数据