4.2.3. 配置 pod 反关联性规则


要指定首选项来防止 pod 与另一个 pod 进行调度,您可以创建一个标签以及使用反关联性偏好规则的 pod。

以下步骤演示了一个简单的双 pod 配置,它创建一个带有某标签的 pod,以及一个使用反关联性偏好规则来尝试阻止随着该 pod 一起调度的 pod。

注意

您不能直接将关联性添加到调度的 pod 中。

流程

  1. 创建 pod 规格中具有特定标签的 pod:

    1. 使用以下内容创建 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]
    2. 创建 pod。

      $ oc create -f <pod-spec>.yaml
  2. 在创建其他 pod 时,配置以下参数:

    1. 使用以下内容创建 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 上标签相同的 keyvalues 参数。

      weight
      对于偏好规则,为节点指定一个权重,作为数字 1-100。优先选择权重最高的节点。
      key
      指定必须匹配的键/值对键(标签),才能应用该规则。
      value
      指定必须匹配键/值对(标签)才能应用该规则的值。
      operator
      指定现有 pod 上的标签和新 pod 规格中 matchExpression 参数的值集合之间的关系。可以是 InNotInExistsDoesNotExist
      topologyKey
      指定系统用来表示此类拓扑域的预填充 Kubernetes 标签。
    2. 创建 pod。

      $ oc create -f <pod-spec>.yaml
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

关于红帽文档

Legal Notice

Theme

© 2026 Red Hat
返回顶部