Introduction
앞에서 정리한 두 내용에 따라 실제 구성을 진행하면서 Shard cluster를 완성해보겠습니다.
configsvr
docker-compose.yml
version: '3'
services:
cfgsvr1:
build:
context: cfg
container_name: cfgsvr1
image: mongo
#command: mongod --configsvr --replSet cfgrs --port 27017 --dbpath /data/db --bind_ip_all
command: mongod --configsvr --replSet cfgrs-1 --port 27017 --dbpath /data/db --bind_ip_all --keyFile /data/mongodb.key --auth --clusterAuthMode keyFile
ports:
- 40001:27017
volumes:
- cfgsvr1:/data/db
- /data/mongodb-cluster/config-compose/cfg/mongod.conf:/etc/mongod.conf
volumes:
cfgsvr1: {}
Dockerfile
FROM mongo:latest
COPY --chown=mongodb:mongodb mongodb.key /data/mongodb.key
RUN chmod 400 /data/mongodb.key
RUN chown mongodb:mongodb /data/mongodb.key
CMD ["/bin/bash"]
mongod.conf
# mongod.conf
storage:
dbPath: /var/lib/mongodb
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIpAll: true
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
# authorization: disabled
authorization: enabled
clusterAuthMode: keyFile
keyFile : /data/mongodb.key
sharding:
clusterRole: configsvr
shardsvr
docker-compose.yml
version: '3'
services:
shard1svr1:
build:
context: sha
container_name: shard1svr1
image: mongo
#command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db --bind_ip_all
command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db --keyFile /data/mongodb.key --auth --bind_ip_all --clusterAuthMode keyFile
ports:
- 50001:27017
volumes:
- shard1svr1:/data/db
- /data/mongodb-cluster/shard-compose/sha/mongod.conf:/etc/mongod.conf
volumes:
shard1svr1: {}
mongod.conf
# mongod.conf
storage:
dbPath: /var/lib/mongodb
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIpAll: true
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
#authorization: disabled
authorization: enabled
clusterAuthMode: keyFile
keyFile : /data/mongodb.key
clusterIpSourceAllowlist:
- 0.0.0.0/0
- ::/0
(Dockerfile은 configsvr에 이랑 동일)
mongos
docker-compose.yml
version: '3'
services:
mongos:
build:
context: mog
container_name: mongos
image: mongo
#command: mongos --configdb cfgrs/192.168.2.245:40001,192.168.2.246:40002,192.168.2.247:40003 --bind_ip_all --port 27017
command: mongos --configdb cfgrs/192.168.2.245:40001,192.168.2.246:40002,192.168.2.247:40003 --bind_ip_all --port 27017 --keyFile /data/mongodb.key --clusterAuthMode keyFile
ports:
- 60000:27017
volumes:
- /data/mongodb-cluster/mongos-compose/mog/mongod.conf:/etc/mongod.conf
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
interval: 30s
timeout: 10s
retries: 5
mongod.conf
# mongod.conf
storage:
dbPath: /var/lib/mongodb
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIpAll: true
processManagement:
timeZoneInfo: /usr/share/zoneinfo
security:
#authorization: disabled
authorization: enabled
clusterAuthMode: keyFile
keyFile : /data/mongodb.key
clusterIpSourceAllowlist:
- 0.0.0.0/0
- ::/0
sharding:
configdb: cfgrs/192.168.2.245:40001,192.168.2.246:40002,192.168.2.247:40003
실행 절차 정리
1) ※ 중요 포인트 처음 docker compose를 진행하기전 설정사항
docker-compose.yml
> command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db --bind_ip_all
mongod.conf
> security: authorization: disabled
위 소스에서 주석처리된 security와 keyfile 세팅이 안된 상태로 첫 빌드를 진행해야 함
2) 각 서버에서 동일하게 3번 작업
cd /data/mongodb-cluster/config-compose/
docker compose up -d --build
3) primary configsvr에 접속하여 작업진행
docker exec -it <configsvr 컨테이너이름> mongosh
use admin
db.createUser({
user: "admin",
pwd: "admin",
roles: [
{
role: "root",
db: "admin"
}
]}
)
use [디비이름]
db.createUser({
user: "owner",
pwd: "owner",
roles: [
{
role: "dbOwner",
db: "[디비이름]"
}
]}
)
rs.initiate(
{
_id: "cfgrs",
configsvr: true,
members: [
{ _id : 0, host : "<서버1>:40001" },
{ _id : 1, host : "<서버2>:40002" },
{ _id : 2, host : "<서버3>:40003" }
]
}
)
4) 각 서버에서 동일하게 3번 작업 (이때도 1에서 진행한 내용 동일)
cd /data/mongodb-cluster/shard-compose/
docker compose up -d --build
5) primary shardsvr에 접속하여 작업진행
docker exec -it <shardsvr 컨테이너이름> mongosh
사용자 계정 및 디비생성 3번에서와 동일하게 진행하고,
rs.initiate(
{
_id: "shard1rs",
members: [
{ _id : 0, host : "<서버1>:50001" },
{ _id : 1, host : "<서버2>:50002" },
{ _id : 2, host : "<서버3>:50003" }
]
}
)
6) 다음 진행하기 전 체크사항
primary configsvr, shardsvr에 접속해서
use admin
use <작업할 데이터베이스>에서
db.auth('아이디', '패스워드') 를 하여 접속한 뒤
show users를 통해 생성된 계정확인
이때 확인을 하지 않으면 security auth keyfile 설정 후 계정 생성하기 어려움
7) security auth keyfile 적용
docker-compose.yml
> command: mongod --configsvr --replSet cfgrs-1 --port 27017 --dbpath /data/db --bind_ip_all --keyFile /data/mongodb.key --auth --clusterAuthMode keyFile
mongod.conf
>
security:
authorization: enabled
clusterAuthMode: keyFile
keyFile : /data/mongodb.key
clusterIpSourceAllowlist:
- 0.0.0.0/0
- ::/0
위 두 내용을 적용하여 다시 compose 진행
서버 3개 => 총 컨테이너 6개(configsvr 3개, shardsvr 3개)
cd /data/mongodb-cluster/config-compose/
docker compose up -d --build
cd /data/mongodb-cluster/shard-compose/
docker compose up -d --build
8) mongos compose 진행
cd /data/mongodb-mongos/shard-compose/
docker compose up -d --build
mongos는 security auth keyfile 적용한 상태로 배포
docker exec -it mongos mongosh
sh.addShard("shard1rs/192.168.2.245:50001")
sh.addShard("shard1rs/192.168.2.246:50002")
sh.addShard("shard1rs/192.168.2.247:50003")
sh.enableSharding("<대상 데이터베이스>")
9) mongodb 설정 진행
> db.createCollection("x2bee")
> db.x2bee.createIndex({pid: -1})
> db.x2bee.insertOne({pid: 10001})
> db.x2bee.ensureIndex({pid : "hashed"})
> sh.shardCollection("admin.x2bee", {pid : "hashed"})
> db.x2bee.getShardDistribution()
여기까지 순조롭게 진행된다면, mongodb shard 구성이 끝이 납니다.
'빅데이터 > Mongo DB' 카테고리의 다른 글
[샤드] mongodb shard 구성 - docker (0) | 2024.02.27 |
---|---|
[샤드] mongodb shard 구성 - install (0) | 2024.02.27 |
Mongodb database 생성 및 CRUD 테스트 (0) | 2023.11.22 |