Browse Source

create docker-compose mongo replica

master
zeus 4 years ago
parent
commit
69449479d0
  1. 15
      .docker/mongodb/file.key
  2. 9
      .docker/mongodb/initdb.d/usercreate.sh
  3. 44
      .docker/mongodb/mongod.conf
  4. 15
      .docker/mongodb/sample.file.key
  5. 40
      .docker/mongodb/scripts/mongosetup.sh
  6. 8
      .env
  7. 10
      connect.mongo.sh
  8. 116
      docker-compose.yml
  9. 8
      sample.env

15
.docker/mongodb/file.key

@ -0,0 +1,15 @@
wRbjCxlrzEi3VzuR7Xe0/qMkxqRoBDmeUWfraYteInJEKisn7sVB01QtH3CfKTdn
ND8gKg+xzwwK4f7lkLzp9SyQqLWv/IZfBnUR9WZnlb64p9r9/uJpTJo0FUVB/vDD
kAs+bVcgFA1BsdoLMQW7AVycNo0uoLyd2nkM51J1XXqdMI+WAB/Dd3jVL9sY3NfJ
LGn9rNxmgnpUiKRAgob4w+D/V62NJr5q7+EOfR3/5xvkg1Wfn1p+sIJ8NFzZIKMg
MQux+e/F+J4IvXTc8+MdS5tCcZKOu1NVIR5i9i08utgqBKJmzTF5ebfCqoSYOppq
xpoHlzk9yLWus10szBImX4hDtoPyYEHztLW8ol6xsLv9TzNrCG82rP64bZub+hvO
GeyQwoRx52Nf5jWBvoMAOEjYRV4ZE3ghvA8quXAoh6kK9kKyQW5IbhGri87AadKs
++x2HEZHPUYaMnN16DbYY2LHLSyd5MGqbJxHXMBT3JSFS0jLYUjlRHCeHvTta8ud
WnKNdSg/NH7MSUQiWwLP3PVEsqFaP+d0BXzM8JNrawiBgUZZ2yCF4l9KbOib1G+1
1Xkp0tjXS0K6s31rhZQJvEhTa3+dhH1D8IWRjcQ2axs3DllQ8bqa0hKtI5pndeLT
LK67d3bOyfkB/5UiLnbYgAO6b2CeSvrVpLJf6bH4MPbSTNf8lYXVxPt6p2qOWK9S
Oqb2ahu02TQmWTwLrqRX8Z9EChEbhggnL7I+TxOw3XcF3t3kAH/SPevU8ugNt00e
MIV15q5apokMA35b+z6VhzvcbQI2ypfKnqqdokkr91FlgS/bzWgMudf6JibqcWXA
tbvJjhod7aic8aNwwUMbWC2Mp9f2roWi2SAfQhKzxawFDWCw66XAAlD3LQKr0F+Y
1tAixQZFDDUhB3CRhyY6FAO8rNOCXvA6v16KvA==

9
.docker/mongodb/initdb.d/usercreate.sh

@ -0,0 +1,9 @@
set -Eeuo pipefail
mongo -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin "$MONGO_INITDB_DATABASE" <<EOF
db.createUser({
user: '$MONGO_INITDB_USERNAME',
pwd: '$MONGO_INITDB_PASSWORD',
roles: [ { role: 'readWrite', db: '$MONGO_INITDB_DATABASE' } ]
})
EOF

44
.docker/mongodb/mongod.conf

@ -0,0 +1,44 @@
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
# net:
# port: 27017
# bindIp: 127.0.0.1
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:

15
.docker/mongodb/sample.file.key

@ -0,0 +1,15 @@
jr1tmf/cJ0Hd+cJd58uO23ltrAu+wk8ht2E9yN/dPK2aPzE+7DnzzH0QxR7P2Vbn
zEn9vvKoUkWis6HScQSwJlfaG+PsdNq8jutEjLXL8jvDqfebxybw0BEWfIx5If2A
rKk0IEPXzdVeQ+1CVEDe8NZ88W9T9ROj0K6Pr6uqFAO7LJP3ECd+Ao2EkUezXAED
17+ccDFOKeF4NYh9j2IAoADzD6v8OYHGYoYmZN2h/jHj/PglVjdwAAiPaOeQQvqP
zaPIakgV9H42mk/tTEvZLdr5/0/9tTwbub+Aas05Vfk5udxdWgoVB8KOCFReEOU/
kruPBd/A9fFRo60ZH05fo9d/DHT67wqed323wdsdwJDu/204Td0jCeKvYQqcfSVd
WS4FIFkMqy/N90ARAnFsuCF8ZaUJUj7nA6xjzh4Hu6+163WRfxQ/4n8Et9QT8rFZ
x2MZZG01kzjZGJV1oHLuOB+zZCzAN+pNZn5+5xYUeLgCl3tw1v0KMeJOhgYpwoNA
zLmA2Fl34jkbGjjjwhjdhyk72RQeUx0m2dMSRv/p1x7gzPhqPvPbzNYn4K9tGjdS
yZqOqDcVEvVlRM+JH2U6yxPPV9qq10dxJKa+uSblPRJ0wxf/LVdYKtTCrFC8ZYce
LOpfJpAp/qwlkd13232r3radqwdw2d23e3sdqwdwR6leyHqMs8xJ9xNLXgrFjxhL
PXvv3NZCZtRDgTPxaDFPXQV2tb8bak3qA91hjzFT7PsC5wuzebWJnYGqcHIo5hpe
JCaBkb309QOx0HLt7vYNX4zdfstw+VaMcxooJAxc+VvxePSPRLDnN99SV92EpHMg
UH4YL8fGXhpnGzJ63VSYHS7D2RwZfXpP80vA0JBejSu6g1N1tjK2Ej09PM/g6k+5
MR5elZtdX3GG480/4In8PUPpkzXE60BblEzyTQ==

40
.docker/mongodb/scripts/mongosetup.sh

@ -0,0 +1,40 @@
#!/bin/bash
MONGODB1=mongo1
MONGODB2=mongo2
MONGODB3=mongo3
echo "**********************************************" ${MONGODB1}
echo "Waiting for startup.."
sleep 30
echo "done"
echo SETUP.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:27017 -u ${MONGO_INITDB_ROOT_USERNAME} -p ${MONGO_INITDB_ROOT_PASSWORD} <<EOF
var cfg = {
"_id": "rs0",
"protocolVersion": 1,
"version": 1,
"members": [
{
"_id": 0,
"host": "${MONGODB1}:27017",
"priority": 2
},
{
"_id": 1,
"host": "${MONGODB2}:27017",
"priority": 0
},
{
"_id": 2,
"host": "${MONGODB3}:27017",
"priority": 0,
}
]
};
rs.initiate(cfg, { force: true });
rs.secondaryOk();
db.getMongo().setReadPref('primary');
rs.status();
EOF

8
.env

@ -0,0 +1,8 @@
# MongoDB
MONGO_URL=mongodb://mongodb:27017
MONGO_INITDB_ROOT_USERNAME=swarmlab
MONGO_INITDB_ROOT_PASSWORD=swarmlab
MONGO_INITDB_DATABASE=app_swarmlab
MONGO_INITDB_USERNAME=app_swarmlab
MONGO_INITDB_PASSWORD=app_swarmlab
MONGO_REPLICA_SET_NAME=rs0

10
connect.mongo.sh

@ -0,0 +1,10 @@
MONGO_INITDB_ROOT_USERNAME=swarmlab
MONGO_INITDB_ROOT_PASSWORD=swarmlab
MONGO_INITDB_DATABASE=app_swarmlab
MONGO_INITDB_USERNAME=app_swarmlab
MONGO_INITDB_PASSWORD=app_swarmlab
MONGO_REPLICA_SET_NAME=rs0
mongo "mongodb://localhost:30001,localhost:30002,localhost:30003/$MONGO_INITDB_DATABASE" -u $MONGO_INITDB_USERNAME
mongo "mongodb://localhost:30001,localhost:30002,localhost:30003/app_swarmlab" -u app_swarmlab

116
docker-compose.yml

@ -0,0 +1,116 @@
version: '3.8'
services:
# setup MongoDB cluster for production
mongo-replica-setup:
container_name: mongo-setup
image: 'mongo:4.2'
restart: on-failure
networks:
- netSwarmlabMongo
volumes:
- ./.docker/mongodb/scripts/mongosetup.sh:/scripts/mongosetup.sh
# entrypoint: ["bash"]
entrypoint: ["bash", "/scripts/mongosetup.sh" ]
env_file:
- .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
depends_on:
- mongo1
- mongo2
- mongo3
mongo1:
hostname: 'mongo1'
container_name: 'mongo1'
image: 'mongo:4.2'
restart: 'on-failure'
command: ["-f", "/etc/mongod.conf", "--keyFile", "/auth/file.key", "--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
expose:
- 27017
ports:
- 30001:27017
networks:
- netSwarmlabMongo
volumes:
- mongoData1:/data/db
- mongoLog1:/var/log/mongodb
- ./.docker/mongodb/initdb.d/:/docker-entrypoint-initdb.d/
- ./.docker/mongodb/mongod.conf:/etc/mongod.conf
- ./.docker/mongodb/file.key:/auth/file.key
healthcheck:
test: test $$(echo "rs.status().ok" | mongo -u $${MONGO_INITDB_ROOT_USERNAME} -p $${MONGO_INITDB_ROOT_PASSWORD} --quiet) -eq 1
interval: 30s
start_period: 60s
env_file:
- .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
mongo2:
hostname: 'mongo2'
container_name: 'mongo2'
image: 'mongo:4.2'
command: ["-f", "/etc/mongod.conf", "--keyFile", "/auth/file.key", "--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
restart: 'on-failure'
expose:
- 27017
ports:
- 30002:27017
networks:
- netSwarmlabMongo
volumes:
- mongoData2:/data/db
- mongoLog2:/var/log/mongodb
- ./.docker/mongodb/mongod.conf:/etc/mongod.conf
- ./.docker/mongodb/file.key:/auth/file.key
env_file:
- .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
depends_on:
- mongo1
mongo3:
hostname: 'mongo3'
container_name: 'mongo3'
image: 'mongo:4.2'
command: ["-f", "/etc/mongod.conf", "--keyFile", "/auth/file.key", "--replSet", "${MONGO_REPLICA_SET_NAME}", "--bind_ip_all"]
restart: 'on-failure'
expose:
- 27017
ports:
- 30003:27017
networks:
- netSwarmlabMongo
volumes:
- mongoData3:/data/db
- mongoLog3:/var/log/mongodb
- ./.docker/mongodb/mongod.conf:/etc/mongod.conf
- ./.docker/mongodb/file.key:/auth/file.key
env_file:
- .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
depends_on:
- mongo1
volumes:
mongoData1:
mongoData2:
mongoData3:
mongoLog1:
mongoLog2:
mongoLog3:
networks:
netSwarmlabMongo:

8
sample.env

@ -0,0 +1,8 @@
# MongoDB
MONGO_URL=mongodb://mongodb:27017
MONGO_INITDB_ROOT_USERNAME=<root_username>
MONGO_INITDB_ROOT_PASSWORD=<root_password>
MONGO_INITDB_DATABASE=<app_db-name>
MONGO_INITDB_USERNAME=<app_username>
MONGO_INITDB_PASSWORD=<app_password>
MONGO_REPLICA_SET_NAME=rs0
Loading…
Cancel
Save