- 预先创建相关pv
1234567891011121314151617181920212223242526272829303132[root@k8s-master cka]# cat pv-5g.yamlkind: PersistentVolumeapiVersion: v1metadata:name: task-pv-volume-5glabels:type: localspec:storageClassName: manualcapacity:storage: 5GiaccessModes:- ReadWriteOncehostPath:path: "/root/cka/data"[root@k8s-master cka]# cat pv.yamlkind: PersistentVolumeapiVersion: v1metadata:name: task-pv-volumelabels:type: localspec:storageClassName: manualcapacity:storage: 2GiaccessModes:- ReadWriteOncehostPath:path: "/root/cka/data" - 执行以下yaml,创建headless servcie,statefulset,系统从statefulset模板中自动创建pvc
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849[root@k8s-master cka]# cat statefulset.yamlapiVersion: v1kind: Servicemetadata:name: nginx-statefullabels:app: nginx-statefulspec:ports:- port: 80name: webclusterIP: Noneselector:appstateful: nginx <<<<这里要等于statefulset中的spec.selector.matchLabels和spec.template.metadata.labels---apiVersion: apps/v1kind: StatefulSetmetadata:name: webspec:selector:matchLabels:appstateful: nginx # has to match .spec.template.metadata.labelsserviceName: "nginx-stateful"replicas: 2 # by default is 1template:metadata:labels:appstateful: nginx # has to match .spec.selector.matchLabels 应用到所有pods上spec:terminationGracePeriodSeconds: 10containers:- name: nginximage: nginxports:- containerPort: 80name: webvolumeMounts:- name: wwwmountPath: /usr/share/nginx/htmlvolumeClaimTemplates:- metadata:name: wwwspec:accessModes: [ "ReadWriteOnce" ]storageClassName: manualresources:requests:storage: 1Gi
- 产生的输出效果
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172[root@k8s-master cka]# kubectl get statefulset web -o yamlapiVersion: apps/v1kind: StatefulSetmetadata:annotations:kubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"apps/v1","kind":"StatefulSet","metadata":{"annotations":{},"name":"web","namespace":"default"},"spec":{"replicas":2,"selector":{"matchLabels":{"appstateful":"nginx"}},"serviceName":"nginx-stateful","template":{"metadata":{"labels":{"appstateful":"nginx"}},"spec":{"containers":[{"image":"nginx","name":"nginx","ports":[{"containerPort":80,"name":"web"}],"volumeMounts":[{"mountPath":"/usr/share/nginx/html","name":"www"}]}],"terminationGracePeriodSeconds":10}},"volumeClaimTemplates":[{"metadata":{"name":"www"},"spec":{"accessModes":["ReadWriteOnce"],"resources":{"requests":{"storage":"1Gi"}},"storageClassName":"manual"}}]}}creationTimestamp: 2018-08-09T10:22:45Zgeneration: 5name: webnamespace: defaultresourceVersion: "245069"selfLink: /apis/apps/v1/namespaces/default/statefulsets/webuid: 282abb71-9bbe-11e8-a2f9-000c29850765spec:podManagementPolicy: OrderedReady <<<如果是Parallel 则会同时启动删除pod而不按照强制顺序replicas: 2revisionHistoryLimit: 10selector:matchLabels:appstateful: nginxserviceName: nginx-statefultemplate:metadata:creationTimestamp: nulllabels:appstateful: nginxspec:containers:- image: nginximagePullPolicy: Alwaysname: nginxports:- containerPort: 80name: webprotocol: TCPresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /usr/share/nginx/htmlname: wwwdnsPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 10updateStrategy:rollingUpdate:partition: 0 <<<<<分区rollingupdate,pod编号大于等于这个数字的会被update,小于的不变。等于可以做到灰度发布type: RollingUpdatevolumeClaimTemplates:- metadata:creationTimestamp: nullname: wwwspec:accessModes:- ReadWriteOnceresources:requests:storage: 1GistorageClassName: manualstatus:phase: Pendingstatus:collisionCount: 0currentReplicas: 2currentRevision: web-7b4ff57f55observedGeneration: 5readyReplicas: 2replicas: 2updateRevision: web-7b4ff57f55
12345[root@k8s-master cka]# kubectl get svc -o wideNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTORkubernetes ClusterIP 10.250.0.1 <none> 443/TCP 3d <none>nginx ClusterIP 10.250.0.157 <none> 80/TCP 7h app=nginxnginx-stateful ClusterIP None <none> 80/TCP 16m appstateful=nginx
123[root@k8s-master cka]# kubectl get statefulset -o wideNAME DESIRED CURRENT AGE CONTAINERS IMAGESweb 2 2 16m nginx nginx
123456789101112[root@k8s-master cka]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE IP NODEbusybox-6cdcdb6f5f-tfh84 1/1 Running 54 3d 10.244.0.4 k8s-mastermysql 1/1 Running 0 1d 10.244.1.23 k8s-node1nginx-56f766d96f-96vx7 1/1 Running 0 7h 10.244.1.24 k8s-node1nginx-56f766d96f-ckxqj 1/1 Running 0 7h 10.244.0.9 k8s-masterpi 0/1 Completed 0 2d 10.244.1.8 k8s-node1pi-jkdfw 0/1 Completed 0 1d 10.244.1.14 k8s-node1redis 1/1 Running 0 1d 10.244.1.19 k8s-node1static-web-k8s-master 1/1 Running 0 3h 10.244.0.10 k8s-masterweb-0 1/1 Running 0 17m 10.244.1.25 k8s-node1web-1 1/1 Running 0 17m 10.244.0.11 k8s-master
12345[root@k8s-master cka]# kubectl get pvc -o wideNAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGEtask-pv-claim Terminating task-pv-volume-4g 4Gi RWO manual 1dwww-web-0 Bound task-pv-volume 2Gi RWO manual 18mwww-web-1 Bound task-pv-volume-5g 5Gi RWO manual 18m
- dns解析
12345678910111213[root@k8s-master cka]# kubectl exec -it busybox-6cdcdb6f5f-tfh84 sh/ # nslookup web-0.nginx-stateful.default.svc.cluster.localServer: 10.250.0.53Address 1: 10.250.0.53Name: web-0.nginx-stateful.default.svc.cluster.localAddress 1: 10.244.1.25/ # nslookup web-1.nginx-stateful.default.svc.cluster.localServer: 10.250.0.53Address 1: 10.250.0.53Name: web-1.nginx-stateful.default.svc.cluster.localAddress 1: 10.244.0.11
解析svc域名,直接解析出两个pod的ip
1234567/ # nslookup nginx-stateful.default.svc.cluster.localServer: 10.250.0.53Address 1: 10.250.0.53Name: nginx-stateful.default.svc.cluster.localAddress 1: 10.244.0.11Address 2: 10.244.1.25
修改image,产生rollingupdate:
12345678910111213141516171819202122232425web-0 1/1 Running 0 52mweb-1 1/1 Running 0 14m-------------开始rolling-----web-1 1/1 Terminating 0 14mweb-1 0/1 Terminating 0 14mweb-1 0/1 Terminating 0 15mweb-1 0/1 Terminating 0 15mweb-1 0/1 Pending 0 0sweb-1 0/1 Pending 0 0sweb-1 0/1 ContainerCreating 0 0sweb-1 0/1 ContainerCreating 0 1sweb-1 0/1 ContainerCreating 0 6sweb-1 1/1 Running 0 15s------以上终结web-1 pod,并重新启动一个新web-1 pod,成功后开始web0----web-0 1/1 Terminating 0 53mweb-0 0/1 Terminating 0 53mweb-0 0/1 Terminating 0 53mweb-0 0/1 Terminating 0 53mweb-0 0/1 Pending 0 0sweb-0 0/1 Pending 0 0sweb-0 0/1 ContainerCreating 0 2sweb-0 0/1 ContainerCreating 0 4sweb-0 0/1 ContainerCreating 0 10sweb-0 1/1 Running 0 34s------以上web0终结及重启过程------------
- [root@k8s-master cka]# kubectl patch statefulset web -p '{"spec":{"replicas":1}}'
减少replicas,将从pod名字中数字编号最大的开始减。且被删除pod所用的pvc并不会被删除,必须手工干预。如果不删除这些pvc,则如果replicas恢复回来,对应的pod名字还会claim对应的pvc,这样保持一致。Note that, the PersistentVolumes associated with the Pods’ PersistentVolume Claims are not deleted when the Pods, or StatefulSet are deleted. This must be done manually.
Statefulset:
pod的名字按照 {statefulset名字}-{数字从0排序},名字始终稳定,不论怎么扩展删除
自动按模板产生pvc, pvc名字 {pvc模板名}-{statefulset名}-{从0编号},pod与pvc关联关系始终稳定不变
按顺序产生pod,按反序删除pod。
rollingupdate时候安装类似删除的动作,删一个,起一个。
https://v1-10.docs.kubernetes.io/docs/concepts/workloads/controllers/statefulset/
文章评论