– Tips –
--image-gc-high-threshold
和--image-gc-low-threshold
。当用于存储镜像的磁盘使用率达到百分之–image-gc-high-threshold时将触发镜像回收,删除最近最久未使用(LRU,Least Recently Used)的镜像直到磁盘使用率降为百分之--image-gc-low-threshold
或无镜像可删为止。默认--image-gc-high-threshold
为90,`--image-gc-low-threshold
为80。--minimum-container-ttl-duration
,--maximum-dead-containers-per-container
和-
-maximum-dead-containers
。从容器停止运行时起经过--minimum-container-ttl-duration
时间后,该容器标记为已过期将来可以被回收(只是标记,不是回收),默认值为1m0s。一般情况下每个pod最多可以保留-
-maximum-dead-containers-per-container
个已停止运行的容器集,默认值为2。整个node节点可以保留--maximum-dead-containers
个已停止运行的容器,默认值为100。--minimum-container-ttl-duration
设为0(表示无限制),--maximum-dead-containers-per-container
和--maximum-dead-containers
设为负数。--minimum-container-ttl-duration
的值可以使用单位后缀,如h表示小时,m表示分钟,s表示秒。--maximum-dead-containers-per-container
和--maximum-dead-containers
冲突时,`--maximum-dead-containers
优先考虑。--maximum-dead-containers-per-container
;2)经过上一步后如果不满足--maximum-dead-containers
,计算值X=(--maximum-dead-containers)/(pod总数)
,再遍历所有pod,使其满足已停止运行的容器集个数不大于X且至少为1;3)经过以上两步后如果还不满足--maximum-dead-containers
,则对所有已停止的容器排序,优先删除创建时间最早的容器直到满足--maximum-dead-containers
为止。– Experiments-
▍1. 镜像回收(使用docker默认 --graph
参数:/var/lib/docker
)
--graph
使用默认的/var/lib/docker
,指向/var文件系统,通过df -lh查看目前 /var 磁盘使用率为30%,启动kubelet设置镜像回收相关参数如下:--image-gc-high-threshold=40 --image-gc-low-threshold=35
此时任意创建两个使用不同镜像的pod,在node节点上可以看到新pull了三个images(pause镜像是启动pod必需的):
[@tc-151-100 /opt/domeos/openxxs/k8s-1.1.7-flannel]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
10.11.150.76:5000/openxxs/iperf 1.2 1783511c56f8 3 months ago 279 MB
10.11.150.76:5000/centos 7 5ddf34d4d69b 8 months ago 172.2 MB
pub.domeos.org/kubernetes/pause latest f9d5de079539 20 months ago 239.8 kB
此时查看/var磁盘使用率达到了41%,然后将使用10.11.150.76:5000/centos:7
镜像的pod删除,等待GC的镜像回收时间点。然而五分钟过去了,什么事情也没有发生=_=!!。还记得 docker rmi 镜像时有个前提条件是什么吗?没错,要求使用该镜像的容器都已经被删除了才可以。前面删除pod只是停止了容器,并没有将容器删除。因此手工将对应的容器docker rm掉,再等待五分钟后,可以看到镜像已经被删除回收了:
[@tc-151-100 /opt/domeos/openxxs/k8s-1.1.7-flannel]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
10.11.150.76:5000/openxxs/iperf 1.2 1783511c56f8 3 months ago 279 MB
pub.domeos.org/kubernetes/pause latest f9d5de079539 20 months ago 239.8 kB
结论:只有相关联的容器都被停止并删除回收后,才能将Kubernetes的镜像垃圾回收策略应用到该镜像上。
--graph
参数:/opt/docker
)结点上运行的docker设置的参数--graph
指向 /opt 磁盘,通过 df -lh 查看目前 /opt 磁盘使用率为 48% ,启动 kubelet 设置镜像回收相关参数如下:
--image-gc-high-threshold=50 --image-gc-low-threshold=40
此时任意创建两个使用不同镜像的pod,在node节点上可以看到新pull了三个images:
[@tc-151-100 /opt/domeos/openxxs/k8s-1.1.7-flannel]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
10.11.150.76:5000/openxxs/iperf 1.2 1783511c56f8 3 months ago 279 MB
10.11.150.76:5000/centos 7 5ddf34d4d69b 8 months ago 172.2 MB
pub.domeos.org/kubernetes/pause latest f9d5de079539 20 months ago 239.8 kB
此时查看/opt磁盘使用率达到了51%,然后将使用10.11.150.76:5000/centos:7镜像的pod删除,手工将对应的容器docker rm掉,等待GC的镜像回收时间点。然而五分钟过去了,十分钟过去了,docker images时centos镜像依旧顽固地坚守在阵地。
结论:目前Kubernetes的镜像垃圾回收策略可以在docker
--graph
参数默认为/var/lib/docker
时正常工作,当--graph
设置为其它磁盘路径时还存在bug。
--docker-root=
。--maximum-dead-containers-per-container
参数--maximum-dead-containers-per-container=1 --minimum-container-ttl-duration=30s --maximum-dead-containers=100
创建一个只包含一个容器且该容器一运行就退出的pod,此时在node节点上可以看到该pod中的容器不断的创建退出创建退出:
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2fe969499164 10.11.150.76:5000/centos:7 "/bin/bash" 4 seconds ago Exited (0) 2 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_92e8bd05-e9e6-11e5-974c-782bcb2a316a_68cc6f03
555b5e7a8550 10.11.150.76:5000/centos:7 "/bin/bash" 24 seconds ago Exited (0) 22 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_92e8bd05-e9e6-11e5-974c-782bcb2a316a_ad4a5e39
94b30a0b32c2 10.11.150.76:5000/centos:7 "/bin/bash" 34 seconds ago Exited (0) 32 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_92e8bd05-e9e6-11e5-974c-782bcb2a316a_4027e3e1
d458e6a7d396 pub.domeos.org/kubernetes/pause:latest "/pause" 34 seconds ago Up 33 seconds k8s_POD.bdb2e1f5_test-gc-pod-exit_default_92e8bd05-e9e6-11e5-974c-782bcb2a316a_09798975
GC的容器回收时间点到达时,可以看到创建时间大于30秒的已退出容器只剩下一个(pause容器不计算),且先创建的容器被优先删除:
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5aae6157aeff 10.11.150.76:5000/centos:7 "/bin/bash" 46 seconds ago Exited (0) 45 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_92e8bd05-e9e6-11e5-974c-782bcb2a316a_f126d2a8
d458e6a7d396 pub.domeos.org/kubernetes/pause:latest "/pause" 2 minutes ago Up 2 minutes k8s_POD.bdb2e1f5_test-gc-pod-exit_default_92e8bd05-e9e6-11e5-974c-782bcb2a316a_09798975
结论:Kubernetes容器垃圾回收的
--maximum-dead-containers-per-container
参数设置可正常工作。
--maximum-dead-containers-per-container
针对容器还是容器集启动kubelet设置容器回收相关参数如下:
--maximum-dead-containers-per-container=1 --minimum-container-ttl-duration=30s --maximum-dead-containers=100
创建一个包含三个容器且这些容器一运行就退出的pod,此时在node节点上可以看到该pod中的容器不断的创建退出创建退出:
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dec04bd28a03 10.11.150.76:5000/centos:7 "/bin/bash" 7 seconds ago Exited (0) 6 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_830a9375
7c94d4a963a7 10.11.150.76:5000/centos:7 "/bin/bash" 7 seconds ago Exited (0) 6 seconds ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_975d44d3
4f3e7e8ddfd5 10.11.150.76:5000/centos:7 "/bin/bash" 8 seconds ago Exited (0) 7 seconds ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_d024eb06
cb48cf2ba133 10.11.150.76:5000/centos:7 "/bin/bash" 12 seconds ago Exited (0) 11 seconds ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_b5ff7373
ec2941f046f0 10.11.150.76:5000/centos:7 "/bin/bash" 13 seconds ago Exited (0) 12 seconds ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_69b1a996
f831e8ed5687 10.11.150.76:5000/centos:7 "/bin/bash" 13 seconds ago Exited (0) 12 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_fbc02e2e
ee972a4537fc pub.domeos.org/kubernetes/pause:latest "/pause" 14 seconds ago Up 13 seconds k8s_POD.bdb2e1f5_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_85b3c032
GC的容器回收时间点到达时,可以看到创建时间大于30秒的已退出容器剩下三个(pause容器不计算),且这三个容器正好是一组:
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4351e6855ae 10.11.150.76:5000/centos:7 "/bin/bash" 51 seconds ago Exited (0) 50 seconds ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_263dd820
990baa6e6a7a 10.11.150.76:5000/centos:7 "/bin/bash" 52 seconds ago Exited (0) 51 seconds ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_b16b5eaa
c6916fb06d65 10.11.150.76:5000/centos:7 "/bin/bash" 53 seconds ago Exited (0) 51 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_1d8ea284
ee972a4537fc pub.domeos.org/kubernetes/pause:latest "/pause" About a minute ago Up About a minute k8s_POD.bdb2e1f5_test-gc-pod-exit_default_d1677c09-e9e7-11e5-974c-782bcb2a316a_85b3c032
结论:
--maximum-dead-containers-per-container
的计数针对一个pod内的容器集而不是容器的个数。
--maximum-dead-containers
参数启动kubelet设置容器回收相关参数如下:
--maximum-dead-containers-per-container=2 --minimum-container-ttl-duration=30s --maximum-dead-containers=3
创建一个包含三个容器的pod,再删除该pod,再创建该pod,再删除该pod,这样就产生了8个已退出容器(包括两个pause容器):
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a28625d189df 10.11.150.76:5000/centos:7 "/bin/bash" 1 seconds ago Exited (0) Less than a second ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_48c11200
97aca44f0deb 10.11.150.76:5000/centos:7 "/bin/bash" 2 seconds ago Exited (0) 1 seconds ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_df34f48d
4e57b6c839ae 10.11.150.76:5000/centos:7 "/bin/bash" 3 seconds ago Exited (0) 2 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_afd622b2
12588fce1433 pub.domeos.org/kubernetes/pause:latest "/pause" 3 seconds ago Exited (2) Less than a second ago k8s_POD.bdb2e1f5_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_c9d4cbaa
621ed207d452 10.11.150.76:5000/centos:7 "/bin/bash" 4 seconds ago Exited (0) 3 seconds ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_c5cbddbb-e9ee-11e5-974c-782bcb2a316a_a91278cd
023c10fad4fd 10.11.150.76:5000/centos:7 "/bin/bash" 5 seconds ago Exited (0) 4 seconds ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_c5cbddbb-e9ee-11e5-974c-782bcb2a316a_6cc03f37
756eb7bb4b53 10.11.150.76:5000/centos:7 "/bin/bash" 5 seconds ago Exited (0) 4 seconds ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_c5cbddbb-e9ee-11e5-974c-782bcb2a316a_83312ec2
d54bdc22773e pub.domeos.org/kubernetes/pause:latest "/pause" 6 seconds ago Exited (2) 3 seconds ago k8s_POD.bdb2e1f5_test-gc-pod-exit_default_c5cbddbb-e9ee-11e5-974c-782bcb2a316a_ccb57220
GC的容器回收时间点到达时,可以看到已退出容器只剩下了三个,pause容器也被回收了:
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a28625d189df 10.11.150.76:5000/centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_48c11200
97aca44f0deb 10.11.150.76:5000/centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_df34f48d
4e57b6c839ae 10.11.150.76:5000/centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_afd622b2
结论:Kubernetes容器垃圾回收的
--maximum-dead-containers
参数设置可正常工作;pause容器也作为可回收容器被管理着;Tips第11条第3)点。
--maximum-dead-containers
对于非kubelet管理的容器是否计数在第5个实验的基础上,手工创建一个container,等待GC的容器回收时间点到达,一分钟过去了,两分钟过去了,docker ps -a 显示的依然是4个容器:
[@tc-151-100 /home/domeos]# docker run -it 10.11.150.76:5000/openxxs/iperf:1.2 /bin/sh
sh-4.2# exit
exit
[@tc-151-100 /home/domeos]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
939b932dc7db 10.11.150.76:5000/openxxs/iperf:1.2 "/bin/sh" 2 minutes ago Exited (0) 2 minutes ago backstabbing_aryabhata
a28625d189df 10.11.150.76:5000/centos:7 "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago k8s_iperf3.5c8de29f_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_48c11200
97aca44f0deb 10.11.150.76:5000/centos:7 "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago k8s_iperf2.5a36e29e_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_df34f48d
4e57b6c839ae 10.11.150.76:5000/centos:7 "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago k8s_iperf1.57dfe29d_test-gc-pod-exit_default_c7612b59-e9ee-11e5-974c-782bcb2a316a_afd622b2
结论:Kubernetes容器垃圾回收的策略不适用于非kubelet管理的容器。
原文链接:https://www.cnblogs.com/openxxs/p/5275051.html
本文转载自openxxs,原文链接:https://www.cnblogs.com/openxxs/p/5275051.html。