深入分析kubelet(4)—— ImageGC

深入分析kubelet(4)—— ImageGC

接著上一篇,繼續(xù)介紹ImageGC過程

code

new

k8s.io\kubernetes\pkg\kubelet\kubelet.go

imageGCPolicy := images.ImageGCPolicy{
    MinAge:               kubeCfg.ImageMinimumGCAge.Duration,
    HighThresholdPercent: int(kubeCfg.ImageGCHighThresholdPercent),
    LowThresholdPercent:  int(kubeCfg.ImageGCLowThresholdPercent),
}
    
images.NewImageGCManager(klet.containerRuntime, klet.StatsProvider, kubeDeps.Recorder, nodeRef, imageGCPolicy, crOptions.PodSandboxImage)

參數(shù)說明:
k8s.io\kubernetes\pkg\kubelet\apis\kubeletconfig\types.go

  • imageGCHighThresholdPercent is the percent of disk usage after which image garbage collection is always run. The percent is calculated as this field value out of 100.
  • imageGCLowThresholdPercent is the percent of disk usage before which image garbage collection is never run. Lowest disk usage to garbage collect to. The percent is calculated as this field value out of 100.

start

// ImageGCPeriod is the period for performing image garbage collection.
    ImageGCPeriod = 5 * time.Minute

// StartGarbageCollection starts garbage collection threads.
func (kl *Kubelet) StartGarbageCollection() {
    go wait.Until(func() {
        kl.imageManager.GarbageCollect()
    }, ImageGCPeriod, wait.NeverStop)
}

每5分鐘執(zhí)行一次image gc

gc

k8s.io\kubernetes\pkg\kubelet\images\image_gc_manager.go

func (im *realImageGCManager) GarbageCollect() error {
    // Get disk usage on disk holding images.
    fsStats, err := im.statsProvider.ImageFsStats()
    
    var capacity, available int64
    if fsStats.CapacityBytes != nil {
        capacity = int64(*fsStats.CapacityBytes)
    }
    if fsStats.AvailableBytes != nil {
        available = int64(*fsStats.AvailableBytes)
    }
    
    // If over the max threshold, free enough to place us at the lower threshold.
    usagePercent := 100 - int(available*100/capacity)
    if usagePercent >= im.policy.HighThresholdPercent {
        amountToFree := capacity*int64(100-im.policy.LowThresholdPercent)/100 - available
        im.freeSpace(amountToFree, time.Now())
    }
}

獲取鏡像盤信息,根據(jù)策略算出需要釋放多少磁盤空間。

// Tries to free bytesToFree worth of images on the disk.
//
// Returns the number of bytes free and an error if any occurred. The number of
// bytes freed is always returned.
// Note that error may be nil and the number of bytes free may be less
// than bytesToFree.
func (im *realImageGCManager) freeSpace(bytesToFree int64, freeTime time.Time) (int64, error) {
    imagesInUse, err := im.detectImages(freeTime)

    // Get all images in eviction order.
    images := make([]evictionInfo, 0, len(im.imageRecords))
    for image, record := range im.imageRecords {
        if isImageUsed(image, imagesInUse) {
            continue
        }
        images = append(images, evictionInfo{
            id:          image,
            imageRecord: *record,
        })
    }
    sort.Sort(byLastUsedAndDetected(images))

    // Delete unused images until we've freed up enough space.
    var deletionErrors []error
    spaceFreed := int64(0)
    for _, image := range images {
        // Images that are currently in used were given a newer lastUsed.
        if image.lastUsed.Equal(freeTime) || image.lastUsed.After(freeTime) {
            continue
        }

        // Avoid garbage collect the image if the image is not old enough.
        // In such a case, the image may have just been pulled down, and will be used by a container right away.
        if freeTime.Sub(image.firstDetected) < im.policy.MinAge {
            continue
        }

        // Remove image. Continue despite errors.
        im.runtime.RemoveImage(container.ImageSpec{Image: image.id})
        
        spaceFreed += image.size

        if spaceFreed >= bytesToFree {
            break
        }
}

找到?jīng)]有正在使用的鏡像,按照上次使用時間排序,刪除最久沒有使用過的鏡像。上次使用時間差必須大于MinAge,最后通過CRI刪除鏡像。

如果釋放資源不滿足要求,會返回錯誤。

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容