NodeAffinityとPodAffinityを定義することで、Kubernetes 内で MongoDB リソースの配置トポロジーを構成できます。 NodeAffinityとPodAffinityは、Kubernetes が Kubernetes クラスターに MongoDB カスタム リソースを配置する場所を指定します。
Kubernetesクラスター内の特定のノードにノードラベルを適用し、labelSelector MongoDB CustomResourceDefinition で対応するラベルを持つ フィールドを定義することで、ノードアフィニティを実装できます。ノードラベルを適用し、 値と一致する場合、labelSelector Kubernetes は、 Kubernetesクラスター内で指定されたノードに特定のMongoDB CustomResourceDefinition のみを配置します。ノードアフィニティ ルールを実装すると、特定のノードタイプに特定のリソースを配置できるため、異種ノードグループにMongoDBリソースを配置する際に便利です。
同様に、クラスター内で実行中いるポッドにラベルを適用し、それらのラベル値をMongoDB CustomResourceDefinition で定義された 値と整合させることで、PedlabelSelector アフィニティを実装できます。ラベルを適用し、 labelSelector値と一致すると、 Kubernetes は、一致するラベルが適用されたポッドとMongoDBカスタム リソースが管理するポッドとを照合します。ポッドを照合することで、システムのパフォーマンスが向上し、定期的に通信するポッド間のレイテンシが軽減されます。また、照合してはなりません。
スタンドアロンおよびレプリカセットの配置では、これらのアフィニティspec.podSpec ルールをMongoDB CustomResourceDefinition の に適用できます。シャードクラスタの配置では、これらのアフィニティ ルールをMongoDBspec.configSrvPodSpec CustomResourceDefinitionspec.mongosPodSpec の 、spec.shardPodSpec 、 セクションに適用できます。
You must deploy MongoDB resources, such as mongos, shards, and config servers in the case of a Sharded Cluster deployment, in the same namespace as the MongoDB resource. However, within that namespace, you can configure the nodeAffinity and podAffinity for mongos, shards, and config server resource types in the ShardedCluster CustomResourceDefinition.
前提条件
MongoDB 配置の配置トポロジーを構成するには、MongoDB Kubernetes Operator を通じて配置された MongoDB レプリカセットlabelSelectors と、MongoDB CustomResourceDefinition で定義された と一致する Kubernetes リソースに適用されたラベルが必要です。 または 次の前提条件を満たす必要があります。
MongoDB CustomResourceDefinition で定義された と一致する Kubernetes リソースに適用されるラベル
labelSelectors。
手順
MongoDB CRD マニフェストを更新します。
次の例に示すように、MongoDB シャーディングされたクラスター定義のpodTemplate.spec.affinityセクションに入力します。
1 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: my-sharded-cluster 6 spec: 7 shardCount: 2 8 mongodsPerShardCount: 3 9 mongosCount: 2 10 configServerCount: 3 11 version: 8.0.0 12 service: my-service 13 14 opsManager: 15 configMapRef: 16 name: my-project 17 credentials: my-credentials 18 type: ShardedCluster 19 20 persistent: true 21 configSrvPodSpec: 22 podTemplate: 23 spec: 24 affinity: 25 podAffinity: 26 requiredDuringSchedulingIgnoredDuringExecution: 27 - labelSelector: 28 matchExpressions: 29 - key: security 30 operator: In 31 values: 32 - S1 33 topologyKey: failure-domain.beta.kubernetes.io/zone 34 nodeAffinity: 35 requiredDuringSchedulingIgnoredDuringExecution: 36 nodeSelectorTerms: 37 - matchExpressions: 38 - key: kubernetes.io/e2e-az-name 39 operator: In 40 values: 41 - e2e-az1 42 - e2e-az2 43 podAntiAffinity: 44 requiredDuringSchedulingIgnoredDuringExecution: 45 - podAffinityTerm: 46 topologyKey: nodeId 47 mongosPodSpec: 48 podTemplate: 49 spec: 50 affinity: 51 podAffinity: 52 requiredDuringSchedulingIgnoredDuringExecution: 53 - labelSelector: 54 matchExpressions: 55 - key: security 56 operator: In 57 values: 58 - S1 59 topologyKey: failure-domain.beta.kubernetes.io/zone 60 nodeAffinity: 61 requiredDuringSchedulingIgnoredDuringExecution: 62 nodeSelectorTerms: 63 - matchExpressions: 64 - key: kubernetes.io/e2e-az-name 65 operator: In 66 values: 67 - e2e-az1 68 - e2e-az2 69 podAntiAffinity: 70 requiredDuringSchedulingIgnoredDuringExecution: 71 - podAffinityTerm: 72 topologyKey: nodeId 73 shardPodSpec: 74 podTemplate: 75 spec: 76 affinity: 77 podAffinity: 78 requiredDuringSchedulingIgnoredDuringExecution: 79 - labelSelector: 80 matchExpressions: 81 - key: security 82 operator: In 83 values: 84 - S1 85 topologyKey: failure-domain.beta.kubernetes.io/zone 86 nodeAffinity: 87 requiredDuringSchedulingIgnoredDuringExecution: 88 nodeSelectorTerms: 89 - matchExpressions: 90 - key: kubernetes.io/e2e-az-name 91 operator: In 92 values: 93 - e2e-az1 94 - e2e-az2 95 podAntiAffinity: 96 requiredDuringSchedulingIgnoredDuringExecution: 97 - podAffinityTerm: 98 topologyKey: nodeId 99 ...