-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathstatefulset_controller.html
1503 lines (832 loc) · 78.2 KB
/
statefulset_controller.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="zh-hans" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>statefulset controller 源码分析 · 田飞雨</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta name="author" content="gosoon">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search-plus/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-base16-ateliersulphurpool.light.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-advanced-emoji/emoji-website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-ace/ace.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-sectionx/sectionx.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-local-video/video-js.min.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-terminal/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-versions-select/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-theme-comscore/test.css">
<link rel="stylesheet" href="../styles/website.css">
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="deployment_controller.html" />
<link rel="prev" href="daemonset_controller.html" />
<link rel="stylesheet" href="../gitbook/gitbook-plugin-chart/c3/c3.min.css">
<script src="../gitbook/gitbook-plugin-chart/c3/d3.min.js"></script>
<script src="../gitbook/gitbook-plugin-chart/c3/c3.min.js"></script>
<script src="../gitbook/gitbook-plugin-graph/d3.min.js"></script>
<script src="../gitbook/gitbook-plugin-graph/function-plot.js"></script>
<link rel="shortcut icon" href='../favicon.ico' type="image/x-icon">
<link rel="bookmark" href='../favicon.ico' type="image/x-icon">
<style>
@media only screen and (max-width: 640px) {
.book-header .hidden-mobile {
display: none;
}
}
</style>
<script>
window["gitbook-plugin-github-buttons"] = {"repo":"gosoon/source-code-reading-notes","types":["star"],"size":"large"};
</script>
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="输入并搜索" />
</div>
<nav role="navigation">
<ul class="summary">
<li>
<a href="https://blog.tianfeiyu.com" target="_blank" class="custom-link">Home</a>
</li>
<li class="divider"></li>
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
<b>1.1.</b>
Introduction
</a>
</li>
<li class="chapter " data-level="1.2" data-path="./">
<a href="./">
<b>1.2.</b>
kubernetes
</a>
<ul class="articles">
<li class="chapter " data-level="1.2.1" data-path="kube_apiserver.html">
<a href="kube_apiserver.html">
<b>1.2.1.</b>
kube-apiserver 的设计与实现
</a>
</li>
<li class="chapter " data-level="1.2.2" data-path="apiserver_bootstrap_controller.html">
<a href="apiserver_bootstrap_controller.html">
<b>1.2.2.</b>
kube-apiserver 中 apiserver service 的实现
</a>
</li>
<li class="chapter " data-level="1.2.3" data-path="nodelifecycle_controller.html">
<a href="nodelifecycle_controller.html">
<b>1.2.3.</b>
node controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.4" data-path="job_controller.html">
<a href="job_controller.html">
<b>1.2.4.</b>
job controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.5" data-path="garbagecollector_controller.html">
<a href="garbagecollector_controller.html">
<b>1.2.5.</b>
garbage collector controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.6" data-path="daemonset_controller.html">
<a href="daemonset_controller.html">
<b>1.2.6.</b>
daemonset controller 源码分析
</a>
</li>
<li class="chapter active" data-level="1.2.7" data-path="statefulset_controller.html">
<a href="statefulset_controller.html">
<b>1.2.7.</b>
statefulset controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.8" data-path="deployment_controller.html">
<a href="deployment_controller.html">
<b>1.2.8.</b>
deployment controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.9" data-path="replicaset_controller.html">
<a href="replicaset_controller.html">
<b>1.2.9.</b>
replicaset controller 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.10" data-path="kube_scheduler_process.html">
<a href="kube_scheduler_process.html">
<b>1.2.10.</b>
kube-scheduler 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.11" data-path="kube_scheduler_algorithm.html">
<a href="kube_scheduler_algorithm.html">
<b>1.2.11.</b>
kube-scheduler predicates 与 priorities 调度算法源码分析
</a>
</li>
<li class="chapter " data-level="1.2.12" data-path="kube_scheduler_preempt.html">
<a href="kube_scheduler_preempt.html">
<b>1.2.12.</b>
kube-scheduler 优先级与抢占机制源码分析
</a>
</li>
<li class="chapter " data-level="1.2.13" data-path="k8s_service_theory.html">
<a href="k8s_service_theory.html">
<b>1.2.13.</b>
kubernetes service 原理解析
</a>
</li>
<li class="chapter " data-level="1.2.14" data-path="kube_proxy_process.html">
<a href="kube_proxy_process.html">
<b>1.2.14.</b>
kube-proxy 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.15" data-path="kube_proxy_iptables.html">
<a href="kube_proxy_iptables.html">
<b>1.2.15.</b>
kube-proxy iptables 模式源码分析
</a>
</li>
<li class="chapter " data-level="1.2.16" data-path="kube_proxy_ipvs.html">
<a href="kube_proxy_ipvs.html">
<b>1.2.16.</b>
kube-proxy ipvs 模式源码分析
</a>
</li>
<li class="chapter " data-level="1.2.17" data-path="kubelet-modules.html">
<a href="kubelet-modules.html">
<b>1.2.17.</b>
kubelet 架构浅析
</a>
</li>
<li class="chapter " data-level="1.2.18" data-path="kubelet_init.html">
<a href="kubelet_init.html">
<b>1.2.18.</b>
kubelet 启动流程分析
</a>
</li>
<li class="chapter " data-level="1.2.19" data-path="kubelet_create_pod.html">
<a href="kubelet_create_pod.html">
<b>1.2.19.</b>
kubelet 创建 pod 的流程
</a>
</li>
<li class="chapter " data-level="1.2.20" data-path="node_status.html">
<a href="node_status.html">
<b>1.2.20.</b>
kubelet 状态上报的方式
</a>
</li>
<li class="chapter " data-level="1.2.21" data-path="k8s_events.html">
<a href="k8s_events.html">
<b>1.2.21.</b>
kubelet 中事件处理机制
</a>
</li>
<li class="chapter " data-level="1.2.22" data-path="kubelet_status_manager.html">
<a href="kubelet_status_manager.html">
<b>1.2.22.</b>
kubelet statusManager 源码分析
</a>
</li>
<li class="chapter " data-level="1.2.23" data-path="kubelet_qos.html">
<a href="kubelet_qos.html">
<b>1.2.23.</b>
kubernetes 中 Qos 的设计与实现
</a>
</li>
<li class="chapter " data-level="1.2.24" data-path="kubelet_garbage_collect.html">
<a href="kubelet_garbage_collect.html">
<b>1.2.24.</b>
kubelet 中垃圾回收机制的设计与实现
</a>
</li>
<li class="chapter " data-level="1.2.25" data-path="k8s_evicted.html">
<a href="k8s_evicted.html">
<b>1.2.25.</b>
kubernetes 中 Evicted pod 是如何产生的
</a>
</li>
<li class="chapter " data-level="1.2.26" data-path="kubernetes_descheduler.html">
<a href="kubernetes_descheduler.html">
<b>1.2.26.</b>
Kubernetes 中 Descheduler 组件的使用与扩展
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.3" data-path="../knative/">
<a href="../knative/">
<b>1.3.</b>
knative
</a>
<ul class="articles">
<li class="chapter " data-level="1.3.1" data-path="../knative/deploy_with_minkube.html">
<a href="../knative/deploy_with_minkube.html">
<b>1.3.1.</b>
在 minkube 上部署 knative
</a>
</li>
<li class="chapter " data-level="1.3.2" data-path="../knative/knative_serving.html">
<a href="../knative/knative_serving.html">
<b>1.3.2.</b>
knative serving 组件分析
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.4" data-path="../cgroup/">
<a href="../cgroup/">
<b>1.4.</b>
cgroup
</a>
<ul class="articles">
<li class="chapter " data-level="1.4.1" data-path="../cgroup/blkio_cgroup.html">
<a href="../cgroup/blkio_cgroup.html">
<b>1.4.1.</b>
blkio cgroup
</a>
</li>
<li class="chapter " data-level="1.4.2" data-path="../cgroup/pid_cgroup.html">
<a href="../cgroup/pid_cgroup.html">
<b>1.4.2.</b>
pid cgroup
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="1.5" data-path="../go/">
<a href="../go/">
<b>1.5.</b>
go
</a>
<ul class="articles">
<li class="chapter " data-level="1.5.1" data-path="../go/func_return_value_or_pointer.html">
<a href="../go/func_return_value_or_pointer.html">
<b>1.5.1.</b>
golang 中函数使用值返回与指针返回的区别,底层原理分析
</a>
</li>
<li class="chapter " data-level="1.5.2" data-path="../go/golang_bootstrap.html">
<a href="../go/golang_bootstrap.html">
<b>1.5.2.</b>
Golang 程序启动流程分析
</a>
</li>
<li class="chapter " data-level="1.5.3" data-path="../go/golang_gpm.html">
<a href="../go/golang_gpm.html">
<b>1.5.3.</b>
Golang GPM 模型剖析
</a>
</li>
</ul>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
本书使用 GitBook 发布
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >statefulset controller 源码分析</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<ul>
<li><a href="#statefulset-的基本功能">Statefulset 的基本功能</a><ul>
<li><a href="#创建">创建</a></li>
<li><a href="#扩容">扩容</a></li>
<li><a href="#缩容">缩容</a></li>
<li><a href="#更新">更新</a></li>
<li><a href="#回滚">回滚</a></li>
<li><a href="#删除">删除</a></li>
<li><a href="#pod-管理策略">Pod 管理策略</a></li>
</ul>
</li>
<li><a href="#statefulsetcontroller-源码分析">StatefulSetController 源码分析</a><ul>
<li><a href="#sync">sync</a></li>
<li><a href="#syncstatefulset">syncStatefulSet</a></li>
<li><a href="#updatestatefulset">updateStatefulSet</a></li>
</ul>
</li>
<li><a href="#总结">总结</a></li>
</ul>
<h3 id="statefulset-的基本功能"><a name="statefulset-的基本功能" class="anchor-navigation-ex-anchor" href="#statefulset-的基本功能"><i class="fa fa-link" aria-hidden="true"></i></a>Statefulset 的基本功能</h3>
<p>statefulset 旨在与有状态的应用及分布式系统一起使用,statefulset 中的每个 pod 拥有一个唯一的身份标识,并且所有 pod 名都是按照 {0..N-1} 的顺序进行编号。本文会主要分析 statefulset controller 的设计与实现,在分析源码前先介绍一下 statefulset 的基本使用。</p>
<h4 id="创建"><a name="创建" class="anchor-navigation-ex-anchor" href="#创建"><i class="fa fa-link" aria-hidden="true"></i></a>创建</h4>
<p>对于一个拥有 N 个副本的 statefulset,pod 是按照 {0..N-1}的序号顺序创建的,并且会等待前一个 pod 变为 <code>Running & Ready</code> 后才会启动下一个 pod。</p>
<pre class="language-"><code>$ kubectl create -f sts.yaml
$ kubectl get pod -o wide -w
NAME READY STATUS RESTARTS AGE IP NODE
web-0 0/1 ContainerCreating 0 20s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> minikube
web-0 1/1 Running 0 3m1s 10.1.0.8 minikube
web-1 0/1 Pending 0 0s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
web-1 0/1 ContainerCreating 0 2s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> minikube
web-1 1/1 Running 0 4s 10.1.0.9 minikube
</code></pre><h4 id="扩容"><a name="扩容" class="anchor-navigation-ex-anchor" href="#扩容"><i class="fa fa-link" aria-hidden="true"></i></a>扩容</h4>
<p>statefulset 扩容时 pod 也是顺序创建的,编号与前面的 pod 相接。</p>
<pre class="language-"><code>$ kubectl scale sts web --replicas=4
statefulset.apps/web scaled
$ kubectl get pod -o wide -w
......
web-2 0/1 Pending 0 0s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
web-2 0/1 ContainerCreating 0 1s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> minikube
web-2 1/1 Running 0 4s 10.1.0.10 minikube
web-3 0/1 Pending 0 0s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
web-3 0/1 ContainerCreating 0 1s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> minikube
web-3 1/1 Running 0 4s 10.1.0.11 minikube
</code></pre><h4 id="缩容"><a name="缩容" class="anchor-navigation-ex-anchor" href="#缩容"><i class="fa fa-link" aria-hidden="true"></i></a>缩容</h4>
<p>缩容时控制器会按照与 pod 序号索引相反的顺序每次删除一个 pod,在删除下一个 pod 前会等待上一个被完全删除。</p>
<pre class="language-"><code>$ kubectl scale sts web --replicas=2
$ kubectl get pod -o wide -w
......
web-3 1/1 Terminating 0 8m25s 10.1.0.11 minikube
web-3 0/1 Terminating 0 8m27s <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> minikube
web-2 1/1 Terminating 0 8m31s 10.1.0.10 minikube
web-2 0/1 Terminating 0 8m33s 10.1.0.10 minikube
</code></pre><h4 id="更新"><a name="更新" class="anchor-navigation-ex-anchor" href="#更新"><i class="fa fa-link" aria-hidden="true"></i></a>更新</h4>
<p>更新策略由 statefulset 中的 <code>spec.updateStrategy.type</code> 字段决定,可以指定为 <code>OnDelete</code> 或者 <code>RollingUpdate</code> , 默认的更新策略为 <code>RollingUpdate</code>。当使用<code>RollingUpdate</code> 更新策略更新所有 pod 时采用与序号索引相反的顺序进行更新,即最先删除序号最大的 pod 并根据更新策略中的 <code>partition</code> 参数来进行分段更新,控制器会更新所有序号大于或等于 <code>partition</code> 的 pod,等该区间内的 pod 更新完成后需要再次设定 <code>partition</code> 的值以此来更新剩余的 pod,最终 <code>partition</code> 被设置为 0 时代表更新完成了所有的 pod。在更新过程中,如果一个序号小于 <code>partition</code> 的 pod 被删除或者终止,controller 依然会使用更新前的配置重新创建。</p>
<pre class="language-"><code>// 使用 RollingUpdate 策略更新
$ kubectl patch statefulset web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"nginx:1.16"}]'
statefulset.apps/web patched
$ kubectl rollout status sts/web
Waiting for 1 pods to be ready...
Waiting for partitioned roll out to finish: 1 out of 2 new pods have been updated...
Waiting for 1 pods to be ready...
partitioned roll out complete: 2 new pods have been updated...
</code></pre><p>如果 statefulset 的 <code>.spec.updateStrategy.type</code> 字段被设置为 <code>OnDelete</code>,在更新 statefulset 时,statefulset controller 将不会自动更新其 pod。你必须手动删除 pod,此时 statefulset controller 在重新创建 pod 时,使用修改过的 <code>.spec.template</code> 的内容创建新 pod。</p>
<pre class="language-"><code>// 使用 OnDelete 方式更新
$ kubectl patch statefulset nginx --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"nginx:1.9"}]'
// 删除 web-1
$ kubectl delete pod web-1
// 查看 web-0 与 web-1 的镜像版本,此时发现 web-1 已经变为最新版本 nginx:1.9 了
$ kubectl get pod -l app=nginx -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\n"}{end}'
web-0 nginx:1.16
web-1 nginx:1.9
</code></pre><p>使用滚动更新策略时你必须以某种策略不段更新 <code>partition</code> 值来进行升级,类似于金丝雀部署方式,升级对于 pod 名称来说是逆序。使用非滚动更新方式式,需要手动删除对应的 pod,升级可以是无序的。</p>
<h4 id="回滚"><a name="回滚" class="anchor-navigation-ex-anchor" href="#回滚"><i class="fa fa-link" aria-hidden="true"></i></a>回滚</h4>
<p>statefulset 和 deployment 一样也支持回滚操作,statefulset 也保存了历史版本,和 deployment 一样利用<code>.spec.revisionHistoryLimit</code> 字段设置保存多少个历史版本,但 statefulset 的回滚并不是自动进行的,回滚操作也仅仅是进行了一次发布更新,和发布更新的策略一样,更新 statefulset 后需要按照对应的策略手动删除 pod 或者修改 <code>partition</code> 字段以达到回滚 pod 的目的。</p>
<pre class="language-"><code>// 查看 sts 的历史版本
$ kubectl rollout history statefulset web
statefulset.apps/web
REVISION
0
0
5
6
$ kubectl get controllerrevision
NAME CONTROLLER REVISION AGE
web-6c4c79564f statefulset.apps/web 6 11m
web-c47b9997f statefulset.apps/web 5 4h13m
// 回滚至最近的一个版本
$ kubectl rollout undo statefulset web --to-revision=5
</code></pre><p>因为 statefulset 的使用对象是有状态服务,大部分有状态副本集都会用到持久存储,statefulset 下的每个 pod 正常情况下都会关联一个 pv 对象,对 statefulset 对象回滚非常容易,但其使用的 pv 中保存的数据无法回滚,所以在生产环境中进行回滚时需要谨慎操作,statefulset、pod、pvc 和 pv 关系图如下所示:</p>
<p><img src="http://cdn.tianfeiyu.com/format-sts.png" alt=""></p>
<h4 id="删除"><a name="删除" class="anchor-navigation-ex-anchor" href="#删除"><i class="fa fa-link" aria-hidden="true"></i></a>删除</h4>
<p>statefulset 同时支持级联和非级联删除。使用非级联方式删除 statefulset 时,statefulset 的 pod 不会被删除。使用级联删除时,statefulset 和它关联的 pod 都会被删除。对于级联与非级联删除,在删除时需要指定删除选项(<code>orphan</code>、<code>background</code> 或者 <code>foreground</code>)进行区分。</p>
<pre class="language-"><code>// 1、非级联删除
$ kubectl delete statefulset web --cascade=false
// 删除 sts 后 pod 依然处于运行中
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 4m38s
web-1 1/1 Running 0 17m
// 重新创建 sts 后,会再次关联所有的 pod
$ kubectl create -f sts.yaml
$ kubectl get sts
NAME READY AGE
web 2/2 28s
</code></pre><p>在级联删除 statefulset 时,会将所有的 pod 同时删掉,statefulset 控制器会首先进行一个类似缩容的操作,pod 按照和他们序号索引相反的顺序每次终止一个。在终止一个 pod 前,statefulset 控制器会等待 pod 后继者被完全终止。</p>
<pre class="language-"><code>// 2、级联删除
$ kubectl delete statefulset web
$ kubectl get pod -o wide -w
......
web-0 1/1 Terminating 0 17m 10.1.0.18 minikube <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
web-1 1/1 Terminating 0 36m 10.1.0.15 minikube <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
web-1 0/1 Terminating 0 36m 10.1.0.15 minikube <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
web-0 0/1 Terminating 0 17m 10.1.0.18 minikube <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span>none</span><span class="token punctuation">></span></span>
</code></pre><h4 id="pod-管理策略"><a name="pod-管理策略" class="anchor-navigation-ex-anchor" href="#pod-管理策略"><i class="fa fa-link" aria-hidden="true"></i></a>Pod 管理策略</h4>
<p>statefulset 的默认管理策略是 <code>OrderedReady</code>,该策略遵循上文展示的顺序性保证。statefulset 还有另外一种管理策略 <code>Parallel</code>,<code>Parallel</code> 管理策略告诉 statefulset 控制器并行的终止所有 pod,在启动或终止另一个 pod 前,不必等待这些 pod 变成 <code>Running & Ready</code> 或者完全终止状态,但是 <code>Parallel</code> 仅仅支持在 <code>OnDelete</code> 策略下生效,下文会在源码中具体分析。</p>
<h3 id="statefulsetcontroller-源码分析"><a name="statefulsetcontroller-源码分析" class="anchor-navigation-ex-anchor" href="#statefulsetcontroller-源码分析"><i class="fa fa-link" aria-hidden="true"></i></a>StatefulSetController 源码分析</h3>
<blockquote>
<p>kubernetes 版本:v1.16</p>
</blockquote>
<p><code>startStatefulSetController</code> 是 statefulSetController 的启动方法,其中调用 <code>NewStatefulSetController</code> 进行初始化 controller 对象然后调用 <code>Run</code> 方法启动 controller。其中 <code>ConcurrentStatefulSetSyncs</code> 默认值为 5。</p>
<p><code>k8s.io/kubernetes/cmd/kube-controller-manager/app/apps.go:55</code></p>
<pre class="language-"><code>func startStatefulSetController(ctx ControllerContext) (http.Handler, bool, error) {
if !ctx.AvailableResources[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}] {
return nil, false, nil
}
go statefulset.NewStatefulSetController(
ctx.InformerFactory.Core().V1().Pods(),
ctx.InformerFactory.Apps().V1().StatefulSets(),
ctx.InformerFactory.Core().V1().PersistentVolumeClaims(),
ctx.InformerFactory.Apps().V1().ControllerRevisions(),
ctx.ClientBuilder.ClientOrDie("statefulset-controller"),
).Run(int(ctx.ComponentConfig.StatefulSetController.ConcurrentStatefulSetSyncs), ctx.Stop)
return nil, true, nil
}
</code></pre><p>当 controller 启动后会通过 informer 同步 cache 并监听 pod 和 statefulset 对象的变更事件,informer 的处理流程此处不再详细讲解,最后会执行 <code>sync</code> 方法,<code>sync</code> 方法是每个 controller 的核心方法,下面直接看 statefulset controller 的 <code>sync</code> 方法。</p>
<h4 id="sync"><a name="sync" class="anchor-navigation-ex-anchor" href="#sync"><i class="fa fa-link" aria-hidden="true"></i></a>sync</h4>
<p>sync 方法的主要逻辑为:</p>
<ul>