为MongoDB资源配置集群拓扑
您可以在Kubernetes中通过定义来配置您的MongoDB资源的部署拓扑NodeAffinity和来配置您的MongoDB资源的部署拓扑PodAffinity
. NodeAffinity和PodAffinity指定在您的集群中部署MongoDB自定义资源的位置。来配置您的MongoDB资源的部署拓扑您可以通过将应用于节点亲和性来实现节点亲和性,通过将节点标签应用于您的特定节点。来配置您的MongoDB资源的部署拓扑在您的MongoDB 自定义资源定义 中定义具有相应标签的 labelSelector
字段和集群。当您应用 节点标签 以及匹配的 labelSelector
值时,来配置您的MongoDB资源的部署拓扑只在您指定的节点上部署特定的MongoDB 自定义资源定义。来配置您的MongoDB资源的部署拓扑在异构节点组中部署MongoDB资源时,实现节点亲和性规则非常有用,因为它允许您将特定资源部署到特定的节点类型。
同样,您可以通过应用标签来实现Pod亲和性,将标签应用于您集群中运行的Pod,并将这些标签值与MongoDB的labelSelector
值相匹配,这些值定义在您的MongoDB CustomResourceDefinition中。来配置您的MongoDB资源的部署拓扑当您应用标签并匹配labelSelector
值时,MongoDB自定义资源管理的Pod将与具有匹配标签的Pod一起放置。通过放置Pod,您可以提高系统性能并减少经常通信的Pod之间的延迟。您还可以定义Pod反亲和性规则,允许您指定不应放置在一起的Pod。
对于独立和副本集部署,您可以将这些亲和性规则应用到spec.podSpec
是您 MongoDB 的 自定义资源定义的一部分。对于分片集群部署,您可以将这些亲和规则应用于 MongoDB 的 spec.configSrvPodSpec
、spec.shardPodSpec
和 spec.mongosPodSpec
部分 自定义资源定义。
在分片集群部署的情况下,您必须将MonogDB资源,如 mongos
、分片和配置服务器,部署在与MongoDB资源相同的命名空间中。然而,在同一个命名空间内,您可以配置 nodeAffinity
和 podAffinity
,用于 mongos
、分片和配置服务器资源类型在 ShardedCluster
自定义资源定义。
先决条件
为了配置MongoDB部署的部署拓扑,您必须通过MongoDB Kubernetes Operator部署一个MongoDB副本集,并对Kubernetes Operator应用标签。来配置您的MongoDB资源的部署拓扑将与在MongoDB 自定义资源定义中定义的 labelSelectors
对齐的资源。
应用于以下资源的标签来配置您的MongoDB资源的部署拓扑将与在MongoDB 自定义资源定义中定义的
labelSelectors
对齐的资源。
步骤
更新你的 MongoDB CRD 清单。
如以下示例所示,在您的 MongoDB 分片集群定义中填充 podTemplate.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: 6.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 ...