Docs Menu
Docs Home
/
MongoDB Enterprise Kubernetes Operator
/

Configure Cluster Topology for MongoDB Resources

On this page

  • Prerequisites
  • Procedure

You can configure the deployment topology of your MongoDB resources within Kubernetes by defining NodeAffinity and PodAffinity. NodeAffinity and PodAffinity specify where Kubernetes deploys MongoDB custom resources in your Kubernetes cluster.

You can implement node affinity by applying node labels to specific nodes within your Kubernetes cluster and defining labelSelector fields with corrosponding labels in your MongoDB CustomResourceDefinition. When you apply node labels and matching labelSelector values, Kubernetes only deploys a given MongoDB CustomResourceDefinition on the nodes you specify within your Kubernetes cluster. Implementing node affinity rules can be useful when deploying MongoDB resources in heterogeneous node groups, as it allows you to deploy specific resources on specific node varieties.

Similarly, you can implement Pod affinity by applying labels to Pods that are running in your cluster and aligning those label values with labelSelector values defined in your MongoDB CustomResourceDefinition. When you apply labels and matching labelSelector values, Kubernetes colocates MongoDB custom resource-managed Pods with Pods with matching labels applied. By collocating Pods, you can improve system performance and reduce latency between Pods that communicate regularly. You can also define Pod antiaffinity rules which allow you to specify Pods which should not be colocated.

For Standalone and Replica Set deployments, you can apply these affinity rules to the spec.podSpec of your MongoDB CustomResourceDefinition. For Sharded Cluster deployments, you can apply these affinity rules to the spec.configSrvPodSpec, spec.shardPodSpec, and spec.mongosPodSpec sections of your MongoDB CustomResourceDefinition.

You must deploy MonogDB 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.

To configure the deployment topology for your MongoDB deployment, you must either have a MongoDB replica set deployed through the MongoDB Kubernetes Operator along with labels applied to the Kubernetes resources that will align with labelSelectors defined in the MongoDB CustomResourceDefinition or meet the following prerequisites:

1

Populate the podTemplate.affinity sections of your MongoDB Sharded Cluster definition as shown in the following example:

1---
2apiVersion: mongodb.com/v1
3kind: MongoDB
4metadata:
5 name: my-sharded-cluster
6spec:
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 affinity:
24 podAffinity:
25 requiredDuringSchedulingIgnoredDuringExecution:
26 - labelSelector:
27 matchExpressions:
28 - key: security
29 operator: In
30 values:
31 - S1
32 topologyKey: failure-domain.beta.kubernetes.io/zone
33 nodeAffinity:
34 requiredDuringSchedulingIgnoredDuringExecution:
35 nodeSelectorTerms:
36 - matchExpressions:
37 - key: kubernetes.io/e2e-az-name
38 operator: In
39 values:
40 - e2e-az1
41 - e2e-az2
42 podAntiAffinity:
43 requiredDuringSchedulingIgnoredDuringExecution:
44 topologyKey: nodeId
45 mongosPodSpec:
46 podTemplate:
47 affinity:
48 podAffinity:
49 requiredDuringSchedulingIgnoredDuringExecution:
50 - labelSelector:
51 matchExpressions:
52 - key: security
53 operator: In
54 values:
55 - S1
56 topologyKey: failure-domain.beta.kubernetes.io/zone
57 nodeAffinity:
58 requiredDuringSchedulingIgnoredDuringExecution:
59 nodeSelectorTerms:
60 - matchExpressions:
61 - key: kubernetes.io/e2e-az-name
62 operator: In
63 values:
64 - e2e-az1
65 - e2e-az2
66 podAntiAffinity:
67 requiredDuringSchedulingIgnoredDuringExecution:
68 topologyKey: nodeId
69 shardPodSpec:
70 podTemplate:
71 affinity:
72 podAffinity:
73 requiredDuringSchedulingIgnoredDuringExecution:
74 - labelSelector:
75 matchExpressions:
76 - key: security
77 operator: In
78 values:
79 - S1
80 topologyKey: failure-domain.beta.kubernetes.io/zone
81 nodeAffinity:
82 requiredDuringSchedulingIgnoredDuringExecution:
83 nodeSelectorTerms:
84 - matchExpressions:
85 - key: kubernetes.io/e2e-az-name
86 operator: In
87 values:
88 - e2e-az1
89 - e2e-az2
90 podAntiAffinity:
91 requiredDuringSchedulingIgnoredDuringExecution:
92 topologyKey: nodeId
93...
2
kubectl apply -f mongodb-crd.yaml

Back

Configure MongoDB Database Backups

Next

Configure Storage Options for MongoDB Resources