Kubernetes进阶实战读书笔记:StatefulSet控制器(特性验证)

一、Pod资源标识符

由statefulset控制器创建的pod资源拥有固定、唯一的表示和专用存储卷、即便重新调度或终止后重建、其名称依然保持不变、且此前的存储卷及其数据不会丢失

[root@master ~]# kubectl get pods -l app=myapp-pod
NAME READY STATUS RESTARTS AGE
myapp-0 1/1 Running 0 40m
myapp-1 1/1 Running 0 25m

Pod资源的主机名同其资源名称、因此也是带索引序号的名称格式、如下面的命令结果所示:

[root@master ~]# for i in 0 1; do kubectl exec myapp-$i -- sh -c 'hostname'; done
myapp-0
myapp-1

Pod资源创建后DNS资源记录格式解析

$(pod_name).$(service_name).$(namespace).svc.cluster.local

验证过程

[root@master ~]# kubectl run -it --image busybox dns-client --restart=Never --rm /bin/sh
If you don't see a command prompt, try pressing enter.
/ # nslookup myapp-0.myapp.default.svc.cluster.local
Server:	10.96.0.10
Address:	10.96.0.10:53

Name:	myapp-0.myapp.default.svc.cluster.local
Address: 10.244.2.83

*** Can't find myapp-0.myapp.default.svc.cluster.local: No answer

/ # nslookup myapp-1.myapp.default.svc.cluster.local
Server:	10.96.0.10
Address:	10.96.0.10:53

Name:	myapp-1.myapp.default.svc.cluster.local
Address: 10.244.1.58

*** Can't find myapp-1.myapp.default.svc.cluster.local: No answer


/ # nslookup myapp
Server:	10.96.0.10
Address:	10.96.0.10:53

Name:	myapp.default.svc.cluster.local
Address: 10.244.1.58
Name:	myapp.default.svc.cluster.local
Address: 10.244.2.83

*** Can't find myapp.svc.cluster.local: No answer
*** Can't find myapp.cluster.local: No answer
*** Can't find myapp.localdomain: No answer
*** Can't find myapp.default.svc.cluster.local: No answer
*** Can't find myapp.svc.cluster.local: No answer
*** Can't find myapp.cluster.local: No answer
*** Can't find myapp.localdomain: No answer

statefulset控制器管控的pod资源终止后会由控制器自动进行重建

Headless Service资源记住于SRV记录来引用真正提供服务的后端POD资源的主机名称、进行指向包含POD IP地址的记录条目

虽然其IP地址存在变化的可能性、但它的名称标识在重建会保持不变、例如、在另一个终端删除POD资源myapp-1

[root@master ~]# kubectl delete pods myapp-1
pod "myapp-1" deleted

删除完成后控制器将随之开始重建pod资源

[root@master ~]# kubectl get pods -l app=myapp-pod
NAME READY STATUS RESTARTS AGE
myapp-0 1/1 Running 0 68m
myapp-1 0/1 ContainerCreating 0 1s

由此上面的命令结果可知,其名称标识符的确未发生改变

POD资源DNS表示亦未发生改变、但其IP地址会指向重建后的pod资源地址

/ # nslookup myapp-1.myapp.default.svc.cluster.local
Server:	10.96.0.10
Address:	10.96.0.10:53

Name:	myapp-1.myapp.default.svc.cluster.local
Address: 10.244.1.60

*** Can't find myapp-1.myapp.default.svc.cluster.local: No answer

二、Pod资源的专有存储卷

[root@master ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
myappdata-myapp-0 Bound pv002 5Gi RWO 79m
myappdata-myapp-1 Bound pv001 5Gi RWO,RWX 57m

为pod资源生成测试页面、用于存储卷持久性测试

[root@master ~]# for i in 0 1 2; do kubectl exec myapp-$i -- sh -c 'echo $(date),Hostname:$(hostname) > /usr/share/nginx/html/index.html';done
sh: can't create /usr/share/nginx/html/index.html: Read-only file system
command terminated with exit code 1
sh: can't create /usr/share/nginx/html/index.html: Read-only file system
command terminated with exit code 1

启动客户端向pod对象进行访问测试

[root@master ~]# kubectl run -it --image cirros client --restart=Never --rm /bin/sh
If you don't see a command prompt, try pressing enter.
/ # curl myapp-0.myapp
Thu Sep 3 09:32:10 UTC 2020,Hostname:myapp-0
/ # curl myapp-1.myapp
Thu Sep 3 09:23:35 UTC 2020,Hostname:myapp-1

删除statefulset控制器的pod资源、存储卷并不会删除

[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 1/1 Running 0 2m4s 10.244.2.89 node2 <none> <none>
myapp-0 1/1 Running 0 5m 10.244.2.88 node2 <none> <none>
myapp-1 1/1 Running 0 4m58s 10.244.1.62 node1 <none> <none>
myapp-2 1/1 Running 0 4m54s 10.244.0.25 master <none> <none>

[root@master ~]# kubectl delete pods myapp-0
pod "myapp-0" deleted

另一个终端监控到的删除及重建过程如下所示:

[root@master ~]# kubectl get pods -l app=myapp-pod -w
NAME READY STATUS RESTARTS AGE
myapp-0 1/1 Running 0 5m47s
myapp-1 1/1 Running 0 5m45s
myapp-2 1/1 Running 0 5m41s
myapp-0 1/1 Terminating 0 6m7s
myapp-0 0/1 Terminating 0 6m8s
myapp-0 0/1 Terminating 0 6m9s
myapp-0 0/1 Terminating 0 6m9s
myapp-0 0/1 Pending 0 0s
myapp-0 0/1 Pending 0 0s
myapp-0 0/1 ContainerCreating 0 0s
myapp-0 1/1 Running 0 2s

再次测试pod资源接口、存储卷是复用此前的那个

/ # curl myapp-0.myapp
Thu Sep 3 09:32:10 UTC 2020,Hostname:myapp-0

由此表明、重建的pod资源被重新调度至那个节点、此前的PVC资源就会被分配至那个节点、这样就真正实现了数据的持久化

三、statefulset资源扩缩容

1、将myapp中的pod副本数量扩展至5个

[root@master ~]# kubectl scale statefulset myapp --replicas=5
statefulset.apps/myapp scaled

2、statefulset资源扩展过程顺次进行、序号依次向后进行

[root@master ~]# kubectl get pods -l app=myapp-pod -w
NAME READY STATUS RESTARTS AGE
myapp-0 1/1 Running 0 105s
myapp-1 1/1 Running 0 7m52s
myapp-2 1/1 Running 0 7m48s
myapp-3 0/1 Pending 0 0s
myapp-3 0/1 Pending 0 0s
myapp-3 0/1 Pending 0 2s
myapp-3 0/1 ContainerCreating 0 2s
myapp-3 1/1 Running 0 4s
myapp-4 0/1 Pending 0 0s
myapp-4 0/1 Pending 0 0s
myapp-4 0/1 Pending 0 2s
myapp-4 0/1 ContainerCreating 0 2s
myapp-4 1/1 Running 0 4s

3、缩容只需要将其副本数量调低即可

[root@master ~]# kubectl patch statefulset myapp -p '{"spec":{"replicas":3}}'
statefulset.apps/myapp patched

4、缩减规模时终止pod资源倒序逐一进行

[root@master ~]# kubectl get pods -l app=myapp-pod -w
NAME READY STATUS RESTARTS AGE
myapp-0 1/1 Running 0 2m57s
myapp-1 1/1 Running 0 9m4s
myapp-2 1/1 Running 0 9m
myapp-3 1/1 Running 0 42s
myapp-4 1/1 Running 0 38s
myapp-4 1/1 Terminating 0 5m27s
myapp-4 0/1 Terminating 0 5m28s
myapp-4 0/1 Terminating 0 5m29s
myapp-4 0/1 Terminating 0 5m30s
myapp-3 1/1 Terminating 0 5m34s
myapp-3 0/1 Terminating 0 5m35s
myapp-3 0/1 Terminating 0 5m36s
myapp-3 0/1 Terminating 0 5m36s

 

直到余下的数量满足为止、另外终止pod资源后、其存储卷并不会被删除、因此缩减规模后若再将其扩展回来、那么此前的数据依然可用、且pod资源名称保持不变

 

posted @ 2020-09-05 23:17  活的潇洒80  阅读(66)  评论(0编辑  收藏