标签:start tcp imp data rip root 运行 clust dep
StatefulSet是为了解决有状态服务的问题(对应Deployments和ReplicaSets是为无状态服务而设计),其应用场景包括
从上面的应用场景可以发现,StatefulSet由以下几个部分组成:
StatefulSet中每个Pod的DNS格式为statefulSetName-{0..N-1}.serviceName.namespace.svc.cluster.local
,其中
serviceName
为Headless Service的名字0..N-1
为Pod所在的序号,从0开始到N-1statefulSetName
为StatefulSet的名字namespace
为服务所在的namespace,Headless Servic和StatefulSet必须在相同的namespace.cluster.local
为Cluster Domain,
以一个简单的 nginx 服务 web.yaml 为例:
[root@kube pv-pvc]# cat pv_statefulset.yaml
//PV 需要事先创建
//persistentVolumeReclaimPolicy: Recycle storageClassName: slow 这两个字段不定义,否则 volumeClaimTemplates: 无法动态生成pvc
apiVersion: v1 kind: PersistentVolume metadata: name: pv0001 spec: capacity: storage: 5Gi volumeMode: Filesystem accessModes: - ReadWriteOnce nfs: path: /nfs/data/v1 server: 10.2.61.21 --- apiVersion: v1 kind: PersistentVolume metadata: name: pv0002 spec: capacity: storage: 5Gi volumeMode: Filesystem accessModes: - ReadWriteOnce nfs: path: /nfs/data/v2 server: 10.2.61.21 [root@kube pv-pvc]#
apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: k8s.gcr.io/nginx-slim:0.8 ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumeClaimTemplates: - metadata: name: www spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 2Gi
[root@kube pv-pvc]# kubectl get pv //查看 pv ,pvc ,sts NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv0001 5Gi RWO Retain Bound default/www-web-0 5m3s pv0002 5Gi RWO Retain Bound default/www-web-1 5m3s [root@kube pv-pvc]# kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE www-web-0 Bound pv0001 5Gi RWO 4m41s www-web-1 Bound pv0002 5Gi RWO 4m19s [root@kube pv-pvc]# kubectl get pods //查看 pod 都是 有序的 NAME READY STATUS RESTARTS AGE web-0 1/1 Running 0 4m46s web-1 1/1 Running 0 4m24s [root@kube pv-pvc]# kubectl get sts NAME READY AGE web 2/2 4m53s [root@kube pv-pvc]#
[root@kube pv-pvc]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10m nginx ClusterIP None <none> 80/TCP 6m9s // Headless service [root@kube pv-pvc]#
还可以进行其他的操作:
//扩容
[root@kube pv-pvc]# kubectl scale statefulset web --replicas=5 statefulset.apps/web scaled [root@kube pv-pvc]# kubectl get pods NAME READY STATUS RESTARTS AGE web-0 1/1 Running 0 16m web-1 1/1 Running 0 15m web-2 1/1 Running 0 7m19s web-3 1/1 Running 0 2m5s web-4 1/1 Running 0 103s //缩减
[root@kube pv-pvc]# kubectl patch statefulset web -p ‘{"spec":{"replicas":3}}‘ statefulset.apps/web patched [root@kube pv-pvc]# kubectl get pods
[root@kube pv-pvc]# kubectl get pvc //statefulset 缩减后 pvc 仍然保留 NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE www-web-0 Bound pv0001 5Gi RWO 19m www-web-1 Bound pv0002 5Gi RWO 19m www-web-2 Bound pv0003 5Gi RWO 11m www-web-3 Bound pv0005 5Gi RWO 5m52s www-web-4 Bound pv0004 5Gi RWO 5m30s
v1.7 + 支持 StatefulSet 的自动更新,通过 spec.updateStrategy
设置更新策略。目前支持两种策略
.spec.template
更新时,并不立即删除旧的 Pod,而是等待用户手动删除这些旧 Pod 后自动创建新 Pod。这是默认的更新策略,兼容 v1.6 版本的行为.spec.template
更新时,自动删除旧的 Pod 并创建新 Pod 替换。在更新时,这些 Pod 是按逆序的方式进行,依次删除、创建并等待 Pod 变成 Ready 状态才进行下一个 Pod 的更新。RollingUpdate 还支持 Partitions,通过 .spec.updateStrategy.rollingUpdate.partition
来设置。当 partition 设置后,只有序号大于或等于 partition 的 Pod 会在 .spec.template
更新的时候滚动更新,而其余的 Pod 则保持不变(即便是删除后也是用以前的版本重新创建)。
v1.7 + 可以通过 .spec.podManagementPolicy
设置 Pod 管理策略,支持两种方式
标签:start tcp imp data rip root 运行 clust dep
原文地址:https://www.cnblogs.com/zy09/p/11447275.html