docker-compose部署mongodb副本集(无需初始化)

部署两节点 两副本,一仲裁

192.168.171.101 primary,arbiter
192.168.171.102 secondary

部署流程

1 配置密钥

主节点上操作。复制到其他节点

openssl rand -base64 756 > mongodb.key
chmod 400 mongodb.key

2 生成密码:(这里为方便不做此步。)

openssl rand -hex 15
随机生成15个字符的16位进制的密码

3 docker-compose

101:

version: '3.3'

services:
  mongo-primary:
    image: mongo:4.4.2
    volumes:
      - ./db:/data/db
      - ./mongodb.key:/data/mongodb.key
    user: root
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    container_name: mongo-pri
    ports:
      - 27017:27017
    command: mongod --replSet mongoReplSet --keyFile /data/mongodb.key
    restart: always
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /data/mongodb.key
        chown 999:999 /data/mongodb.key
        exec docker-entrypoint.sh $$@
  mongo-arbiter:
    image: mongo:4.4.2
    volumes:
      - ./arb_db:/data/db
      - ./mongodb.key:/data/mongodb.key
    user: root
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    container_name: mongo-arb
    ports:
      - 27018:27017
    command: mongod --replSet mongoReplSet --keyFile /data/mongodb.key
    restart: always
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /data/mongodb.key
        chown 999:999 /data/mongodb.key
        exec docker-entrypoint.sh $$@
  mongo-init:
    image: mongo:4.4.2
    depends_on:
      - mongo-primary
      - mongo-arbiter
    restart: on-failure:5
    command:
      - mongo
      - mongodb://admin:[email protected]:27017/admin
      - --eval
      - 'rs.initiate({_id:"mongoReplSet",members:[{_id:1,host:"192.168.171.101:27017",priority:3},{_id:2,host:"192.168.171.102:27017",priority:2},{_id:3,host:"192.168.171.101:37019",arbiterOnly:true}}]})'

priority:数值越大,越先可能被选为primary

102:

version: '3.3'

services:
  mongo-sec1:
    image: mongo:4.4.2
    volumes:
      - ./db:/data/db
      - ./mongodb.key:/data/mongodb.key
    user: root
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    container_name: mongo-sec1
    ports:
      - 27017:27017
    command: mongod --replSet mongoReplSet --keyFile /data/mongodb.key
    restart: always
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /data/mongodb.key
        chown 999:999 /data/mongodb.key
        exec docker-entrypoint.sh $$@
  mongo-arbiter:
    image: mongo:4.4.2
    volumes:
      - ./arb_db:/data/db
      - ./mongodb.key:/data/mongodb.key
    user: root
    environment:
      - MONGO_INITDB_ROOT_USERNAME=admin
      - MONGO_INITDB_ROOT_PASSWORD=admin
    container_name: mongo-arb1
    ports:
      - 27018:27017
    command: mongod --replSet mongoReplSet --keyFile /data/mongodb.key
    restart: always
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /data/mongodb.key
        chown 999:999 /data/mongodb.key
        exec docker-entrypoint.sh $$@

注意:需要先启动102节点上的服务。否则101上的mongo-init会因找不到102而发生错误。

测试

docker-compose部署mongodb副本集(无需初始化)_第1张图片

docker-compose部署mongodb副本集(无需初始化)_第2张图片

在主mongo上插入数据

use test
db.use.insert({"username": "visionwang",age:26})

在从节点上查看

mongoReplSet:SECONDARY> rs.slaveOk();
 #从节点默认不可读写,明确从节点参数,暂时设置为可读
use test      #切换到test库
db.use.find();   #查看数据

docker-compose部署mongodb副本集(无需初始化)_第3张图片
到这里mongodb的副本部署已经完成。

你可能感兴趣的:(数据库,docker,云计算,mongodb,docker,数据库)