蓝盾「Kubernetes调度优化」

背景

在 蓝盾「Docker公共构建机」缓存清理 中我们通过分析源码,知道拉起的构建 Pod 通过 hostPath 挂载工作目录做缓存。我们接下来进一步分析创建 Pod 的流程。

部署配置

dispatch:
  # 调度需要使用到的label,确定构建机唯一性
  label: bkci.dispatch.kubenetes/core
  watch:
    task:
      label: bkci.dispatch.kubenetes/watch-task
  builder:
    # 将构建机调度到指定标签节点的配置,不填写则在集群内都可以调度,优先级小于专机和特殊机器
    nodeSelector:
      label:
      value:
    # 构建机曾经调度过的节点名称列表
    nodesAnnotation: bkci.dispatch.kubenetes/builder-history-nodes
    # 容器历史资源使用相关
    realResource:
      # 监控构建机容器资源使用的 prometheus api地址,字段为空则不开启 realResource 优化
      prometheusUrl:
      realResourceAnnotation: bkci.dispatch.kubenetes/builder-real-resources
  # 一些具有特定属性的机器,例如独特的网络策略
  specialMachine:
    label: bkci.dispatch.kubenetes/special-builder
  # 只给特定用户使用的专机
  privateMachine:
    label: bkci.dispatch.kubenetes/private-builder

通过 dispatch-k8s-manager 模块的配置文件,我们发现可以通过 nodeSelector、nodesAnnotation、realResource 等配置来设置调度策略。

源码分析

亲和性和污点容忍

func CreateBuilder(builder *Builder) (taskId string, err error) {
	volumes, volumeMounts := getBuilderVolumeAndMount(builder.Name, builder.NFSs)

	var replicas int32 = 1
	tolers, nodeMatches := buildDedicatedBuilder(builder)
	annotations, err := getBuilderAnnotations(builder.Name)
	if err != nil {
		return "", err
	}

	go task.DoCreateBuilder(
		taskId,
		&kubeclient.Deployment{
			Name:        builder.Name,
			Labels:      labels,
			MatchLabels: matchlabels,
			Replicas:    &replicas,
			Pod: kubeclient.Pod{
				Labels:      labels,
				Annotations: annotations,
				Volumes:     volumes,
				Containers: []kubeclient.Container{
					{
						Image:        builder.Image,
						Resources:    *resources,
						Env:          getEnvs(builder.Env),
						Command:      builder.Command,
						VolumeMounts: volumeMounts,
					},
				},
				NodeMatches:     nodeMatches,
				Tolerations:     tolers,
				PullImageSecret: pullImageSecret,
			},
		},
	)
	return taskId, nil
}

// buildDedicatedBuilder 获取污点和节点亲和度配置
func buildDedicatedBuilder(builder *Builder) ([]corev1.Toleration, []kubeclient.NodeMatch) {
    // 优先读取专机配置
    // 读取具有特殊配置的机器
    // 如果配置中配置了节点选择器则使用节点选择器
    return nil, nil
}

// getBuilderAnnotations 获取构建机注释配置
func getBuilderAnnotations(builderName string) (map[string]string, error) {
	// 获取节点记录,用来把构建机分配到已有的节点
	// 获取RealResource记录
	return result, nil
}

在 kubeclient 的 CreateDeployment 方法中:

func CreateDeployment(dep *Deployment) error {
	// 将 NodeMatches 转为 nodeAffinity
	var affinity *corev1.Affinity
	if len(dep.Pod.NodeMatches) > 0 {
		var matches []corev1.NodeSelectorRequirement
		for _, mat := range dep.Pod.NodeMatches {
			matches = append(matches, corev1.NodeSelectorRequirement{
				Key:      mat.Key,
				Operator: mat.Operator,
				Values:   mat.Values,
			})
		}
		affinity = &corev1.Affinity{
			NodeAffinity: &corev1.NodeAffinity{
				RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
					NodeSelectorTerms: []corev1.NodeSelectorTerm{
						{
							MatchExpressions: matches,
						},
					},
				},
			},
		}
	}
	...
}

在 CreateBuilder 里,调度相关的两个核心参数 tolers 和 nodeMatches 都是通过 buildDedicatedBuilder(builder) 返回的:

历史节点调度

蓝盾源码里我们找到了有关亲和性以及污点容忍的实现,但是有关历史节点调度的实现只有通过 getBuilderAnnotations 给 Pod 设置注解。至于如何通过注解影响调度在蓝盾源码里并没有找到相关内容。

我们进一步分析发现,历史节点调度需要通过 蓝盾 K8S 调度插件 实现。

apiVersion: v1
kind: Pod
metadata:
  annotations:
    bkci.dispatch.kubenetes/builder-history-nodes: '["10.x.x.1","10.x.x.2","10.x.x.3"]'
  labels:
    bkci.dispatch.kubenetes/core: build1753761077695-ivcpmoxg
    bkci.dispatch.kubenetes/watch-task: t-1753785688231121886-iInjpMUr-builder-start
  name: build1753761077695-ivcpmoxg-c9d8fc6c9-mqhkk
const nodesAnnotation = "bkci.dispatch.kubenetes/builder-history-nodes"
const readResourceAnnotation = "bkci.dispatch.kubenetes/builder-real-resources"

type realResourceUsage struct {
    Cpu    string `json:"cpu"`
    Memory string `json:"memory"`
}

func (s *SchedulerPlugin) Score(_ context.Context, _ *framework.CycleState, pod *v1.Pod, nodeName string) (int64, *framework.Status) {
    // 读取历史节点信息
    var nodeHis []string
    if nodesS, ok := pod.ObjectMeta.Annotations[nodesAnnotation]; ok {
        _ = json.Unmarshal([]byte(nodesS), &nodeHis)
    }

    // 读取资源信息
    var realResources []realResourceUsage
    if realS, ok := pod.ObjectMeta.Annotations[readResourceAnnotation]; ok {
        _ = json.Unmarshal([]byte(realS), &realResources)
    }

    // 计算历史节点分数
    nodeScore := calculateNodeHisScore(nodeHis, nodeName)
    // 计算资源分数
    realResourceScore := ...

    return nodeScore + realResourceScore, nil
}

var nodeHisScores = map[int]int64{0: 30, 1: 20, 2: 10}

// calculateNodeHisScore 计算历史节点分数,将3个历史节点从最近到最远依次打分 30 - 10分
func calculateNodeHisScore(nodeHis []string, nodeName string) int64 {
	if len(nodeHis) == 0 {
		return framework.MinNodeScore
	}

	for index, name := range nodeHis {
		if name != nodeName {
			continue
		}

		score := framework.MinNodeScore
		if indexS, ok := nodeHisScores[index]; ok {
			score = indexS
		}

		return score
	}

	return framework.MinNodeScore
}

总结

在蓝盾流水线中,通过以下方式实现了 Kubernetes 的调度优化:

这些机制协同提升了调度效率和资源利用率。

参考