溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何避免container頻繁地restart?

發(fā)布時(shí)間:2020-05-28 09:38:24 來(lái)源:億速云 閱讀:251 作者:Leah 欄目:系統(tǒng)運(yùn)維

如何避免container頻繁地restart?相信大部分人都還沒(méi)學(xué)會(huì)這個(gè)技能,為了讓大家學(xué)會(huì),給大家總結(jié)了以下內(nèi)容,話不多說(shuō),一起往下看吧。

分析:

  1. 在 Pod 中 restart container 的時(shí)候(具體時(shí)機(jī)是,周期性執(zhí)行 SyncPod() 的時(shí)候),Pod 會(huì)通過(guò)自身的 Status 結(jié)構(gòu)找到當(dāng)前這個(gè) container(因?yàn)?Pod 中可能有多個(gè) container)上一次退出的時(shí)間,記為 ts
  2. 如果是第一次 restart,那么直接重啟 container,并且在 Kubelet 的 backOff.perItemBackoff (一個(gè) map 結(jié)構(gòu),key 是根據(jù) container 和所在 pod 對(duì)象計(jì)算出來(lái)的 id)中記錄下次 backoff 的時(shí)間(初始值為 10s,然后按照指數(shù)增長(zhǎng),最大 5min)
  3. 如果不是第一次 restart,即 Kubelet 的 backOff.perItemBackoff 中已經(jīng)有這個(gè) container 的 backOff 記錄,計(jì)為 backoff,那么
    • 如果 now() - ts < backoff,表明等待的時(shí)間還不夠,拋出 CrashLoopBackOff Event(然后等到下一個(gè) SyncPod 的周期到的時(shí)候,重新比較這個(gè)值)
    • 否則,說(shuō)明已經(jīng)等待 backoff 時(shí)間了,可以 restart 了,此時(shí)執(zhí)行 backOff.Next(),將該容器對(duì)應(yīng)的 backoff 翻倍,然后執(zhí)行 restart 操作
  4. 在步驟 3 中計(jì)算 backoff 的過(guò)程中,還會(huì)去檢查當(dāng)前時(shí)間距離上一次 container 退出時(shí)的間隔,如果大于 2 * MaxContainerBackOff = 10 minutes,那么會(huì)將這個(gè) container 對(duì)應(yīng)的 backoff 重置為初始值 10s

源碼細(xì)節(jié)

  1. kubernetes/pkg/kubelet/kubelet.go
    通過(guò)源碼發(fā)現(xiàn),kubernetes/pkg/kubelet/kubelet.go 文件中有兩個(gè)常量:

    MaxContainerBackOff = 300 * time.Second
    backOffPeriod = time.Second * 10

使用這兩個(gè)變量構(gòu)造了一個(gè) BackOff 對(duì)象,這個(gè)是 kubelet 的屬性,對(duì)該 node 上所有 pod 都適用

    klet.backOff = flowcontrol.NewBackOff(backOffPeriod, MaxContainerBackOff)

BackOff 結(jié)構(gòu)如下

type Backoff struct {
        sync.Mutex
        Clock           clock.Clock
        defaultDuration time.Duration
        maxDuration     time.Duration
        perItemBackoff  map[string]*backoffEntry
}

然后在 SyncPod 方法中使用這個(gè)對(duì)象

// Call the container runtime's SyncPod callback
result := kl.containerRuntime.SyncPod(pod, apiPodStatus, podStatus, pullSecrets, kl.backOff)
  1. kubernetes/pkg/kubelet/kuberuntime/kuberuntime_manager.go

SyncPod 具體做的事有:

// SyncPod syncs the running pod into the desired pod by executing following steps:
//
//  1. Compute sandbox and container changes.
//  2. Kill pod sandbox if necessary.
//  3. Kill any containers that should not be running.
//  4. Create sandbox if necessary.
//  5. Create init containers.
//  6. Create normal containers.
func (m *kubeGenericRuntimeManager) SyncPod(pod *v1.Pod, _ v1.PodStatus, podStatus *kubecontainer.PodStatus, pullSecrets []v1.Secret, backOff *flowcontrol.Backoff) (result kubecontainer.PodSyncResult) {

同樣在這個(gè)文件中,有一個(gè)關(guān)鍵的函數(shù)

// If a container is still in backoff, the function will return a brief backoff error and
// a detailed error message.
    func (m *kubeGenericRuntimeManager) doBackOff(pod *v1.Pod, container *v1.Container, podStatus *kubecontainer.PodStatus, backOff *flowcontrol.Backoff) (bool, string, error) {
        var cStatus *kubecontainer.ContainerStatus
        for _, c := range podStatus.ContainerStatuses {
                if c.Name == container.Name && c.State == kubecontainer.ContainerStateExited {
                        cStatus = c
                        break
                }
        }

        if cStatus == nil {
                return false, "", nil
        }

        glog.Infof("checking backoff for container %q in pod %q", container.Name, format.Pod(pod))
        // Use the finished time of the latest exited container as the start point to calculate whether to do back-off.
        ts := cStatus.FinishedAt
        // backOff requires a unique key to identify the container.
        key := getStableKey(pod, container)
        if backOff.IsInBackOffSince(key, ts) {
                if ref, err := kubecontainer.GenerateContainerRef(pod, container); err == nil {
                        m.recorder.Eventf(ref, v1.EventTypeWarning, events.BackOffStartContainer, "Back-off restarting failed container")
                }
                err := fmt.Errorf("Back-off %s restarting failed container=%s pod=%s", backOff.Get(key), container.Name, format.Pod(pod))
                glog.Infof("%s", err.Error())
                return true, err.Error(), kubecontainer.ErrCrashLoopBackOff
        }

        backOff.Next(key, ts)
        return false, "", nil
}

其中 backOff.Next 函數(shù)定義如下

// move backoff to the next mark, capping at maxDuration
func (p *Backoff) Next(id string, eventTime time.Time) {
        p.Lock()
        defer p.Unlock()
        entry, ok := p.perItemBackoff[id]
        if !ok || hasExpired(eventTime, entry.lastUpdate, p.maxDuration) {
                entry = p.initEntryUnsafe(id)
        } else {
                delay := entry.backoff * 2 // exponential
                entry.backoff = time.Duration(integer.Int64Min(int64(delay), int64(p.maxDuration)))
        }
        entry.lastUpdate = p.Clock.Now()
}

看完上述內(nèi)容,你們掌握避免container頻繁地restart的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI