4.2.3. 配置 pod 反关联性规则
要指定首选项来防止 pod 与另一个 pod 进行调度,您可以创建一个标签以及使用反关联性偏好规则的 pod。
以下步骤演示了一个简单的双 pod 配置,它创建一个带有某标签的 pod,以及一个使用反关联性偏好规则来尝试阻止随着该 pod 一起调度的 pod。
您不能直接将关联性添加到调度的 pod 中。
流程
创建 pod 规格中具有特定标签的 pod:
使用以下内容创建 YAML 文件:
apiVersion: v1 kind: Pod metadata: name: security-s1 labels: security: S1 spec: securityContext: runAsNonRoot: true seccompProfile: type: RuntimeDefault containers: - name: security-s1 image: docker.io/ocpqe/hello-pod securityContext: allowPrivilegeEscalation: false capabilities: drop: [ALL]创建 pod。
$ oc create -f <pod-spec>.yaml
在创建其他 pod 时,配置以下参数:
使用以下内容创建 YAML 文件:
apiVersion: v1 kind: Pod metadata: name: security-s2-east # ... spec: # ... affinity: podAntiAffinity: preferredDuringSchedulingIgnoredDuringExecution: - weight: 100 podAffinityTerm: labelSelector: matchExpressions: - key: security values: - S1 operator: In topologyKey: kubernetes.io/hostname # ...其中:
spec.affinity.podAffinity- 指定用于配置 pod 关联性的小节。
spec.affinity.podAffinity.preferredDuringSchedulingIgnoredDuringExecution指定 偏好规则 的参数。或者,您可以使用
required DuringSchedulingIgnoredDuringExecution参数配置所需的规则。配置 weight 和以下
podAffinityTerm.labelSelector.matchExpressions参数。如果您希望新 pod 与其他 pod 一起调度,请使用与第一个 pod 上标签相同的key和values参数。weight- 对于偏好规则,为节点指定一个权重,作为数字 1-100。优先选择权重最高的节点。
key- 指定必须匹配的键/值对键(标签),才能应用该规则。
value- 指定必须匹配键/值对(标签)才能应用该规则的值。
operator-
指定现有 pod 上的标签和新 pod 规格中
matchExpression参数的值集合之间的关系。可以是In、NotIn、Exists或DoesNotExist。 topologyKey- 指定系统用来表示此类拓扑域的预填充 Kubernetes 标签。
创建 pod。
$ oc create -f <pod-spec>.yaml