configuration/aws-automation/launch-mongodb-replica.sh
... ...
@@ -1,9 +1,16 @@
1 1
#!/bin/bash
2 2
# Usage:
3
-# launch-mongodb-replica.sh <replica-set-name> <primary-host:primary-port> <priority>
3
+# launch-mongodb-replica.sh [ <replica-set-name> [ <primary-host:primary-port> [ <priority> ] ] ]
4 4
#
5
-# Example:
6
-# launch-mongodb-replica.sh live dbserver.internal.sapsailing.com:10202 1
5
+# The defaults are: live, mongo0.internal.sapsailing.com:27017, 1
6
+#
7
+# Examples:
8
+# launch-mongodb-replica.sh
9
+# connects to the "live" replica set through mongo0.internal.sapsailing.com:27017 with priority 1
10
+# launch-mongodb-replica.sh archive dbserver.internal.sapsailing.com:10201 0
11
+# connects to the "archive" replica set at dbserver.internal.sapsailing.com:10201 and ensures
12
+# that the new replica never becomes PRIMARY by setting the priority to 0
7 13
#
8 14
aws ec2 run-instances --placement AvailabilityZone=eu-west-1c --instance-type i2.xlarge --security-group-ids sg-0a9bc2fb61f10a342 --image-id ami-0e7e1e80586782638 --count 1 --user-data "REPLICA_SET_NAME=$1
9
-REPLICA_SET_PRIMARY=$2" --ebs-optimized --key-name Axel
15
+REPLICA_SET_PRIMARY=$2
16
+REPLICA_SET_PRIORITY=$3" --ebs-optimized --key-name Axel
configuration/mongo_instance_setup/add-as-replica
... ...
@@ -9,7 +9,7 @@ if [ -z "$REPLICA_SET_PRIMARY" ]; then
9 9
fi
10 10
REPLICA_SET_PRIORITY=$(ec2-metadata | grep REPLICA_SET_PRIORITY | sed -e 's/^user-data: //' | sed -e 's/^REPLICA_SET_PRIORITY=//')
11 11
if [ -z "$REPLICA_SET_PRIORITY" ]; then
12
- REPLICA_SET_PRIORITY=0
12
+ REPLICA_SET_PRIORITY=1
13 13
fi
14 14
if [ \! -z "REPLICA_SET_PRIMARY" ]; then
15 15
IP=$(ec2-metadata -o | sed -e 's/^local-ipv4: //')
wiki/info/landscape/creating-ec2-mongodb-image-from-scratch.md
... ...
@@ -88,22 +88,32 @@ 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. 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
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``. Copy the ``*.service`` files to ``/etc/systemd/system`` and the remaining scripts to ``/usr/local/bin``.
92 92
93
-Then execute ``systemctl enable mongod.service`` to launch the MongoDB process and ensure it is always launched when the instance (re-)boots.
93
+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 it start automatically when the system comes up. It depends on ``mongod.service``, hence waits until the MongoDB process has started successfully. It then registers as a replica in a replica set. The following user data variables can be used to configure the behavior:
94
+- ``REPLICA_SET_NAME``: the replica set name to which to subscribe; this name will also be patched into the ``/etc/mongod.conf`` config file; defaults to ``live``
95
+- ``REPLICA_SET_PRIMARY``: the hostname and port, separated by a colon ``:``, of the replica set's primary to which to connect; defaults to ``mongo0.internal.sapsailing.com:27017``
96
+- ``REPLICA_SET_PRIORITY``: the priority with which the new replica shall become a master; defaults to ``1`` which lets the new replica become master. Set it to ``0`` to avoid ever promoting the new replica to a primary.
94 97
95
-## MongoDB Replica Set Configuration
98
+Then execute ``systemctl enable mongod.service`` and ``systemctl enable mongo-replica-set.service`` to launch the MongoDB process and ensure it is always launched when the instance (re-)boots and becomes a replica in a replica set according to the user data and the defaults described above.
96 99
97
-Connect to the MongoDB on that instance, then issue the command ``rs.initiate()`` in order to turn the instance into the "seed" of a replica set. You can then, for the time being, ``quit()`` the mongo shell. Re-connecting, e.g., with ``mongo "mongodb://localhost:27017/?replicaSet=live"``, will show the ``PRIMARY`` of the new replica set.
98
-
99
-Connected to the PRIMARY using the ``mongo`` shell, a replica can be added using ``rs.add({host: "hostname:port"})``. For additional options see [here](https://docs.mongodb.com/manual/reference/method/rs.add/). To add a hidden replica on ``dbserver.internal.sapsailing.com:10203``, use ``rs.add({host: "dbserver.internal.sapsailing.com:10203", buildIndexes: false, hidden: true, priority: 0})``.
100
+## Manual MongoDB Replica Set Configuration
100 101
102
+In case you decide to skip the benefits of ``mongo-replica-set.service``, you can also establish the connection with the replica set as follows. Connect to the MongoDB on the instance. If it is the first instance of a replica set, issue the command ``rs.initiate()`` in order to turn the instance into the "seed" of a replica set. You can then, for the time being, ``quit()`` the mongo shell. Re-connecting, e.g., with ``mongo "mongodb://localhost:27017/?replicaSet=live"``, will show the ``PRIMARY`` of the new replica set. Don't issue these commands if you only want to add another replica to an existing replica set.
101 103
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.
104
+Connected to the PRIMARY using the ``mongo`` shell, a replica can be added using ``rs.add({host: "hostname:port"})``. For additional options see [here](https://docs.mongodb.com/manual/reference/method/rs.add/). To add a hidden replica on ``dbserver.internal.sapsailing.com:10203``, use ``rs.add({host: "dbserver.internal.sapsailing.com:10203", buildIndexes: false, hidden: true, priority: 0})``.
103 105
104 106
## Launch Script for a MongoDB Replica
105 107
108
+Under ``configuration/aws-automation/launch-mongodb-replica.sh`` there is a script that can be used for quickly launching a MongoDB replica. Usage:```
109
+ launch-mongodb-replica.sh [ &lt;replica-set-name&gt; [ &lt;primary-host:primary-port&gt; [ &lt;priority&gt; ] ] ]
110
+```
111
+The defaults for the parameters are, respectively: ``live``, ``mongo0.internal.sapsailing.com:27017``, and ``1``.
112
+
113
+This can, e.g., be used to fire up a replica for the ``winddb`` instance on ``dbserver.internal.sapsailing.com:10201`` which is configured to run on a slow, inexpensive but large disk and on a host that has only little RAM configured because the only load it has to serve may be occasional master data import activity and burst-loads during archive server re-starts. To handle the latter, an instance with more RAM and fast disk would be preferable because otherwise indices won't fit into RAM, letting MongoDB read indices from the slow disk. This lets the archive server take many days to load.
106 114
115
+Instead, the ``launch-mongodb-replica.sh`` script can be used to fire up a replica with a very fast NMVe SSD attached to the instance. A good instance type for this may be ``i2.xlarge`` which has 30GB of RAM, 4 vCPUs and 800GB of NVMe storage. Launch like this:
107 116
108
-55min initial replication for archive server winddb with dbserver.internal.sapsailing.com:10201/winddb using a gp2 SSD; additional 20min to build index
117
+``configuration/aws-automation/launch-mongodb-replica.sh archive dbserver.internal.sapsailing.com:10201 0``
109 118
119
+You can then check the replication status by connecting your ``mongo`` client to ``dbserver.internal.sapsailing.com:10201`` and check the output of ``rs.status()``. As long as the new instance is listed in status ``STARTUP2`` it is still synchronizing from the primary. Eventually (took 55min during the last test) it transitions to status ``SECONDARY``. You can then use a connection string such as ``mongodb://dbserver.internal.sapsailing.com:10201/winddb?replicaSet=archive&retryWrites=true&readPreference=secondary`` for the ``MONGODB_URI`` in your archive server Java instance which will then preferably connect to your new fast secondary replica. Consider that the new secondary replica will have to build indices when first queried. This can take another 20 minutes. When done loading the archive server, terminate the instance which will properly remove the replica from the replica set.
... ...
\ No newline at end of file