From 4850fa92d8fbd5ad4c406cd3fe0eaca8f3097696 Mon Sep 17 00:00:00 2001 From: Mahmoud Saada Date: Sat, 6 Oct 2018 14:24:54 -0400 Subject: [PATCH] mongodb: add support for init standalone maintenance scripts (#7250) mongodb: update README remove autolinting issues remove goodbye log Signed-off-by: Mahmoud Saada --- stable/mongodb-replicaset/Chart.yaml | 2 +- stable/mongodb-replicaset/README.md | 20 +++++++++++++++ stable/mongodb-replicaset/init/on-start.sh | 25 ++++++++++++++++--- .../templates/mongodb-init-configmap.yaml | 5 ++++ stable/mongodb-replicaset/values.yaml | 8 ++++++ 5 files changed, 56 insertions(+), 4 deletions(-) diff --git a/stable/mongodb-replicaset/Chart.yaml b/stable/mongodb-replicaset/Chart.yaml index f2bde2c1ee..d68afd46ac 100644 --- a/stable/mongodb-replicaset/Chart.yaml +++ b/stable/mongodb-replicaset/Chart.yaml @@ -1,6 +1,6 @@ name: mongodb-replicaset home: https://github.com/mongodb/mongo -version: 3.5.7 +version: 3.6.0 appVersion: 3.6 description: NoSQL document-oriented database that stores JSON-like documents with dynamic schemas, simplifying the integration of data in content-driven applications. diff --git a/stable/mongodb-replicaset/README.md b/stable/mongodb-replicaset/README.md index a4efec9d15..2bee909540 100644 --- a/stable/mongodb-replicaset/README.md +++ b/stable/mongodb-replicaset/README.md @@ -73,6 +73,7 @@ The following table lists the configurable parameters of the mongodb chart and t | `auth.existingAdminSecret` | If set, and existing secret with this name is used for the admin user | `` | | `serviceAnnotations` | Annotations to be added to the service | `{}` | | `configmap` | Content of the MongoDB config file | `` | +| `initMongodStandalone` | If set, initContainer executes script in standalone mode | `` | | `nodeSelector` | Node labels for pod assignment | `{}` | | `affinity` | Node/pod affinities | `{}` | | `tolerations` | List of node taints to tolerate | `[]` | @@ -355,3 +356,22 @@ connecting to: mongodb://127.0.0.1:27017 ### Scaling Scaling should be managed by `helm upgrade`, which is the recommended way. + +### Indexes and Maintenance + +You can run Mongo in standalone mode and execute Javascript code on each replica at initContainer time using `initMongodStandalone`. +This allows you to create indexes on replicasets following [best practices](https://docs.mongodb.com/manual/tutorial/build-indexes-on-replica-sets/). + +#### Example: Creating Indexes + +```js +initMongodStandalone: |+ + db = db.getSiblingDB("mydb") + db.my_users.createIndex({email: 1}) +``` + +Tail the logs to debug running indexes or to follow their progress + +```sh +kubectl exec -it $RELEASE-mongodb-replicaset-0 -c bootstrap -- tail -f /work-dir/log.txt +``` diff --git a/stable/mongodb-replicaset/init/on-start.sh b/stable/mongodb-replicaset/init/on-start.sh index 87a4412b4e..fc1dae1fb9 100644 --- a/stable/mongodb-replicaset/init/on-start.sh +++ b/stable/mongodb-replicaset/init/on-start.sh @@ -45,6 +45,20 @@ shutdown_mongo() { mongo admin "${admin_creds[@]}" "${ssl_args[@]}" --eval "db.shutdownServer({$args})" } +init_mongod_standalone() { + log "Starting a MongoDB instance as standalone..." + mongod --config /data/configdb/mongod.conf --dbpath=/data/db "${auth_args[@]}" --bind_ip=0.0.0.0 >> /work-dir/log.txt 2>&1 & + log "Waiting for MongoDB to be ready..." + until mongo "${ssl_args[@]}" --eval "db.adminCommand('ping')"; do + log "Retrying..." + sleep 2 + done + log "Initialized." + log "Running init js script on standalone mongod..." + mongo admin "${admin_creds[@]}" "${ssl_args[@]}" /init/initMongodStandalone.js + shutdown_mongo +} + my_hostname=$(hostname) log "Bootstrapping MongoDB replica set member: $my_hostname" @@ -97,10 +111,16 @@ EOL rm mongo.key mongo.crt fi +if [ -f /init/initMongodStandalone.js ] +then + init_mongod_standalone +else + log "Skipping init mongod standalone script" +fi log "Peers: ${peers[*]}" -log "Starting a MongoDB instance..." +log "Starting a MongoDB instance as replica..." mongod --config /data/configdb/mongod.conf --dbpath=/data/db --replSet="$replica_set" --port=27017 "${auth_args[@]}" --bind_ip=0.0.0.0 >> /work-dir/log.txt 2>&1 & log "Waiting for MongoDB to be ready..." @@ -174,5 +194,4 @@ if mongo "${ssl_args[@]}" --eval "rs.status()" | grep "no replset config has bee log "Done." fi -shutdown_mongo -log "Good bye." +shutdown_mongo \ No newline at end of file diff --git a/stable/mongodb-replicaset/templates/mongodb-init-configmap.yaml b/stable/mongodb-replicaset/templates/mongodb-init-configmap.yaml index 53185c44ff..8f5ba0db5e 100644 --- a/stable/mongodb-replicaset/templates/mongodb-init-configmap.yaml +++ b/stable/mongodb-replicaset/templates/mongodb-init-configmap.yaml @@ -13,3 +13,8 @@ metadata: data: on-start.sh: | {{ .Files.Get "init/on-start.sh" | indent 4 }} +{{- if .Values.initMongodStandalone }} + initMongodStandalone.js: | +{{ .Values.initMongodStandalone | indent 4 }} +{{- end }} + diff --git a/stable/mongodb-replicaset/values.yaml b/stable/mongodb-replicaset/values.yaml index 9ea9bc0dc1..de7b0f78ce 100644 --- a/stable/mongodb-replicaset/values.yaml +++ b/stable/mongodb-replicaset/values.yaml @@ -107,6 +107,14 @@ tls: # Entries for the MongoDB config file configmap: +# Javascript code to execute on each replica at initContainer time +# This is the recommended way to create indexes on replicasets. +# Below is an example that creates indexes in foreground on each replica in standalone mode. +# ref: https://docs.mongodb.com/manual/tutorial/build-indexes-on-replica-sets/ +# initMongodStandalone: |+ +# db = db.getSiblingDB("mydb") +# db.my_users.createIndex({email: 1}) + # Readiness probe readinessProbe: initialDelaySeconds: 5