1. hosts에 container명 추가
Replica Set 구성 할떄 사용
$ sudo vi /etc/hosts
2. docker-compose-replicaset.yml(인증없이 생성)
version: '3.3'
services:
mongo1:
container_name: mongo1
image: mongo:latest
volumes:
- ./scripts/rs-init.sh:/scripts/rs-init.sh
- /data/mongo/mongoRepl/mongo1:/data/db
networks:
- mongo-network
ports:
- 37017:37017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs:, "--port", "37017" ]
mongo2:
container_name: mongo2
image: mongo:latest
volumes:
- /data/mongo/mongoRepl/mongo2:/data/db
networks:
- mongo-network
ports:
- 37018:37018
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs", "--port", "37018" ]
depends_on:
- mongo1
mongo3:
container_name: mongo3
image: mongo:latest
volumes:
- /data/mongo/mongoRepl/mongo3:/data/db
networks:
- mongo-network
ports:
- 37019:37019
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs", "--port", "37019" ]
depends_on:
networks:
mongo-network:
driver: bridge
3. scripts/rs-init.sh(Replica Set 구성)
#!/bin/bash
DELAY=25
mongo --port 37017 <<EOF
var config = {
"_id": "dbrs",
"version": 1,
"members": [
{
"_id": 1,
"host": "mongo1:37017",
"priority": 2
},
{
"_id": 2,
"host": "mongo2:37018",
},
{
"_id": 3,
"host": "mongo3:37019",
}
]
};
rs.initiate(config, { force: true });
rs.reconfig(config, { force: true });
EOF
echo "****** Waiting for ${DELAY} seconds for replicaset configuration to be applied ******"
sleep $DELAY
4. scripts/startReplicaSetEnvironment.sh(서버 실행)
#!/bin/bash
DELAY=10
docker-compose --file docker-compose-replicaset.yml down
docker-compose --file docker-compose-replicaset.yml up -d
echo "****** Waiting for ${DELAY} seconds for containers to go up ******"
sleep $DELAY
docker exec mongo1 /scripts/rs-init.sh
5. DB 및 계정 생성
mongo1 서버 접속해서 사용자 계정 생성.
$ docker exec –it mongo1 mongo
> use admin
switched to db admin
> db.createUser(
{
user: "admin",
pwd: "qwe123",
roles: [ "root" ]
}
)
> show users
> use mongouser
> db.test.insert({"name": "MongoDB Tutorial", "author": "velopert"});
> use admin
> db.createUser(
{
user: "dbuser",
pwd: "qwe123",
roles: [ "dbAdmin" ]
}
)
6. auth key 생성
Replication을 위한 키를 생성 한다.
$ mkdir mongo-security
$ cd mongo-security
$ openssl rand -base64 768 > file.key
$ chmod 400 file.key
$ sudo chown dbuser:dbuser file.key
7. docker-compose-replicaset.yml(계정 생성후 수정)
version: '3.3'
services:
mongo1:
container_name: mongo1
image: mongo:latest
volumes:
- ./scripts/rs-init.sh:/scripts/rs-init.sh
- ./mongo-security/file.key:/auth/file.key
- /data/mongo/mongoRepl/mongo1:/data/db
networks:
- mongo-network
ports:
- 37017:37017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs", "--keyFile", "/auth/file.key", "--port", "37017" ]
mongo2:
container_name: mongo2
image: mongo:latest
volumes:
- ./mongo-security/file.key:/auth/file.key
- /data/mongo/mongoRepl/mongo2:/data/db
networks:
- mongo-network
ports:
- 37018:37018
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs", "--keyFile", "/auth/file.key", "--port", "37018" ]
depends_on:
- mongo1
mongo3:
container_name: mongo3
image: mongo:latest
volumes:
- ./mongo-security/file.key:/auth/file.key
- /data/mongo/mongoRepl/mongo3:/data/db
networks:
- mongo-network
ports:
- 37019:37019
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "dbrs", "--keyFile", "/auth/file.key", "--port", "37019" ]
depends_on:
networks:
mongo-network:
driver: bridge
8. scripts/rs-init.sh(계정 생성후 수정)
#!/bin/bash
DELAY=25
mongo -u "admin" -p "qwe123" --authenticationDatabase "admin" --port 37017 <<EOF
var config = {
"_id": "dbrs",
"version": 1,
"members": [
{
"_id": 1,
"host": "mongo1:37017",
"priority": 2
},
{
"_id": 2,
"host": "mongo2:37018",
},
{
"_id": 3,
"host": "mongo3:37019",
}
]
};
rs.initiate(config, { force: true });
rs.reconfig(config, { force: true });
EOF
echo "****** Waiting for ${DELAY} seconds for replicaset configuration to be applied ******"
sleep $DELAY
9. 서버 실행
docker-compose 파일이 있는 폴더에서 실행
$ ./scripts/startReplicaSetEnvironment.sh
mongo1 서버에 접속 했을때 PRIMARY로 접속 되는지 확인
$ docker exec -it mongo1 bash
root@8e2629c0b6e1:/# mongo -u admin -p qwe123 --port 37017
'DB' 카테고리의 다른 글
Oracle to PostgreSQL 쿼리 변환 (0) | 2023.04.10 |
---|---|
Postgresql pgpool을 활용한 클러스터 명령어 및 docker-compose (0) | 2023.04.10 |
Postgresql pgpool을 활용한 클러스터링 (0) | 2023.04.10 |
MongoDB Replicaset 구성 (0) | 2023.04.07 |
PostgreSql postgis 쿼리 (0) | 2023.04.07 |