前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >docker-compose部署单机版本分片mongo

docker-compose部署单机版本分片mongo

作者头像
kinnylee
发布2020-10-15 10:12:20
1.3K0
发布2020-10-15 10:12:20
举报
文章被收录于专栏:kinnylee钻研技术

环境介绍

  • os: centos7
  • docker: 18.09.0
  • mongo: 4.0.5

执行步骤

1. 清理旧数据(如果需要)

执行 clean-deploy.sh

  • 删除之前的容器
  • 删除数据目录
代码语言:javascript
复制
DIR=/data/fates
DATA_PATH="${DIR}/mongo"
PWD='kinnylee'

# 第一次执行没有旧数据,不需要执行这步
docker-compose -f fates-mongo-compose.yaml down

if [ -d "${DATA_PATH}" ]; then
    echo "delete directory: ${DATA_PATH}"
    echo ${PWD} | sudo -S rm -rf ${DATA_PATH}
fi

2. 创建数据目录

执行before-deploy.sh

  • 创建数据目录
代码语言:javascript
复制
PWD='kinnylee'

DATA_DIR_LIST=('config1' 'config2' 'config3' 'shard1' 'shard2' 'shard3' 'script')

function check_directory() {
  if [ ! -d "${DATA_PATH}" ]; then
    echo "create directory: ${DATA_PATH}"
    echo ${PWD} | sudo -S mkdir -p ${DATA_PATH}
  else
    echo "directory ${DATA_PATH} already exists."
  fi


  cd "${DATA_PATH}"

  for SUB_DIR in ${DATA_DIR_LIST[@]}
  do
    if [ ! -d "${DATA_PATH}/${SUB_DIR}" ]; then
      echo "create directory: ${DATA_PATH}/${SUB_DIR}"
      echo "${PWD}" | sudo -S mkdir -p "${DATA_PATH}/${SUB_DIR}"
    else
      echo "directory: ${DATA_PATH}/${SUB_DIR} already exists."
    fi
  done

  echo "change directory owner to $USER:$USER"
  echo "${PWD}" | sudo -S chown -R $USER:$USER "${DATA_PATH}"
}

check_directory

3. 部署docker容器

3.1 无授权模式

执行deploy.sh

代码语言:javascript
复制
docker-compose -f fates-mongo-compose.yaml up -d

yaml文件如下

代码语言:javascript
复制
version: '3.4'
services:
  shard1:
    image: mongo:4.0.5
    # --shardsvr: 这个参数仅仅只是将默认的27017端口改为27018,如果指定--port参数,可用不需要这个参数
    # --directoryperdb:每个数据库使用单独的文件夹
    command: mongod --shardsvr --directoryperdb --replSet shard1
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db

  shard2:
    image: mongo:4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db

  shard3:
    image: mongo:4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db

  config1:
    image: mongo:4.0.5
    # --configsvr: 这个参数仅仅是将默认端口由27017改为27019, 如果指定--port可不添加该参数
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config1:/data/configdb

  config2:
    image: mongo:4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config2:/data/configdb

  config3:
    image: mongo:4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config3:/data/configdb

  mongos:
    image: mongo:4.0.5
    # mongo3.6版默认绑定IP为127.0.0.1,此处绑定0.0.0.0是允许其他容器或主机可以访问
    command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017
    ports:
      - 27017:27017
    volumes:
      - /etc/localtime:/etc/localtime
    depends_on:
      - config1
      - config2
      - config3
3.2 授权模式
1. 执行generate-keyfile.sh
  • 生成keyfile文件
代码语言:javascript
复制
#!/bin/bash

DATA_PATH=/data/fates/mongo
PWD='kinnylee'

function check_directory() {
  if [ ! -d "${DATA_PATH}" ]; then
    echo "directory: ${DATA_PATH} not exists, please run before-depoly.sh."
  fi
}

function generate_keyfile() {
  cd "${DATA_PATH}/script"
  if [ ! -f "${DATA_PATH}/script/mongo-keyfile" ]; then
    echo 'create mongo-keyfile.'
    openssl rand -base64 756 -out mongo-keyfile
    echo "${PWD}" | sudo -S chmod 600 mongo-keyfile
    echo "${PWD}" | sudo -S chown 999 mongo-keyfile
  else
    echo 'mongo-keyfile already exists.'
  fi
}

check_directory
generate_keyfile
2. 执行deploy-key.sh
代码语言:javascript
复制
docker-compose -f fates-mongo-compose-key.yaml up -d

yaml文件如下

代码语言:javascript
复制
version: '3.4'
services:
  shard1:
    image: mongo:4.0.5
    # --shardsvr: 这个参数仅仅只是将默认的27017端口改为27018,如果指定--port参数,可用不需要这个参数
    # --directoryperdb:每个数据库使用单独的文件夹
    command: mongod --shardsvr --directoryperdb --replSet shard1 --keyFile /data/mongo-keyfile
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard1:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile

  shard2:
    image: mongo:4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard2 --keyFile /data/mongo-keyfile
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard2:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile

  shard3:
    image: mongo:4.0.5
    command: mongod --shardsvr --directoryperdb --replSet shard3 --keyFile /data/mongo-keyfile
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/shard3:/data/db
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile

  config1:
    image: mongo:4.0.5
    # --configsvr: 这个参数仅仅是将默认端口由27017改为27019, 如果指定--port可不添加该参数
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config1:/data/configdb
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile

  config2:
    image: mongo:4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config2:/data/configdb
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile

  config3:
    image: mongo:4.0.5
    command: mongod --configsvr --directoryperdb --replSet fates-mongo-config --smallfiles --keyFile /data/mongo-keyfile
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/config3:/data/configdb
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile

  mongos:
    image: mongo:4.0.5
    # mongo3.6版默认绑定IP为127.0.0.1,此处绑定0.0.0.0是允许其他容器或主机可以访问
    command: mongos --configdb fates-mongo-config/config1:27019,config2:27019,config3:27019 --bind_ip 0.0.0.0 --port 27017 --keyFile /data/mongo-keyfile
    ports:
      - 27017:27017
    volumes:
      - /etc/localtime:/etc/localtime
      - /data/fates/mongo/script/mongo-keyfile:/data/mongo-keyfile
    depends_on:
      - config1
      - config2
      - config3

4. 配置分片信息

容器启动需要一定时间,执行这一步时要等上一步把容器都启动起来,才能执行,执行不成功就多运行几遍

执行shard-config.sh

代码语言:javascript
复制
docker-compose -f fates-mongo-compose.yaml exec config1 bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"config1:27019\" },{ _id : 1, host : \"config2:27019\" }, { _id : 2, host : \"config3:27019\" }]})' | mongo --port 27019"
docker-compose -f fates-mongo-compose.yaml exec shard1 bash -c "echo 'rs.initiate({_id: \"shard1\",members: [{ _id : 0, host : \"shard1:27018\" }]})' | mongo --port 27018"
docker-compose -f fates-mongo-compose.yaml exec shard2 bash -c "echo 'rs.initiate({_id: \"shard2\",members: [{ _id : 0, host : \"shard2:27018\" }]})' | mongo --port 27018"
docker-compose -f fates-mongo-compose.yaml exec shard3 bash -c "echo 'rs.initiate({_id: \"shard3\",members: [{ _id : 0, host : \"shard3:27018\" }]})' | mongo --port 27018"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard1/shard1:27018\")' | mongo"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard2/shard2:27018\")' | mongo"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard3/shard3:27018\")' | mongo"

没有出现Connection refused等错误就表示成功了

5. 创建用户(授权模式)

执行add-user.sh

  • 需要添加用户登录权限执行这一步,不需要登录的不用执行这一步
  • 脚本中管理员用户名和密码都是root,可自行修改
代码语言:javascript
复制
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n db.createUser({user:\"root\",pwd:\"root\",roles:[{role:\"root\",db:\"admin\"}]})' | mongo"
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show user' | mongo"

6. 验证

  • 通过客户端连接验证是否成功
代码语言:javascript
复制
mongo {ip}
6.1 无授权模式

执行check.sh

代码语言:javascript
复制
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show users' | mongo"
6.2 授权模式

执行check-key.sh

代码语言:javascript
复制
docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show users' | mongo -u root -p root"

完整脚本

  • 容器启动需要一定时间,脚本里设置为120s,但是经常还是有节点完全没有起来,可单独执行shard_config执行分片,否则无法连接到mongo
  • 包括后面添加用户这一步,也是需要等容器完全起来执行才不会报错
  • 这里为了方便一键执行,把所有步骤放到一起了
代码语言:javascript
复制
DIR=/data/fates
DATA_PATH="${DIR}/mongo"

DATA_DIR_LIST=('config1' 'config2' 'config3' 'shard1' 'shard2' 'shard3' 'script')

BASE_DIR=$(cd "$(dirname "$0")";pwd)

function check_directory() {
  if [ ! -d "${DATA_PATH}" ]; then
    echo "create directory: ${DATA_PATH}"
    sudo mkdir -p ${DATA_PATH}
  else
    echo "directory ${DATA_PATH} already exists."
  fi


  cd "${DATA_PATH}"

  for SUB_DIR in ${DATA_DIR_LIST[@]}
  do
    if [ ! -d "${DATA_PATH}/${SUB_DIR}" ]; then
      echo "create directory: ${DATA_PATH}/${SUB_DIR}"
      sudo mkdir -p "${DATA_PATH}/${SUB_DIR}"
    else
      echo "directory: ${DATA_PATH}/${SUB_DIR} already exists."
    fi
  done

  echo "change directory owner to $USER:$USER"
  sudo chown -R $USER:$USER "${DATA_PATH}"
}

function rm_old_directory() {
  if [ -d "${DATA_PATH}" ]; then
      echo "delete directory: ${DATA_PATH}"
      sudo rm -rf ${DATA_PATH}
  fi
}

function cp_script() {
  cp "${BASE_DIR}"/* "${DATA_PATH}"/script
}

function deploy_no_auth() {
  cd "${DATA_PATH}"/script
  docker-compose -f fates-mongo-compose.yaml up -d
}

function deploy_with_auth() {
  cd "${DATA_PATH}"/script
  docker-compose -f fates-mongo-compose-key.yaml up -d
}

function generate_keyfile() {
  cd "${DATA_PATH}/script"
  if [ ! -f "${DATA_PATH}/script/mongo-keyfile" ]; then
    echo 'create mongo-keyfile.'
    openssl rand -base64 756 -out mongo-keyfile
    sudo chmod 600 mongo-keyfile
    sudo chown 999 mongo-keyfile
  else
    echo 'mongo-keyfile already exists.'
  fi
}

function config_shard() {
  cd "${DATA_PATH}/script"
  docker-compose -f fates-mongo-compose.yaml exec config1 bash -c "echo 'rs.initiate({_id: \"fates-mongo-config\",configsvr: true, members: [{ _id : 0, host : \"config1:27019\" },{ _id : 1, host : \"config2:27019\" }, { _id : 2, host : \"config3:27019\" }]})' | mongo --port 27019"
  docker-compose -f fates-mongo-compose.yaml exec shard1 bash -c "echo 'rs.initiate({_id: \"shard1\",members: [{ _id : 0, host : \"shard1:27018\" }]})' | mongo --port 27018"
  docker-compose -f fates-mongo-compose.yaml exec shard2 bash -c "echo 'rs.initiate({_id: \"shard2\",members: [{ _id : 0, host : \"shard2:27018\" }]})' | mongo --port 27018"
  docker-compose -f fates-mongo-compose.yaml exec shard3 bash -c "echo 'rs.initiate({_id: \"shard3\",members: [{ _id : 0, host : \"shard3:27018\" }]})' | mongo --port 27018"
  docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard1/shard1:27018\")' | mongo"
  docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard2/shard2:27018\")' | mongo"
  docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo 'sh.addShard(\"shard3/shard3:27018\")' | mongo"
}

function add_user() {
  cd "${DATA_PATH}/script"
  docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n db.createUser({user:\"root\",pwd:\"root\",roles:[{role:\"root\",db:\"admin\"}]})' | mongo"
}

function check() {
  cd "${DATA_PATH}/script"
  docker-compose -f fates-mongo-compose.yaml exec mongos bash -c "echo -e 'use admin\n show users' | mongo -u root -p root"
}

function main() {
  rm_old_directory
  check_directory
  cp_script
  generate_keyfile
  deploy_no_auth
  sleep 120
  config_shard
  add_user
  deploy_with_auth
  sleep 90
  check
}

main

参考

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 环境介绍
  • 执行步骤
    • 1. 清理旧数据(如果需要)
      • 2. 创建数据目录
        • 3. 部署docker容器
          • 3.1 无授权模式
          • 3.2 授权模式
        • 4. 配置分片信息
          • 5. 创建用户(授权模式)
            • 6. 验证
              • 6.1 无授权模式
              • 6.2 授权模式
          • 完整脚本
          • 参考
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档