mongodb containerized deployment

mongodb containerized deployment

1. Single node

  • create working directory
mkdir -p /data/mongodb/{
    
    conf,data,logs}
  • container orchestration

Constructdocker-compose.yaml

version: '3'
services:
  mongodb-container:
    image: mongo:4.4.18
    container_name: mongodb-container
    command: [--auth]
    environment:
      # 时区上海
      TZ: Asia/Shanghai
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: Gn8_J777sxxn
    volumes:
        - /data/mongodb/data:/data/db
        - /data/mongodb/conf:/data/configdb
        - /data/mongodb/logs:/var/log/mongodb
    ports:
      - 27017:27017
    restart: always
    logging:
      driver: json-file
      options:
        max-size: "100m"
        max-file: "50"
  • start service
cd /data/mongodb
docker-compose -f docker-compose.yaml up -d
  • verify
# 进入容器
docker exec -it mongodb-container bash
mongo 127.0.0.1:27017/admin -u root -p

2. Master-slave replication

A replica cluster is a group of mongod instances maintaining the same dataset. A replica set consists of several data-carrying nodes and an optional quorum node. Among the nodes carrying data, only one node is considered as the primary node, and the other nodes are considered as secondary nodes.

The primary node receives all write operations, and the secondary node receives the operations of the primary node and applies them to its own data set. The replica node is only used as a backup. When the primary node fails, a new primary node will be automatically selected.

This tutorial describes the construction of a replica cluster consisting of three nodes, and access control is not enabled on all nodes.

Because the cluster needs to determine the master node through elections, in order to avoid problems in the election, the number of cluster members should always be an odd number.

  • create working directory
# 在所有节点创建工作目录
mkdir -p /data/mongodb/{
    
    conf,data,logs}
  • create keyfile
cd /data/mongodb/conf
openssl rand -base64 756 > mongodb.key
chmod 400 mongodb.key
# 将mongodb.key分发至所有mongodb宿主机的相同目录
  • container orchestration

docker-compose.yaml

version: '3'
services:
  mongodb:
    image: mongo:4.4.18
    container_name: mongodb
    command: mongod --auth --keyFile /data/configdb/mongodb.key --replSet MongoRs
    environment:
      # 时区上海
      TZ: Asia/Shanghai
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: Gn8_J777sxxn
    volumes:
        - /data/mongodb/data:/data/db
        - /data/mongodb/conf:/data/configdb
        - /data/mongodb/logs:/var/log/mongodb
    ports:
      - 27017:27017
    entrypoint:
      - bash
      - -c
      - |
        chmod 400 /data/configdb/mongodb.key
        chown 999:999 /data/configdb/mongodb.key
        exec docker-entrypoint.sh $$@
    restart: always
  • start service

Start the service on each node

cd /data/mongodb
docker-compose -f docker-compose.yaml up -d
  • Configure replica set
# 本次假设需要设定192.168.1.11为主,其余为从
# 登录192.168.10.101上的库(使用客户端命令连接任意一个节点,但这里尽量要连接主节点)
docker exec -it mongodb bash
mongo 192.168.1.11:27017/admin -u root -p

# 添加配置信息
# 这里的_id要与docker-compose中的参数一致,priority指定选举优先级,越高越容易被选举为主,默认为1
> config={
    
    _id:"MongoRs",members:[ 
{
    
    _id:0,host:"192.168.1.11:27017",priority:10}, 
{
    
    _id:1,host:"192.168.1.12:27017",priority:1}, 
{
    
    _id:2,host:"192.168.1.12:27017",priority:1}] 
}

# 初始化副本集
> rs.initiate(config)

# 查看副本集配置信息
> rs.conf()

# 查看副本集运行状态:
> rs.status()
  • Configure the replica set in application.yml
spring:
  data:
    mongodb:
      uri: mongodb://root:[email protected]:27017,192.168.1.12:27017,192.168.1.13:27017/admin?replicaSet=MongoRs&readPreference=secondaryPreferred&connectTimeoutMS=300000&slaveOk=true

Guess you like

Origin blog.csdn.net/m0_38004228/article/details/130520364