3.5.1 使用硬策略requiredDuringSchedulingIgnoredDuringExecution创建pod,requiredDuringSchedulingIgnoredDuringExecution参数表示:节点必须包含一个键名为 kubernetes.io/hostname
的标签,并且该标签的取值必须为 k8scloude2
或 k8scloude3
。
你可以使用 operator 字段来为 Kubernetes 设置在解释规则时要使用的逻辑操作符 。你可以使用 In、NotIn、Exists、DoesNotExist、Gt 和 Lt 之一作为操作符 。NotIn 和 DoesNotExist 可用来实现节点反亲和性行为 。你也可以使用节点污点 将 Pod 从特定节点上驱逐 。
注意:
- 如果你同时指定了 nodeSelector 和 nodeAffinity,两者 必须都要满足, 才能将 Pod 调度到候选节点上 。
- 如果你指定了多个与 nodeAffinity 类型关联的 nodeSelectorTerms,只要其中一个 nodeSelectorTerms 满足的话,Pod 就可以被调度到节点上 。
- 如果你指定了多个与同一 nodeSelectorTerms 关联的 matchExpressions,则只有当所有 matchExpressions 都满足时 Pod 才可以被调度到节点上 。
[root@k8scloude1 pod]# vim requiredDuringSchedule.yaml#硬策略[root@k8scloude1 pod]# cat requiredDuringSchedule.yamlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: pod1name: pod1namespace: podspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/hostnameoperator: Invalues:- k8scloude2- k8scloude3containers:- image: nginximagePullPolicy: IfNotPresentname: pod1resources: {}ports:- name: httpcontainerPort: 80protocol: TCPhostPort: 80dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}[root@k8scloude1 pod]# kubectl apply -f requiredDuringSchedule.yamlpod/pod1 created
可以看到pod运行在k8scloude3节点[root@k8scloude1 pod]# kubectl get podsNAMEREADYSTATUSRESTARTSAGEpod11/1Running06s[root@k8scloude1 pod]# kubectl get pods -o wideNAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATESpod11/1Running010s10.244.251.212k8scloude3<none><none>[root@k8scloude1 pod]# kubectl delete pod pod1 --forcewarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "pod1" force deleted
创建pod,requiredDuringSchedulingIgnoredDuringExecution参数表示:节点必须包含一个键名为 kubernetes.io/hostname
的标签,并且该标签的取值必须为 k8scloude4
或 k8scloude5
。[root@k8scloude1 pod]# vim requiredDuringSchedule1.yaml [root@k8scloude1 pod]# cat requiredDuringSchedule1.yamlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: pod1name: pod1namespace: podspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/hostnameoperator: Invalues:- k8scloude4- k8scloude5containers:- image: nginximagePullPolicy: IfNotPresentname: pod1resources: {}ports:- name: httpcontainerPort: 80protocol: TCPhostPort: 80dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}[root@k8scloude1 pod]# kubectl apply -f requiredDuringSchedule1.yamlpod/pod1 created
由于requiredDuringSchedulingIgnoredDuringExecution是硬策略,k8scloude4 , k8scloude5不满足条件,所以pod创建失败[root@k8scloude1 pod]# kubectl get pods -o wideNAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATESpod10/1Pending07s<none><none><none><none>[root@k8scloude1 pod]# kubectl delete pod pod1 --forcewarning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.pod "pod1" force deleted
3.5.2 使用软策略preferredDuringSchedulingIgnoredDuringExecution给节点打标签[root@k8scloude1 pod]# kubectl label nodes k8scloude2 xx=72node/k8scloude2 labeled[root@k8scloude1 pod]# kubectl label nodes k8scloude3 xx=59node/k8scloude3 labeled
创建pod,preferredDuringSchedulingIgnoredDuringExecution参数表示:节点最好具有一个键名为 xx
且取值大于 60
的标签 。[root@k8scloude1 pod]# vim preferredDuringSchedule.yaml [root@k8scloude1 pod]# cat preferredDuringSchedule.yamlapiVersion: v1kind: Podmetadata:creationTimestamp: nulllabels:run: pod1name: pod1namespace: podspec:affinity:nodeAffinity:preferredDuringSchedulingIgnoredDuringExecution:- weight: 2preference:matchExpressions:- key: xxoperator: Gtvalues:- "60"containers:- image: nginximagePullPolicy: IfNotPresentname: pod1resources: {}ports:- name: httpcontainerPort: 80protocol: TCPhostPort: 80dnsPolicy: ClusterFirstrestartPolicy: Alwaysstatus: {}[root@k8scloude1 pod]# kubectl apply -f preferredDuringSchedule.yamlpod/pod1 created
可以看到pod运行在k8scloude2,因为k8scloude2标签为 xx=72,72大于60
推荐阅读
- JAVA开发搞了一年多的大数据,究竟干了点啥
- JAVA的File对象
- 原神须弥地区死域有什么效果
- 原神巡林小队三号林区位置在哪
- 不思议迷宫幽灵船八个部件位置在哪
- 原神巡林小队二号林区位置在哪
- 原神巡林小队一号林区位置在哪
- 阴阳锅防空洞的密码有哪些
- 自费研究生的培养方式是统分还是自筹呢 培养方式统分
- 光伏发电的政府补助政策 光伏发电新政策