10-kubernetes statefulset控制器
有状态副本集控制器,需要关注个体,无状态关注群体,坏一个替换即可
管理具有一下特性的程序
稳定且唯一的网络标识符
稳定且持久的存储
有序,平滑地部署和扩展
有序,平滑地删除和终止
有序的滚动更新
典型Statefulset有三组件组成
Headless Hervice(无头服务)
Statefulset (控制器)
volumeClaimTemplate (存储卷申请模板)
POD标识符需要稳定持久有效且唯一名称,每个节点需要有专有的存储
部署例子:
前期准备好存储,配置好,PV 与PVC
1.定义 PV
kubectl explain pv.spec.nfs #帮助信息。
NFS 配置PV例子
apiVersion: v1 kind: PersistentVolume metadata: name: pv001 labels: name: pv001 spec: nfs: path: /data/volumes/v1 server: stor01.leiyan.com accessModes: ["ReadWriteMany","ReadWriteOnce"] capacity: storage: 5Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv002 labels: name: pv002 spec: nfs: path: /data/volumes/v2 server: stor01.leiyan.com accessModes: ["ReadWriteOnce"] capacity: storage: 5Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv003 labels: name: pv003 spec: nfs: path: /data/volumes/v3 server: stor01.leiyan.com accessModes: ["ReadWriteMany","ReadWriteOnce"] capacity: storage: 5Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv004 labels: name: pv004 spec: nfs: path: /data/volumes/v4 server: stor01.leiyan.com accessModes: ["ReadWriteMany","ReadWriteOnce"] capacity: storage: 10Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv005 labels: name: pv005 spec: nfs: path: /data/volumes/v5 server: stor01.leiyan.com accessModes: ["ReadWriteMany","ReadWriteOnce"] capacity: storage: 10Gi
配置StatefulSet例子
apiVersion: v1 kind: Service metadata: name: myapp-svc labels: app: myapp-svc spec: ports: - port: 80 name: web clusterIP: None selector: app: myapp-pod --- apiVersion: apps/v1 kind: StatefulSet metadata: name: myapp spec: serviceName: myapp-svc replicas: 3 selector: matchLabels: app: myapp-pod template: metadata: labels: app: myapp-pod spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - containerPort: 80 name: web volumeMounts: - name: myappdata mountPath: /usr/share/ngin/html volumeClaimTemplates: - metadata: name: myappdata spec: accessModes: ["ReadWriteOnce"] storageClassName: "gluster-dynamic" resources: requests: storage: 5Gi
验证
kubectl get svc kubectl get sts kubectl get pvc #解析格式 POD_NAME.SERVICE_NAME.NS_NAME.svc.cluster.local myapp-0.myapp.default.svc.cluster.local
扩缩容
kubectl scale sts myapp --relicas=5 kubectl patch sts myapp -p '{"spec":{"replicas":5}}'
升级
kubectl explain sts.spec.updateStrategy.rollingUpdate #分区更新方法 partition:N >=4 kubectl patch sts myapp -p '{"spec":{"updateStrategy":{"rollingUpdate":{"partition":4}}}}' #开始更新第0个开始的容器 kubectl patch sts myapp -p '{"spec":{"template":{"spec":{"containers[0]":{"image":"ikubernetes/myapp:v2"}}}}}' #或者 kubectl set image sts/myapp myapp=ikubernetes/myapp:v2 #验证 kubectl get sts -o wide kubectl get pods -o wide kubectl get pods myapp-3 -o yaml
停留在世界边缘,与之惜别