1

我们有一个 Kubernetes cronjob,它的任务是检测一个新上传的文件,并对其执行一些操作。此操作每分钟运行一次,可能需要 10 分钟才能完成。

目前它可以工作,并在检测到新文件时为作业创建新的 pod。但是,我们希望将由 cronjob 创建的 pod 生成到不同的节点上。在这个阶段,我的所有 pod 都在同一个节点中生成,这可能会导致我的 EC2 实例在最坏的情况下崩溃,即有很多新文件并且我的系统运行我们的内存。

我正在使用 EFS 文件系统在不同节点之间共享文件,因此所有节点都可以读取上传的文件。

如何使用 kubernetes cronjobs 在不同的节点上生成新的 pod?

4

1 回答 1

3

您可以在 cronjob 的 pod 模板部分使用Pod间反亲和性。Pod 间亲和性和反亲和性允许您根据已经在节点上运行的 pod 上的标签来限制您的 pod 有资格调度哪些节点,而不是基于节点上的标签。规则的形式是“如果 X 已经在运行一个或多个符合规则 Y 的 pod,则该 pod 应该(或者,在反亲和的情况下,不应该)在 X 中运行”</p>

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          affinity:
            podAntiAffinity:
              requiredDuringSchedulingIgnoredDuringExecution:
              - labelSelector:
                  matchExpressions:
                  - key: app
                    operator: In
                    values:
                    - web-store
                topologyKey: "kubernetes.io/hostname"
          containers:
            - name: hello
              image: bash
              command: ["echo",  "Hello world"]
          restartPolicy: OnFailure

必要的 API 文档

kubectl explain cronjob.spec.jobTemplate.spec.template.spec.affinity.podAntiAffinity
KIND:     CronJob
VERSION:  batch/v1beta1

RESOURCE: podAntiAffinity <Object>

DESCRIPTION:
     Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod
     in the same node, zone, etc. as some other pod(s)).

     Pod anti affinity is a group of inter pod anti affinity scheduling rules.

FIELDS:
   preferredDuringSchedulingIgnoredDuringExecution  <[]Object>
     The scheduler will prefer to schedule pods to nodes that satisfy the
     anti-affinity expressions specified by this field, but it may choose a node
     that violates one or more of the expressions. The node that is most
     preferred is the one with the greatest sum of weights, i.e. for each node
     that meets all of the scheduling requirements (resource request,
     requiredDuringScheduling anti-affinity expressions, etc.), compute a sum by
     iterating through the elements of this field and adding "weight" to the sum
     if the node has pods which matches the corresponding podAffinityTerm; the
     node(s) with the highest sum are the most preferred.

   requiredDuringSchedulingIgnoredDuringExecution   <[]Object>
     If the anti-affinity requirements specified by this field are not met at
     scheduling time, the pod will not be scheduled onto the node. If the
     anti-affinity requirements specified by this field cease to be met at some
     point during pod execution (e.g. due to a pod label update), the system may
     or may not try to eventually evict the pod from its node. When there are
     multiple elements, the lists of nodes corresponding to each podAffinityTerm
     are intersected, i.e. all terms must be satisfied.

注意: Pod 反亲和要求节点标签一致,也就是说集群中的每个节点都必须有一个合适的标签匹配 topologyKey。如果某些或所有节点缺少指定的 topologyKey 标签,则可能导致意外行为。

于 2020-04-11T12:24:20.977 回答