只关注部分:
如何将工作负载 ( Pod
, Job
, Cronjob
) 安排在一组特定的Nodes
您可以通过以下方式生成Cronjob
/ Job
:
nodeSelector
nodeAffinity
nodeSelector
nodeSelector
是最简单的节点选择约束推荐形式。nodeSelector
是 PodSpec 的一个字段。它指定键值对的映射。为了使 pod 有资格在节点上运行,节点必须将每个指示的键值对作为标签(它也可以有额外的标签)。最常见的用法是一个键值对。
-- Kubernetes.io:文档:概念:调度驱逐:分配 pod 节点:节点选择器
它的示例可能如下(假设您的节点具有在 中引用的特定标签.spec.jobTemplate.spec.template.spec.nodeSelector
):
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
nodeSelector: # <-- IMPORTANT
schedule: "here" # <-- IMPORTANT
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
在清单上方运行会将您的Pod
( Cronjob
) 安排在具有schedule=here
标签的节点上:
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-1616323740-mqdmq 0/1 Completed 0 2m33s 10.4.2.67 node-ffb5 <none> <none>
hello-1616323800-wv98r 0/1 Completed 0 93s 10.4.2.68 node-ffb5 <none> <none>
hello-1616323860-66vfj 0/1 Completed 0 32s 10.4.2.69 node-ffb5 <none> <none>
nodeAffinity
节点亲和性在概念上类似于nodeSelector
- 它允许您根据节点上的标签来限制您的 pod 有资格在哪些节点上进行调度。
目前有两种类型的节点亲和性,称为requiredDuringSchedulingIgnoredDuringExecution
和preferredDuringSchedulingIgnoredDuringExecution
。您可以将它们分别视为“硬”和“软”,因为前者指定了将 Pod 调度到节点上必须满足的规则(就像 nodeSelector,但使用更具表现力的语法),而后者指定调度程序将尝试强制执行但不保证的首选项。
-- Kubernetes.io: Docs: Concepts: Scheduling eviction: Assign pod node: Node affinity
它的示例可能如下(假设您的节点具有在 中引用的特定标签.spec.jobTemplate.spec.template.spec.nodeSelector
):
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
# --- nodeAffinity part
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: schedule
operator: In
values:
- here
# --- nodeAffinity part
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-1616325840-5zkbk 0/1 Completed 0 2m14s 10.4.2.102 node-ffb5 <none> <none>
hello-1616325900-lwndf 0/1 Completed 0 74s 10.4.2.103 node-ffb5 <none> <none>
hello-1616325960-j9kz9 0/1 Completed 0 14s 10.4.2.104 node-ffb5 <none> <none>
其他资源:
我想你也可以看看这个 StackOverflow 答案: