-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathk8s_evicted.html
1180 lines (560 loc) · 53.2 KB
/
k8s_evicted.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>kubernetes 中 Evicted pod 是如何产生的 · 田飞雨</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="kubernetes_descheduler.html" />
<link rel="prev" href="kubelet_garbage_collect.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 " 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 active" 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=".." >kubernetes 中 Evicted pod 是如何产生的</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="#线上被驱逐实例数据">线上被驱逐实例数据</a></li>
<li><a href="#实例被驱逐的原因">实例被驱逐的原因</a><ul>
<li><a href="#节点资源不足导致实例被驱逐">节点资源不足导致实例被驱逐</a></li>
</ul>
</li>
<li><a href="#kubelet-驱逐实例时与资源处理相关的已知问题">kubelet 驱逐实例时与资源处理相关的已知问题</a></li>
<li><a href="#驱逐实例未被删除原因分析">驱逐实例未被删除原因分析</a></li>
<li><a href="#解决方案">解决方案</a></li>
<li><a href="#总结">总结</a></li>
</ul>
<h3 id="线上被驱逐实例数据"><a name="线上被驱逐实例数据" class="anchor-navigation-ex-anchor" href="#线上被驱逐实例数据"><i class="fa fa-link" aria-hidden="true"></i></a>线上被驱逐实例数据</h3>
<p>最近在线上发现很多实例处于 Evicted 状态,通过 pod yaml 可以看到实例是因为节点资源不足被驱逐,但是这些实例并没有被自动清理,平台的大部分用户在操作时看到服务下面出现 Evicted 实例时会以为服务有问题或者平台有问题的错觉,影响了用户的体验。而这部分 Evicted 状态的 Pod 在底层关联的容器其实已经被销毁了,对用户的服务也不会产生什么影响,也就是说只有一个 Pod 空壳在 k8s 中保存着,但需要人为手动清理。本文会分析为什么为产生 Evicted 实例、为什么 Evicted 实例没有被自动清理以及如何进行自动清理。</p>
<blockquote>
<p>kubernetes 版本:v1.17</p>
</blockquote>
<pre class="language-"><code>$ kubectl get pod | grep -i Evicted
cloud-1023955-84421-49604-5-deploy-c-7748f8fd8-hjqsh 0/1 Evicted 0 73d
cloud-1023955-84421-49604-5-deploy-c-7748f8fd8-mzd8x 0/1 Evicted 0 81d
cloud-1237162-276467-199844-2-deploy-7bdc7c98b6-26r2r 0/1 Evicted 0 18d
</code></pre><p>Evicted 实例状态:</p>
<pre class="language-"><code>status:
message: 'Pod The node had condition: [DiskPressure]. '
phase: Failed
reason: Evicted
startTime: "2021-09-14T10:42:32Z"
</code></pre><h3 id="实例被驱逐的原因"><a name="实例被驱逐的原因" class="anchor-navigation-ex-anchor" href="#实例被驱逐的原因"><i class="fa fa-link" aria-hidden="true"></i></a>实例被驱逐的原因</h3>
<p>kubelet 默认会配置节点资源不足时驱逐实例的策略,当节点资源不足时 k8s 会停止该节点上实例并在其他节点启动新实例,在某些情况下也可通过配置 <code>--eviction-hard=</code> 参数为空来禁用驱逐策略,在之前的生产环境中我们也确实这么做了。</p>
<h4 id="节点资源不足导致实例被驱逐"><a name="节点资源不足导致实例被驱逐" class="anchor-navigation-ex-anchor" href="#节点资源不足导致实例被驱逐"><i class="fa fa-link" aria-hidden="true"></i></a>节点资源不足导致实例被驱逐</h4>
<p>k8s 中产生 Evicted 状态实例主要是因为节点资源不足实例主动被驱逐导致的,kubelet eviction_manager 模块会定期检查节点内存使用率、inode 使用率、磁盘使用率、pid 等资源,根据 kubelet 的配置当使用率达到一定阈值后会先回收可以回收的资源,若回收后资源使用率依然超过阈值则进行驱逐实例操作。</p>
<table>
<thead>
<tr>
<th>Eviction Signal</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>memory.available</td>
<td>memory.available := node.status.capacity[memory] - node.stats.memory.workingSet</td>
</tr>
<tr>
<td>nodefs.available</td>
<td>nodefs.available := node.stats.fs.available</td>
</tr>
<tr>
<td>nodefs.inodesFree</td>
<td>nodefs.inodesFree := node.stats.fs.inodesFree</td>
</tr>
<tr>
<td>imagefs.available</td>
<td>imagefs.available := node.stats.runtime.imagefs.available</td>
</tr>
<tr>
<td>imagefs.inodesFree</td>
<td>imagefs.inodesFree := node.stats.runtime.imagefs.inodesFree</td>
</tr>
<tr>
<td>pid.available</td>
<td>pid.available := node.stats.rlimit.maxpid - node.stats.rlimit.curproc</td>
</tr>
</tbody>
</table>
<p>kubelet 中 pod 的 stats 数据一部分是通过 cAdvisor 接口获取到的,一部分是通过 CRI runtimes 的接口获取到的。</p>
<p><strong>memory.available</strong>:当前节点可用内存,计算方式为 cgroup memory 子系统中 memory.usage_in_bytes 中的值减去 memory.stat 中 total_inactive_file 的值;
<strong>nodefs.available</strong>:nodefs 包含 kubelet 配置中 <code>--root-dir</code> 指定的文件分区和 /var/lib/kubelet/ 所在的分区磁盘使用率;
<strong>nodefs.inodesFree</strong>:nodefs.available 分区的 inode 使用率;
<strong>imagefs.available:</strong>镜像所在分区磁盘使用率;
<strong>imagefs.inodesFree:</strong>镜像所在分区磁盘inode使用率;
<strong>pid.available:</strong><code>/proc/sys/kernel/pid_max</code> 中的值为系统最大可用 pid 数;</p>
<p>kubelet 可以通过参数 <code>--eviction-hard</code> 来配置以上几个参数的阈值,该参数默认值为 <code>imagefs.available<15%,memory.available<100Mi,nodefs.available<10%,nodefs.inodesFree<5%</code>,当达到阈值时会驱逐节点上的容器。</p>
<h3 id="kubelet-驱逐实例时与资源处理相关的已知问题"><a name="kubelet-驱逐实例时与资源处理相关的已知问题" class="anchor-navigation-ex-anchor" href="#kubelet-驱逐实例时与资源处理相关的已知问题"><i class="fa fa-link" aria-hidden="true"></i></a>kubelet 驱逐实例时与资源处理相关的已知问题</h3>
<p><strong>1、kubelet 不会实时感知到节点内存数据的变化</strong></p>
<p>kubelet 定期通过 cadvisor 接口采集节点内存使用数据,当节点短时间内内存使用率突增,此时 kubelet 无法感知到也不会有 MemoryPressure 相关事件,但依然会调用 OOMKiller 停止容器。可以通过为 kubelet 配置 <code>--kernel-memcg-notification</code> 参数启用 memcg api,当触发memory 使用率阈值时 memcg 会主动进行通知;</p>
<p>memcg 主动通知的功能是 cgroup 中已有的,kubelet 会在 <code>/sys/fs/cgroup/memory/cgroup.event_control</code> 文件中写入 memory.available 的阈值,而阈值与 inactive_file 文件的大小有关系,kubelet 也会定期更新阈值,当 memcg 使用率达到配置的阈值后会主动通知 kubelet,kubelet 通过 epoll 机制来接收通知。</p>
<p><strong>2、kubelet</strong> <strong>memory.available 不会计算 active page</strong></p>
<p>kubelet 通过内存使用率驱逐实例时,内存使用率数据包含了 page cache 中 active_file 的数据,在某些场景下会因 page cache 过高导致内存使用率超过阈值会造成实例被驱逐,</p>
<p>由于在内存紧张时 inactive_file 会被内核首先回收,但在内存不足时,active_file 也会被内核进行回收,社区对此机制也有一些疑问,针对内核回收内存的情况比较复杂,社区暂时还未进行回应,详情可以参考 <a href="https://github.com/kubernetes/kubernetes/issues/43916" target="_blank">kubelet counts active page cache against memory.available (maybe it shouldn't?)</a>。</p>
<p>kubelet 计算节点可用内存的方式如下:</p>
<pre class="language-"><code>#!/bin/bash
#!/usr/bin/env bash
# This script reproduces what the kubelet does
# to calculate memory.available relative to root cgroup.
# current memory usage
memory_capacity_in_kb=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}')
memory_capacity_in_bytes=$((memory_capacity_in_kb * 1024))
memory_usage_in_bytes=$(cat /sys/fs/cgroup/memory/memory.usage_in_bytes)
memory_total_inactive_file=$(cat /sys/fs/cgroup/memory/memory.stat | grep total_inactive_file | awk '{print $2}')
memory_working_set=${memory_usage_in_bytes}
if [ "$memory_working_set" -lt "$memory_total_inactive_file" ];
then
memory_working_set=0
else
memory_working_set=$((memory_usage_in_bytes - memory_total_inactive_file))
fi
memory_available_in_bytes=$((memory_capacity_in_bytes - memory_working_set))
memory_available_in_kb=$((memory_available_in_bytes / 1024))
memory_available_in_mb=$((memory_available_in_kb / 1024))
echo "memory.capacity_in_bytes $memory_capacity_in_bytes"
echo "memory.usage_in_bytes $memory_usage_in_bytes"
echo "memory.total_inactive_file $memory_total_inactive_file"
echo "memory.working_set $memory_working_set"
echo "memory.available_in_bytes $memory_available_in_bytes"
echo "memory.available_in_kb $memory_available_in_kb"
echo "memory.available_in_mb $memory_available_in_mb"
</code></pre><h3 id="驱逐实例未被删除原因分析"><a name="驱逐实例未被删除原因分析" class="anchor-navigation-ex-anchor" href="#驱逐实例未被删除原因分析"><i class="fa fa-link" aria-hidden="true"></i></a>驱逐实例未被删除原因分析</h3>
<p>源码中对于 Statefulset 和 DaemonSet 会自动删除 Evicted 实例,但是对于 Deployment 不会自动删除。阅读了部分官方文档以及 issue,暂未找到官方对 Deployment Evicted 实例未删除原因给出解释。</p>
<p><strong>statefulset:</strong>
pkg/controller/statefulset/stateful_set_control.go</p>
<pre class="language-"><code> // Examine each replica with respect to its ordinal
for i := range replicas {
// delete and recreate failed pods
if isFailed(replicas[i]) {
ssc.recorder.Eventf(set, v1.EventTypeWarning, "RecreatingFailedPod",
"StatefulSet %s/%s is recreating failed Pod %s",
set.Namespace,
set.Name,
replicas[i].Name)
if err := ssc.podControl.DeleteStatefulPod(set, replicas[i]); err != nil {
return &status, err
}
if getPodRevision(replicas[i]) == currentRevision.Name {
status.CurrentReplicas--
}
if getPodRevision(replicas[i]) == updateRevision.Name {
status.UpdatedReplicas--
}
......
</code></pre><p><strong>daemonset:</strong>
pkg/controller/daemon/daemon_controller.go</p>
<pre class="language-"><code>func (dsc *DaemonSetsController) podsShouldBeOnNode(
......
) (nodesNeedingDaemonPods, podsToDelete []string) {
......
switch {
......
case shouldContinueRunning:
......
for _, pod := range daemonPods {
if pod.DeletionTimestamp != nil {
continue
}
if pod.Status.Phase == v1.PodFailed {
// This is a critical place where DS is often fighting with kubelet that rejects pods.
// We need to avoid hot looping and backoff.
backoffKey := failedPodsBackoffKey(ds, node.Name)