Kubernetes,将cronjob pod扩展到另一个节点

时间:2020-04-11 12:07:26

标签: amazon-web-services kubernetes cron kubernetes-cronjob

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

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

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

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

1 个答案:

答案 0 :(得分:1)

您可以在cronjob的Pod模板部分中使用Inter pod antiAffinity。Pod间的亲和力和反亲和力使您可以根据已经运行的Pod上的标签来限制Pod可以调度哪些节点在节点上,而不是基于节点上的标签。规则的格式为“如果该X已经运行了一个或多个满足规则Y的Pod,则该Pod应该(或者在非亲和性的情况下不应该)运行在X中”

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标签,则可能导致意外行为。