Kubernetes,将 cronjob pod 扩展到不同的节点
Kubernetes, scaling cronjob pod to a different node
我们有一个 kubernetes cronjob,其任务是检测新上传的文件,并对其执行一些操作。此操作每分钟运行一次,可能需要 10 分钟才能完成。
目前它可以工作,并且会在检测到新文件时为作业创建新的 pods。但是,我们希望由 cronjob 创建的 pod 生成到不同的节点上。在这个阶段,我的所有 pods 都在同一个节点中生成,这可能会导致我的 EC2 实例在最坏的情况下崩溃,因为有很多新文件并且我的系统运行我们的内存。
我正在使用 EFS 文件系统在我的不同节点之间共享文件,因此所有节点都可以读取上传的文件。
如何使用 kubernetes cronjobs 让新 pods 在不同的节点上生成?
您可以在 cronjob.Inter 的 pod 模板部分使用 Inter pod antiAffinity-pod 亲和力和反亲和力允许您根据 pods 已经 运行 在节点上,而不是基于节点上的标签。规则的形式是“如果 X 已经 运行 一个或多个 pods 符合规则 Y”
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 标签,可能会导致意外行为。
我们有一个 kubernetes cronjob,其任务是检测新上传的文件,并对其执行一些操作。此操作每分钟运行一次,可能需要 10 分钟才能完成。
目前它可以工作,并且会在检测到新文件时为作业创建新的 pods。但是,我们希望由 cronjob 创建的 pod 生成到不同的节点上。在这个阶段,我的所有 pods 都在同一个节点中生成,这可能会导致我的 EC2 实例在最坏的情况下崩溃,因为有很多新文件并且我的系统运行我们的内存。
我正在使用 EFS 文件系统在我的不同节点之间共享文件,因此所有节点都可以读取上传的文件。
如何使用 kubernetes cronjobs 让新 pods 在不同的节点上生成?
您可以在 cronjob.Inter 的 pod 模板部分使用 Inter pod antiAffinity-pod 亲和力和反亲和力允许您根据 pods 已经 运行 在节点上,而不是基于节点上的标签。规则的形式是“如果 X 已经 运行 一个或多个 pods 符合规则 Y”
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 标签,可能会导致意外行为。