3d3984a11ff0fec20dce7f3f42d11e03b20ca35a
configuration/mongo_instance_setup/add-as-replica
| ... | ... | @@ -1,7 +1,11 @@ |
| 1 | 1 | #!/bin/bash |
| 2 | 2 | REPLICA_SET_NAME=$(ec2-metadata | grep REPLICA_SET_NAME | sed -e 's/^user-data: //' | sed -e 's/^REPLICA_SET_NAME=//') |
| 3 | 3 | REPLICA_SET_PRIMARY=$(ec2-metadata | grep REPLICA_SET_PRIMARY | sed -e 's/^user-data: //' | sed -e 's/^REPLICA_SET_PRIMARY=//') |
| 4 | +REPLICA_SET_PRIORITY=$(ec2-metadata | grep REPLICA_SET_PRIORITY | sed -e 's/^user-data: //' | sed -e 's/^REPLICA_SET_PRIORITY=//') |
|
| 5 | +if [ -z "$REPLICA_SET_PRIORITY" ]; then |
|
| 6 | + REPLICA_SET_PRIORITY=0 |
|
| 7 | +fi |
|
| 4 | 8 | if [ \! -z "REPLICA_SET_PRIMARY" ]; then |
| 5 | 9 | IP=$(ec2-metadata -o | sed -e 's/^local-ipv4: //') |
| 6 | - echo "rs.add({host: \"$IP:27017\", priority: 0})" | mongo "mongodb://$REPLICA_SET_PRIMARY/?replicaSet=$REPLICA_SET_NAME&retryWrites=true" |
|
| 10 | + echo "rs.add({host: \"$IP:27017\", priority: $REPLICA_SET_PRIORITY})" | mongo "mongodb://$REPLICA_SET_PRIMARY/?replicaSet=$REPLICA_SET_NAME&retryWrites=true" |
|
| 7 | 11 | fi |
wiki/info/landscape/creating-ec2-mongodb-image-from-scratch.md
| ... | ... | @@ -34,7 +34,7 @@ and instead add |
| 34 | 34 | bindIp: 0.0.0.0 |
| 35 | 35 | ``` |
| 36 | 36 | |
| 37 | -so that MongoDB listens on all interfaces, not only localhost. Furthermore, set the ``directoryPerDB`` property to ``true`` and provide a replication set name using the ``replication.replSetName`` property. |
|
| 37 | +so that MongoDB listens on all interfaces, not only localhost. Furthermore, set the ``directoryPerDB`` property to ``true`` and provide a replication set name using the ``replication.replSetName`` property. The MongoDB default would be ``rs0``. In our landscape we're using ``archive`` for the ``winddb`` database on ``dbserver.internal.sapsailing.com:10201`` and ``live`` for all live events with a hidden replica at ``dbserver.internal.sapsailing.com:10203``. |
|
| 38 | 38 | |
| 39 | 39 | Here is the full ``/etc/mongod.conf`` file: |
| 40 | 40 | |
| ... | ... | @@ -88,7 +88,7 @@ replication: |
| 88 | 88 | #snmp: |
| 89 | 89 | ``` |
| 90 | 90 | |
| 91 | -To deal with the set-up of the ephemeral volume, there are a number of ``systemd`` units that can be found in the git repository at ``configuration/mongo_instance_setup``. The ``ephemeralvolume.service`` ensures that the volume exists and is formatted with the XFS file system. This service uses the ``/usr/local/bin/ephemeralvolume`` script to do so. After that, the volume is mounted to ``/var/lib/mongo`` using the ``var-lib-mongo.mount`` unit. The ownerships are adjusted after that by the ``chownvarlibmongo.service`` unit to the ``mongod`` user. This is then also the last unit required to run before the ``mongod.service``. |
|
| 91 | +To deal with the set-up of the ephemeral volume, there are a number of ``systemd`` units that can be found in the git repository at ``configuration/mongo_instance_setup``. The ``ephemeralvolume.service`` ensures that the volume exists and is formatted with the XFS file system. It looks for the volume at ``/dev/nvme0n1`` and on ``/dev/xvdb``, ensures XFS formatting and then mounts the partition to ``/var/lib/mongo``. This service uses the ``/usr/local/bin/ephemeralvolume`` script to do so. The ownerships are adjusted after that by the ``chownvarlibmongo.service`` unit to the ``mongod`` user. This is then also the last unit required to run before the ``mongod.service`` can start. After that, the replica set hook-up needs to take place. For that, the unit ``mongo-replica-set.service`` is provided, together with the two scripts ``/usr/local/bin/add-as-replica`` and ``/usr/local/bin/remove-as-replica``. The service unit is configured so that it is "wanted" by ``multi-user.target``, making |
|
| 92 | 92 | |
| 93 | 93 | Then execute ``systemctl enable mongod.service`` to launch the MongoDB process and ensure it is always launched when the instance (re-)boots. |
| 94 | 94 | |
| ... | ... | @@ -100,3 +100,10 @@ Connected to the PRIMARY using the ``mongo`` shell, a replica can be added using |
| 100 | 100 | |
| 101 | 101 | |
| 102 | 102 | TODO: automate the initialization and replica set extension using "Addition Details" in the instance; create a MongoDB script that is executed during start-up; if no replica set exists and no user detail specifies where the primary is, run ``rs.initiate()``. If a replica set already exists, leave things unchanged. If no replica set exists and in a user detail something like ``REPLICA_SET_NAME=...`` and ``REPLICA_SET_PRIMARY=...`` is provided, add the local node as a secondary to the primary / replica set specified. |
| 103 | + |
|
| 104 | +## Launch Script for a MongoDB Replica |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + |
|
| 108 | +55min initial replication for archive server winddb with dbserver.internal.sapsailing.com:10201/winddb using a gp2 SSD; additional 20min to build index |
|
| 109 | + |