pod绑核规则

pod绑核是按照container来绑定的,如果一个pod有多个container对于不同的container会区别对待。

// kubernetes/pkg/kubelet/cm/cpumanager/policy_static.go

func (p *staticPolicy) AddContainer(s state.State, pod *v1.Pod, container *v1.Container, containerID string) (rerr error) {
    // So long as this function does not return an error,
    // add (pod, container, containerID) to the containerMap.
    defer func() {
        if rerr == nil {
            p.containerMap.Add(pod, container, containerID)
        }
    }()

    if numCPUs := p.guaranteedCPUs(pod, container); numCPUs != 0 {
        klog.Infof("[cpumanager] static policy: AddContainer (pod: %s, container: %s, container id: %s)", pod.Name, container.Name, containerID)
        // container belongs in an exclusively allocated pool

        if _, ok := s.GetCPUSet(containerID); ok {
            klog.Infof("[cpumanager] static policy: container already present in state, skipping (container: %s, container id: %s)", container.Name, containerID)
            return nil
        }

        // Proactively remove CPUs from init containers that have already run.
        // They are guaranteed to have run to completion before any other
        // container is run.
        for _, initContainer := range pod.Spec.InitContainers {
            if container.Name != initContainer.Name {
                initContainerID, err := p.containerMap.Get(pod, &initContainer)
                if err != nil {
                    continue
                }
                err = p.RemoveContainer(s, initContainerID)
                if err != nil {
                    klog.Warningf("[cpumanager] unable to remove init container (container id: %s, error: %v)", initContainerID, err)
                }
            }
        }

        // Call Topology Manager to get the aligned socket affinity across all hint providers.
        hint := p.affinity.GetAffinity(string(pod.UID), container.Name)
        klog.Infof("[cpumanager] Pod %v, Container %v Topology Affinity is: %v", pod.UID, container.Name, hint)

        // Allocate CPUs according to the NUMA affinity contained in the hint.
        cpuset, err := p.allocateCPUs(s, numCPUs, hint.NUMANodeAffinity)
        if err != nil {
            klog.Errorf("[cpumanager] unable to allocate %d CPUs (container id: %s, error: %v)", numCPUs, containerID, err)
            return err
        }
        s.SetCPUSet(containerID, cpuset)
    }
    // container belongs in the shared pool (nothing to do; use default cpuset)
    return nil
}

func (p *staticPolicy) guaranteedCPUs(pod *v1.Pod, container *v1.Container) int {
    if v1qos.GetPodQOS(pod) != v1.PodQOSGuaranteed {
        return 0
    }
    cpuQuantity := container.Resources.Requests[v1.ResourceCPU]
    if cpuQuantity.Value()*1000 != cpuQuantity.MilliValue() {
        return 0
    }
    // Safe downcast to do for all systems with < 2.1 billion CPUs.
    // Per the language spec, `int` is guaranteed to be at least 32 bits wide.
    // https://golang.org/ref/spec#Numeric_types
    return int(cpuQuantity.Value())
}

其中guaranteedCPUs代表了绑核规则的判断。
1.pod必须是Guaranteed级别的
2.container的cpu必须是整数核的

你可能感兴趣的:(pod绑核规则)