Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Kubernetes garbage Collection Mechanism

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Kubernetes garbage collection mechanism example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Discovery of problems with Kubernetes garbage collection mechanism

In the test environment, the image above the node node is suddenly deleted, so find out the principle and mechanism of deletion.

Garbage collection mechanism

The GC feature of Kubelet cleans up unused image and container. Kubelet executes GC on container every minute and GC on image every 5 minutes. External garbage collection tools are not recommended because they can break Kubelet.

ImageKubernetes works with Cadvisor to manage the lifecycle of all image through ImageManager. Image's GC policy consists of a high threshold and a low threshold, and disk usage above the high threshold triggers GC to delete the least recently used images until the low threshold is met. Containercontainer's GC mainly has three user-defined variables: MinAge: the minimum time for a container to be GC MaxPerPodContainer: the maximum number of dead containers allowed in each PodContainer, and PodContainer refers to 1 Container instead of podMaxContainers: the maximum number of dead containers Minage=0,MaxPerPodContainer and MaxContainers MaxContainers,maxperpodcontainer will be adjusted until downgraded to 1 and evicted from the oldest container. Deleted containers owned by pods are deleted as soon as they exceed the MinAge. Containers that are not managed by Kubelet are not constrained by container garbage collection. Configuring GC is achieved by modifying the kubelet flags. Image gc parameter: image-gc-high-threshold image GC high threshold percentage, default is 85%image-gc-low-threshold image GC low threshold percentage, default is 80%minimum-container-ttl-duration MinAge parameter, default is 0maximum-dead-containers-per-container MaxPerPodContainer, default is 1.maximum-dead-containers MaxContainers The default is-1, which means that there is no restriction that Container may be included in logs and other data useful for troubleshooting by GC,Container before it expires. It is strongly recommended that MaxPerPodContainer and maximum-dead-containers are large enough.

GC effect

Configure a container profile pod-gc.yaml that will fail after 10s

ApiVersion: v1kind: Podmetadata: name: gc-testspec: containers:-name: busybox-gc-1 image: busybox:v1 command:-/ bin/sh-- c-'sleep 10 & & hello'

Create a pod using yaml

Kubectl apply-f pod-gc.yaml

Check the status of docker. When the container of 2 exit appears, the GC will be started and the earliest container will be reclaimed.

# docker ps-a | grep busybox-gc8ddbfcf4ebee 59788edf1f3e "/ bin/sh-c 'sleep 1 …" 2 seconds ago Up 1 second k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_57d7c1ade82af 59788edf1f3e "/ bin/sh-c 'sleep 1 …" About a minute ago Exited About a minute ago k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_4# docker ps-a | grep busybox-gc8ddbfcf4ebee 59788edf1f3e "/ bin/sh-c 'sleep 1 …" 10 seconds ago Up 9 seconds k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_57d7c1ade82af 59788edf1f3e "/ bin/sh-c 'sleep 1..." About a minute ago Exited About a minute ago k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_4# docker ps-a | grep busybox-gc8ddbfcf4ebee 59788edf1f3e "/ bin/sh-c 'sleep 1 …" 11 seconds ago Exited Less than a second ago k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_57d7c1ade82af 59788edf1f3e "/ bin/sh-c 'sleep 1..." About a minute ago Exited About a minute ago k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_4# docker ps-a | grep busybox-gc8ddbfcf4ebee 59788edf1f3e "/ bin/sh-c 'sleep 1 …" 12 seconds ago Exited 1 second ago k8s_busybox-gc-1_gc-test_default_3651c167-75f0-11e9-bc74-52540005f38a_5

Modify the configuration file / var/lib/kubelet/kubeadm-flags.env add parameter-maximum-dead-containers-per-container=2

KUBELET_KUBEADM_ARGS=--cgroup-driver=systemd-- network-plugin=cni-- pod-infra-container-image=k8s.gcr.io/pause:3.1-- maximum-dead-containers-per-container=2systemctl restart kubelet # # restart the kubelet service kubectl delete-f pod-gc.yaml # # delete the previous podkubectl apply-f pod-gc.yaml # # add pod again

As you can see, when the number of containers in exit reaches 3, gc will be triggered.

# docker ps-a | grep busybox-gcc8677098e9aa af2f74c517aa "/ bin/sh-c 'sleep 1 …" 8 seconds ago Up 8 seconds k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_2f0bc1b7893bd af2f74c517aa "/ bin/sh-c 'sleep 1 …" 34 seconds ago Exited 23 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_19e7fa118b1d3 af2f74c517aa "/ bin/sh-c 'sleep 1 …" 45 seconds ago Exited 35 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_0# docker ps-a | grep busybox-gcc8677098e9aa af2f74c517aa "/ bin/sh-c 'sleep 1 …" 9 seconds ago Up 9 seconds k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_2f0bc1b7893bd af2f74c517aa "/ bin/sh-c 'sleep 1..." 35 seconds ago Exited 24 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_19e7fa118b1d3 af2f74c517aa "/ bin/sh-c 'sleep 1 …" 46 seconds ago Exited 36 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_0# docker ps-a | grep busybox-gcc8677098e9aa af2f74c517aa "/ bin/sh-c 'sleep 1 …" 10 seconds ago Exited Less than a second ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_2f0bc1b7893bd af2f74c517aa "/ bin/sh-c 'sleep 1..." 36 seconds ago Exited 25 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_19e7fa118b1d3 af2f74c517aa "/ bin/sh-c 'sleep 1 …" 47 seconds ago Exited 36 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_0# docker ps-a | grep busybox-gcc8677098e9aa af2f74c517aa "/ bin/sh-c 'sleep 1 …" 11 seconds ago Exited (127) 1 second ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_2f0bc1b7893bd af2f74c517aa "/ bin/sh-c 'sleep 1..." 37 seconds ago Exited (12726 seconds ago k8s_busybox-gc-1_gc-test_default_3425a55b-75f5-11e9-bc74-52540005f38a_1) Thank you for reading this article carefully. I hope the article "sample Analysis of Kubernetes garbage Collection Mechanism" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report