本地使用 docker-componse 搭建 mongodb 3 节点集群[1主2辅,自动切换主备,可改为真实异地物理节点]
支持:在另一个 docker-componse app 内访问集群支持:在宿主机访问集群
事务功能需要集群
读写分离,异地容灾
mongo 集群搭建
docker-compose.yml
version: '3'
services:
mongo1:
hostname: mongo1
container_name: localmongo1
image: mongo
restart: always
expose:
- 27017
ports:
- 27017:27017
command: '--quiet --bind_ip_all --port 27017 --replSet rs0'
volumes:
- ./db1/mongo:/data/db
#environment:
#MONGO_INITDB_ROOT_USERNAME: root
#MONGO_INITDB_ROOT_PASSWORD: root
networks:
- default
- nginx-proxy
mongo2:
hostname: mongo2
container_name: localmongo2
image: mongo
restart: always
expose:
- 27018
ports:
- 27018:27018
command: '--quiet --bind_ip_all --port 27018 --replSet rs0'
volumes:
- ./db2/mongo:/data/db
#depends_on:
#- mongo1
networks:
- default
- nginx-proxy
mongo3:
hostname: mongo3
container_name: localmongo3
image: mongo
restart: always
expose:
- 27019
ports:
- 27019:27019
command: '--quiet --bind_ip_all --port 27019 --replSet rs0'
volumes:
- ./db3/mongo:/data/db
#depends_on:
#- mongo2
networks:
- default
- nginx-proxy
rsinit:
build:
context: .
dockerfile: rsinit
depends_on:
- mongo1
- mongo2
- mongo3
entrypoint: ["sh", "-c", "rs.sh"]
networks:
- default
- nginx-proxy
networks:
nginx-proxy:
external: true
rs.sh
#!/bin/bash
echo "prepare rs initiating"
check_db_status() {
mongo1=$(mongo --host mongo1 --port 27017 --eval "db.stats().ok" | tail -n1 | grep -E '(^|\s)1($|\s)')
mongo2=$(mongo --host mongo2 --port 27018 --eval "db.stats().ok" | tail -n1 | grep -E '(^|\s)1($|\s)')
mongo3=$(mongo --host mongo3 --port 27019 --eval "db.stats().ok" | tail -n1 | grep -E '(^|\s)1($|\s)')
if [[ $mongo1 == 1 ]] && [[ $mongo2 == 1 ]] && [[ $mongo3 == 1 ]]; then
init_rs
else
check_db_status
fi
}
init_rs() {
ret=$(mongo --host mongo1 --port 27017 --eval "rs.initiate({ _id: 'rs0', members: [{ _id: 0, host: 'mongo1:27017' }, { _id: 1, host: 'mongo2:27018' }, { _id: 2, host: 'mongo3:27019' } ] })" > /dev/null 2>&1)
}
check_db_status > /dev/null 2>&1
echo "rs initiating finished"
exit 0
rsinit
FROM mongo
ADD rs.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/rs.sh
运行
docker-compose up --build
停止
docker-compose down
在宿主机访问集群
在本机 host 文件添加解析
127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3
访问集群的连接地址
mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=rs0
可选的配置
mongodb://mongo1:27017,mongo2:27018,mongo3:27019/?replicaSet=rs0&slaveOk=true&authSource=admin&readPreference=secondaryPreferred&w=majority&ssl=false
MongoDB Composs 连接成功会显示为3节点集群
在其他 docker-componse 开启的 app 容器内访问集群
Dockerfile
FROM node:12.13.1-alpine3.10
MAINTAINER black
#RUN apk add --no-cache curl
# create a specific user to run this container
#RUN adduser -S -D user-app
# add files to container
#ADD . /app
# specify the working directory
WORKDIR /app
RUN chmod -R 777 /app
# build process
#RUN npm install
#RUN npm run build
#RUN npm prune --production
# run the container using a specific user
#USER user-app
#EXPOSE 8080
# run application
#CMD ["npm", "start"]
docker-compose.yml
version: '3.7'
services:
graphql:
build:
context: ./
dockerfile: Dockerfile
command: 'npm run start'
#environment:
#UV_THREADPOOL_SIZE: 128
#NODE_APP_INSTANCE: 0
#NODE_ENV: docker
#DB_URL: postgres://root:123@postgres/project
expose:
- 4000
ports:
- "80:4000"
volumes:
#- ./:/usr/src/app/
- ./:/app/
networks:
- default
- nginx-proxy
networks:
nginx-proxy:
external: true
运行
docker-compose up --build
停止
docker-compose down
原文地址 : https://github.com/WangShuXia...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。