본문 바로가기
개발

MongoDB replica set 사용하기(with Docker Compose, Haproxy load balancer)

by amkorousagi 2023. 5. 15.

MongoDB replica set 사용하기(with Docker Compose, Haproxy load balancer)

MongoDB replica set

MongoDB replica set  설명


production 환경을 위해

mongodb replica set을 사용합니다.

 

MongoDB replica set  사용 방법


0. 환경 확인

저는 window 10 환경입니다.

일반적으로는 linux 계열의 OS에서 하시는 것을 추천합니다.

(윈도우는 LF나 권한 및 소유 문제 등이 발생합니다.)

 

 

1. docker desktop을 설치

 

Install Docker Desktop on Windows

 

docs.docker.com

 

 

2. docker-compose.yml 작성

# Use root/example as user/password credentials
version: "3.8"

services:
  mongo1:
    image: mongo
    container_name: mongo1
    restart: always
    ports:
      - "4001:27017"
    environment:
      - .env
    volumes:
      - ./mongo1:/data/db
      - ./key:/key
      - ./mongodb.conf:/etc/mongodb.conf
    networks:
      - mongo-net
    command: mongod --config /etc/mongodb.conf

  mongo2:
    image: mongo
    container_name: mongo2
    restart: always
    ports:
      - "4002:27017"
    environment:
      - .env
    volumes:
      - ./mongo2:/data/db
      - ./key:/key
      - ./mongodb.conf:/etc/mongodb.conf
    networks:
      - mongo-net
    command: mongod --config /etc/mongodb.conf

  mongo3:
    image: mongo
    container_name: mongo3
    restart: always
    ports:
      - "4003:27017"
    environment:
      - .env
    volumes:
      - ./mongo3:/data/db
      - ./key:/key
      - ./mongodb.conf:/etc/mongodb.conf
      - ./haproxy.cfg:/etc/haproxy.cfg
    networks:
      - mongo-net
    command: mongod --config /etc/mongodb.conf

  haproxy:
    image: haproxy:2.4
    restart: always
    container_name: haproxy
    ports:
      - 3015:27017
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
    networks:
      - mongo-net

networks:
  mongo-net:
    driver: bridge

 

 

3. mongodb key file 생성

mkdir key
cd key
openssl rand -base64 756 > mongodb.key

만약 window라면 추후 에러가 날 수 있으니 다음 포스팅을 참조하시길 바랍니다.

 

mongod --keyFile 시 permissions on /etc/mongodb.key are too open 또는 /etc/mongodb.key: bad file

mongod --keyFile 시 permissions on /etc/mongodb.key are too open 또는 /etc/mongodb.key: bad file key: bad file or key are too open 에러 내용 docker-compose를 활용해 mongodb replica set을 구축하는 도중 위와 같은 에러들을 만났습

amkorousagi-money.tistory.com

 

4. .env 작성

MONGO_HOST=0.0.0.0
MONGO_PORT=27017
MONGO_INITDB_ROOT_USERNAME=root
MONGO_INITDB_ROOT_PASSWORD=password # 본인이 원하는 비밀번호로 대체
MONGO_INITDB_DATABASE=koj

 

5. mongodb.conf 작성

systemLog:
  destination: file
  path: "/var/log/mongodb/mongod.log"
  logAppend: true

storage:
  dbPath: "/data/db"

net:
  port: 27017
  bindIp: "0.0.0.0"

replication:
  replSetName: "rs0"

security:
  keyFile: "/key/mongodb.key"
  authorization: "enabled"

 

 

6. haproxy.cfg 작성

global
  daemon
  maxconn 256

defaults
  mode http
  timeout connect 5000ms
  timeout client 50000ms
  timeout server 50000ms

frontend mongo_frontend
  bind *:27017
  mode tcp
  option tcplog
  default_backend mongo_backend

backend mongo_backend
  mode tcp
  balance roundrobin
  server mongo1 mongo1:27017 check
  server mongo2 mongo2:27017 check
  server mongo3 mongo3:27017 check

윈도우라면 LF 관련 문제가 있으니 따로 도커를 따서 작성하시기 바랍니다.

 

 

7. docker compose up -d

docker compose up -d

 

 

8. mongodb replica set init

docker exec -it mongo1 /bin/bash
mongosh
use admin
db.runCommand({replSetInitiate:{_id:"rs0",members:[{_id:0,host:"mongo1:27017"},{_id:1,host:"mongo2:27017"},{_id:2,host:"mongo3:27017"}]}})
db.createUser(
  {
    user: "root",
    pwd: "password",
    roles: [ { role: "root", db: "admin" } ]
  }
)
db.auth("root","password")
db.createUser({user:"myUser",pwd:"myPassword",roles:[{role:"readWrite",db:"myDB"}]})

 

 

MongoServerError: Authentication failed.

MongoServerError: Authentication failed. 또는 MongoServerError: not authorized on admin to execute command MongoServerError: Authentication failed. 에러 내용 mongosh로 유저 생성 등의 초기화 과정을 수행하기 위해 db.auth()를 진행

amkorousagi-money.tistory.com

 

 

9. docker compose down && docker compose up -d

docker compose down
docker compose up -d

 

MongoDB replica set 결과


로그 파일을 통해 정상적으로 haproxy load balancer, mongodb replica set이 동작함을 알 수 있습니다.

haproxy load balancer log

 

 

 

또한 이를 node에서 mongoose.connect를 통해 로그밸런서를 통해 db에 접근할 수 있음을 확인할 수 있습니다.

external node mongoose connect log

 

readPreference 설정도 해야한다.

 

MongoDB Compass Secondary node가 비었을 때

db.getMongo().setReadPref('secondary') readPreference: 'primary' MongoDB Compass Secondary node가 비었을 때 에러 내용 MongoDB Compass로 Mongo Replica set의 Seconadry node에 연결하면 모두 비어있다. 에러 원인 기본 설정이 second

amkorousagi-money.tistory.com

 

참조


 

MongoDB Documentation

Atlas MongoDB Atlas is a multi-cloud developer data platform that provides the database and data services that accelerate and simplify how you build with data. Available on AWS, Google Cloud, and Microsoft Azure. MongoDB Database Manual The official manual

www.mongodb.com

 

HAProxy - The Reliable, High Perf. TCP/HTTP Load Balancer

2.5 2021-11-23 2023-Q1 (unmaintained) 2.5.14 2023/05/02 git / web / dir / announce / bugs 2.3 2020-11-05 2022-Q1 (unmaintained) 2.3.21 2022/07/27 git / web / dir / announce / bugs 2.1 2019-11-25 2021-Q1 (unmaintained) 2.1.12 2021/03/18 git / web / dir / an

www.haproxy.org

 

Docker Compose overview

 

docs.docker.com

 

 

 

댓글