|
1 | | -# MongoDB Community Operator Architecture |
| 1 | +# MongoDB Community Kubernetes Operator Architecture |
2 | 2 |
|
3 | | -**Work in Progress** |
| 3 | +The MongoDB Community Kubernetes Operator is a [Custom Resource Definition](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) and a [Controller](https://kubernetes.io/docs/concepts/architecture/controller/). |
4 | 4 |
|
5 | | -## Topics |
| 5 | +## Table of Contents |
6 | 6 |
|
7 | | -* Automation Agent |
8 | | -* Cluster Configuration and ConfigMap |
9 | | -* MongoDB Docker Images |
10 | | -* Example case: MongoDB version upgrade |
| 7 | +- [Cluster Configuration](#cluster-configuration) |
| 8 | +- [Example: MongoDB Version Upgrade](#example-mongodb-version-upgrade) |
| 9 | +- [MongoDB Docker Images](#mongodb-docker-images) |
11 | 10 |
|
| 11 | +## Cluster Configuration |
| 12 | + |
| 13 | +You create and update MongoDB resources by defining a MongoDB resource definition. When you apply the MongoDB resource definition to your Kubernetes environment, the Operator: |
| 14 | + |
| 15 | +1. Creates a [StatefulSet](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/) that contains one [pod](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/) for each [replica set](https://docs.mongodb.com/manual/replication/) member. |
| 16 | +1. Creates two [containers](https://kubernetes.io/docs/concepts/containers/overview/) in each pod: |
| 17 | + |
| 18 | + - A container for the [`mongod`](https://docs.mongodb.com/manual/reference/program/mongod/index.html) process binary. </br> |
| 19 | + `mongod` is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations. |
| 20 | + |
| 21 | + - A container for the MongoDB Agent. </br> |
| 22 | + The Automation function of the MongoDB Agent handles configuring, stopping, and restarting the `mongod` process. The MongoDB Agent periodically polls the `mongod` to determine status and can deploy changes as needed. |
| 23 | +1. Writes the Automation configuration as a [ConfigMap](https://kubernetes.io/docs/concepts/configuration/configmap/) and mounts it to each pod. |
| 24 | +1. Initiates the MongoDB Agent, which in turn creates the database configuration and launches the `mongod` process according to your [MongoDB resource definition](deploy/crds/mongodb.com_v1_mongodb_cr.yaml). |
| 25 | + |
| 26 | +<!-- |
| 27 | +<img src="" alt="Architecure diagram of the MongoDB Community Kubernetes Operator"> |
| 28 | +--> |
| 29 | + |
| 30 | +This architecture maximizes use of the MongoDB Agent while integrating naturally with Kubernetes to produce a number of benefits. |
| 31 | + |
| 32 | +- The database container is not tied to the lifecycle of the Agent container or to the Operator, so you can: |
| 33 | + - Use your preferred Linux distribution inside the container. |
| 34 | + - Update operating system packages on your own schedule. |
| 35 | + - Upgrade the Operator or Agent without affecting the database image or uptime of the MongoDB servers. |
| 36 | +- Containers are immutable and have a single responsibility or process, so you can: |
| 37 | + - Describe and understand each container. |
| 38 | + - Configure resources independently for easier debugging and triage. |
| 39 | + - Inspect resources independently, including tailing the logs. |
| 40 | + - Expose the state of each container. |
| 41 | +- Pods are defined as StatefulSets so they benefit from stable identities. |
| 42 | +- You can upgrade the Operator without restarting either the database or the MongoDB Agent containers. |
| 43 | +- You can set up a MongoDB Kubernetes cluster offline once you download the Docker containers for the database and MongoDB Agent. |
| 44 | + |
| 45 | +## Example: MongoDB Version Upgrade |
| 46 | + |
| 47 | +The MongoDB Community Kubernetes Operator uses the Automation function of the MongoDB Agent to efficiently handle rolling upgrades. The Operator configures the StatefulSet to block Kubernetes from performing native rolling upgrades because the native process can trigger multiple re-elections in your MongoDB cluster. |
| 48 | + |
| 49 | +When you update the MongoDB version in your resource definition and reapply it to your Kubernetes environment, the Operator initiates a rolling upgrade: |
| 50 | + |
| 51 | +1. The Operator changes the StatefulSet [update strategy](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#update-strategies) from `RollingUpdate` to `OnDelete`. |
| 52 | + |
| 53 | +1. The Operator updates the [image](https://kubernetes.io/docs/concepts/containers/images/) specification to the new version of MongoDB and writes a new Automation configuration ConfigMap to each pod. |
| 54 | + |
| 55 | +1. The MongoDB Agent chooses the first pod to upgrade and stops the `mongod` process using a local connection and [`db.shutdownServer`](https://docs.mongodb.com/manual/reference/method/db.shutdownServer/#db.shutdownServer). |
| 56 | + |
| 57 | +1. A pre-stop hook on the database container checks the state of the MongoDB Agent. If the MongoDB Agent expects the `mongod` process to start with a new version, the hook uses a Kubernetes API call to delete the pod. |
| 58 | + |
| 59 | +1. The Kubernetes Controller downloads the target version of MongoDB from its default docker registry and restarts the pod with the target version of `mongod` in the database container. |
| 60 | + |
| 61 | +1. The MongoDB Agent starts. It checks the target version of the new `mongod`, then generates the configuration file for the `mongod` process. |
| 62 | + |
| 63 | +1. The `mongod` process receives the configuration file from the MongoDB Agent and starts. |
| 64 | + |
| 65 | +1. The MongoDB Agent reaches goal state. |
| 66 | + |
| 67 | +1. The MongoDB Agent chooses the next pod to upgrade and repeats the process until all pods are upgraded. |
| 68 | + |
| 69 | +1. The Operator changes the StatefulSet update strategy from `OnDelete` back to `RollingUpdate`. |
| 70 | + |
| 71 | +<!-- |
| 72 | +<img src="" alt="Rolling upgrade flow diagram for the MongoDB Community Kubernetes Operator"> |
| 73 | +--> |
| 74 | + |
| 75 | +This upgrade process allows the MongoDB Agent to: |
| 76 | + |
| 77 | +- Perform pre-conditions. |
| 78 | +- Upgrade the secondaries first. |
| 79 | +- Wait for the secondaries' oplogs to catch up before triggering an election. |
| 80 | +- Upgrade quickly for large replica sets. |
| 81 | +- Consider voting nodes. |
| 82 | +- Ensure a replica set is always available throughout the entire upgrade process. |
| 83 | + |
| 84 | +## MongoDB Docker Images |
| 85 | + |
| 86 | +MongoDB images are available on [Docker Hub](https://hub.docker.com/_/mongo?tab=tags&page=1&ordering=last_updated). |
0 commit comments