forked from jasonyihk/kube-mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.db.sh
58 lines (49 loc) · 1.51 KB
/
init.db.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
set -ex
PRIMARY=mongo-0
CMD_MONGO_NOAUTH="kubectl --context kube-aws-k8s-uat-context -n database exec -i $PRIMARY -- mongo --quiet"
CMD_MONGO_AUTH="kubectl --context kube-aws-k8s-uat-context -n database exec -i $PRIMARY -- mongo -u admin -p password --authenticationDatabase admin --quiet"
#Initiate replica set
INIT_RS(){
echo "initizate replica set"
$CMD_MONGO_NOAUTH << EOF
rs.initiate({
_id: 'rs0',
version: 1,
members: [
{ _id: 0, host : 'mongo-0.mongod.database.svc.cluster.local:27017' },
{ _id: 1, host : 'mongo-1.mongod.database.svc.cluster.local:27017' },
{ _id: 2, host : 'mongo-2.mongod.database.svc.cluster.local:27017' }
]
});
EOF
}
#Add a member to the replica set
#e.g. ADD_USER
ADD_USER(){
$CMD_MONGO_NOAUTH << EOF
db.getSiblingDB("admin").createUser({
user : "admin",
pwd : "password",
roles: [ { role: "root", db: "admin" } ]
});
EOF
}
#Add a member to the replica set
#e.g. ADD_NODE mongo-1.mongod.database.svc.cluster.local
ADD_NODE(){
$CMD_MONGO_AUTH << EOF
rs.add({host: "$1:27017"});
EOF
}
#Re-configure replica set
RECONFIG_RS(){
echo "reconfig replica set"
$CMD_MONGO_NOAUTH << EOF
var cfg = rs.conf();
cfg.members[0].host='mongo-0.mongod.database.svc.cluster.local:27017';
cfg.members[1].host='mongo-1.mongod.database.svc.cluster.local:27017';
cfg.members[2].host='mongo-2.mongod.database.svc.cluster.local:27017';
rs.reconfig(cfg, {force : true});
EOF
}