《Linux运维总结:基于ARM64+X86_64架构CPU使用docker-compose一键离线部署mongodb 7.0.14容器版分片集群》

总结:整理不易,如果对你有帮助,可否点赞关注一下?

更多详细内容请参考:《Linux运维篇:Linux系统运维指南》


一、部署背景

由于业务系统的特殊性,我们需要面向不通的客户安装我们的业务系统,而作为基础组件中的mongodb针对不同的客户环境需要多次部署集群,作为一个运维工程师,提升工作效率也是工作中的重要一环。所以我觉得有必要针对 X86_64 + ARM64 CPU架构mongodb 7.0.14容器版 分片集群编写自动化部署工具。

CPU架构 docker版本 docker-compose版本
X86_64 26.1.3 v2.29.0
ARM64 26.1.3 v2.29.0

二、工具介绍

一键部署工具实现功能如下:

1、支持ARM64、X86_64架构CPU
2、支持单实例部署
3、支持单机伪分片集群部署(1个config副本集+2个分片副本集+3路由节点)
4、支持多机分布式分片集群部署(1个config副本集+2个分片副本集+3路由节点)
5、支持数据目录、日志目录、端口、账号、密码、副本集合名称、最大并发连接数等灵活配置
6、支持数据文件、日志文件、配置文件持久化
7、支持创建、启动、停止、卸载等操作
8、支持一键连接、一键备份、一键还原等操作

如下图所示:

说明:工具使用非常简单,只需要修改变量文件,就可以快速帮你快速部署mongodb 7.0.14分片集群。


三、工具下载

基于X86_64+ARM64架构CPU使用docker-compose一键离线部署mongodb 7.0.14容器版分片集群工具


四、部署步骤

说明:单机伪集群是指在单主机部署mongodb分片集群,分布式集群是指在多个不同主机上部署mongodb分片集群,这里部署的是多机分布式分片集群。


1、查看帮助命令


2、部署规划

说明:这里由于资源限制,使用4台服务器部署分片集群。如果你的资源充足,建议每个实例使用单独1台服务器部署。

宿主机IP 操作系统 内核版本 节点说明
192.168.1.111 操作系统 5.15.0-113-generic 配置服务副本集(1primary+2secondary,共3个实例)
192.168.1.112 操作系统 5.15.0-113-generic 分片1副本集(1primary+1secondary+1arbiter,共3个实例)
192.168.1.113 操作系统 5.15.0-113-generic 分片2副本集(1primary+1secondary+1arbiter,共3个实例)
192.168.1.114 操作系统 5.15.0-113-generic 路由节点(3个mongos实例)

3、准备一键部署工具包

说明:将一键部署工具包分别上传到mongodb分片集群所有节点上。


4、编辑变量文件

说明:以下操作分别在mongodb分片集群节点的部署工具包中修改cluster.conf文件, 三个节点上cluster.conf文件内容一致

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# cp cluster.conf.tpl cluster.conf
root@localhost:/opt/mongodb-shard-cluster# vim cluster.conf
# 部署顺序: 
#   1、配置节点 > 分片节点1 > 分片节点2 > 路由节点
#   2、配置节点顺序为secondary2>secondary1>primary,分片节点顺序为arbiter>secondary>primary,路由节点顺序为route1>route2>route3
# 建议填写为当前磁盘最大存储空间目录,如果系统盘空间最大,建议填写为/data,如果挂在盘空间最大,建议填写为挂载目录
export BASE_DIR="/data"

# cpu架构,可选值为<x86_64和aarch64>,根据实际情况修改
export CPU_PLATFROM="x86_64"

# mongodb版本,默认不修改
export MONGODB_VERSION="7.0.14"

# Mongodb数据存放根目录,默认即可,建议不要修改
export MONGODB_DATA_DIR="${BASE_DIR}/basic-data/mongodb-shard-cluster"
##################################################################################################################
# 注意:容器网络模式为<host主机网络模式>,监听地址填写为宿主机内网地址,<主机名为容器的主机名,非宿主机主机名>
# mongodb<配置服务>副本集信息
export MONGODB_CONFIG_PRIMARY_HOST_IP="192.168.1.111"                     # primary实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_CONFIG_PRIMARY_HOST_NAME="config-primary.mongo.com"        # primary实例主机名,可根据实际情况修改
export MONGODB_CONFIG_PRIMARY_HOST_PORT="17101"                           # primary实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGODB_CONFIG_SECONDARY1_HOST_IP="192.168.1.111"                  # secondary1实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_CONFIG_SECONDARY1_HOST_NAME="config-secondary1.mongo.com"  # secondary1实例主机名,可根据实际情况修改
export MONGODB_CONFIG_SECONDARY1_HOST_PORT="17102"                        # secondary1实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGODB_CONFIG_SECONDARY2_HOST_IP="192.168.1.111"                  # secondary2实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_CONFIG_SECONDARY2_HOST_NAME="config-secondary2.mongo.com"  # secondary2实例主机名,可根据实际情况修改
export MONGODB_CONFIG_SECONDARY2_HOST_PORT="17103"                        # secondary2实例映射宿主机端口,端口不冲突情况下,建议不修改
export MONGODB_CONFIG_REPLICASET_NAME="config-rs"                         # config服务副本集合名称,默认即可,建议不修改

# mongodb<分片1>副本集信息
export MONGODB_SHARD1_PRIMARY_HOST_IP="192.168.1.112"                     # primary实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_SHARD1_PRIMARY_HOST_NAME="shard1-primary.mongo.com"        # primary实例主机名,可根据实际情况修改
export MONGODB_SHARD1_PRIMARY_HOST_PORT="17201"                           # primary实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGODB_SHARD1_SECONDARY_HOST_IP="192.168.1.112"                   # secondary实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_SHARD1_SECONDARY_HOST_NAME="shard1-secondary.mongo.com"    # secondary实例主机名,可根据实际情况修改
export MONGODB_SHARD1_SECONDARY_HOST_PORT="17202"                         # secondary实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGODB_SHARD1_ARBITER_HOST_IP="192.168.1.112"                     # arbiter实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_SHARD1_ARBITER_HOST_NAME="shard1-arbiter.mongo.com"        # arbiter实例主机名,可根据实际情况修改
export MONGODB_SHARD1_ARBITER_HOST_PORT="17203"                           # arbiter实例映射宿主机端口,端口不冲突情况下,建议不修改
export MONGODB_SHARD1_REPLICASET_NAME="shard1-rs"                         # 分片1副本集合名称,默认即可,建议不修改

# mongodb<分片2>副本集信息
export MONGODB_SHARD2_PRIMARY_HOST_IP="192.168.1.113"                     # primary实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_SHARD2_PRIMARY_HOST_NAME="shard2-primary.mongo.com"        # primary实例主机名,可根据实际情况修改
export MONGODB_SHARD2_PRIMARY_HOST_PORT="17301"                           # primary实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGODB_SHARD2_SECONDARY_HOST_IP="192.168.1.113"                   # secondary实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_SHARD2_SECONDARY_HOST_NAME="shard2-secondary.mongo.com"    # secondary实例主机名,可根据实际情况修改
export MONGODB_SHARD2_SECONDARY_HOST_PORT="17302"                         # secondary实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGODB_SHARD2_ARBITER_HOST_IP="192.168.1.113"                     # arbiter实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGODB_SHARD2_ARBITER_HOST_NAME="shard2-arbiter.mongo.com"        # arbiter实例主机名,可根据实际情况修改
export MONGODB_SHARD2_ARBITER_HOST_PORT="17303"                           # arbiter实例映射宿主机端口,端口不冲突情况下,建议不修改
export MONGODB_SHARD2_REPLICASET_NAME="shard2-rs"                         # 分片2副本集合名称,默认即可,建议不修改

# mongods<路由实例>信息
export MONGOS_ROUTE1_HOST_IP="192.168.1.114"                              # mongos1实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGOS_ROUTE1_HOST_NAME="route1.mongo.com"                         # mongos1实例主机名,可根据实际情况修改
export MONGOS_ROUTE1_HOST_PORT="17401"                                    # mongos1实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGOS_ROUTE2_HOST_IP="192.168.1.114"                              # mongos2实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGOS_ROUTE2_HOST_NAME="route2.mongo.com"                         # mongos2实例主机名,可根据实际情况修改
export MONGOS_ROUTE2_HOST_PORT="17402"                                    # mongos2实例映射宿主机端口,端口不冲突情况下,建议不修改

export MONGOS_ROUTE3_HOST_IP="192.168.1.114"                              # mongos3实例监听地址,建议填写<宿主机内网地址>,根据实际情况填写
export MONGOS_ROUTE3_HOST_NAME="route3.mongo.com"                         # mongos3实例主机名,可根据实际情况修改
export MONGOS_ROUTE3_HOST_PORT="17403"                                    # mongos3实例映射宿主机端口,端口不冲突情况下,建议不修改

# mongos或mongod接受的最大并行连接数,如果该设置高于操作系统配置<文件描述符,ulimit -n>的最大连接跟踪阈值,则该设置无效 
export MONGODB_MAX_CONNECTIONS="10000"

# Mongodb分片集群<admin库+管理账号+密码>,密码要求大小写和数字,且8位以上,根据实际情况填写
# 注意:<分片集群用户admin库管理账号密码>与<本地分片admin库用户管理账号和密码>相同,均使用MONGODB_ADMIN_USER和MONGODB_ADMIN_PASSWORD变量
export MONGODB_ADMIN_USER="admin"          
export MONGODB_ADMIN_PASSWORD="Kzyh9nwyDWuL"

# Mongodb分片集群<读写库+用户账号+密码>,密码要求大小写和数字,且8位以上,根据实际情况填写
# 注意:<分片集群读写库用户管理账号密码>与<本地分片读写库用户管理账号和密码>相同,均使用MONGODB_ADMIN_USER和MONGODB_ADMIN_PASSWORD变量
export MONGODB_READWRITE_DATABASE="lolaage"
export MONGODB_READWRITE_USER="lolaage"
export MONGODB_READWRITE_PASSWORD="Kzyh9nwyDWuL"
##################################################################################################################

5、执行部署

说明:部署顺序必须为
1、配置节点 > 分片节点1 > 分片节点2 > 路由节点
2、配置节点顺序为secondary2>secondary1>primary,分片节点顺序为arbiter>secondary>primary,路由节点顺序为route1>route2>route3


a、配置服务副本集部署(192.168.1.111)

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build config-secondary2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build config-secondary1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build config-primary

如下图所示:


b、分片1副本集部署(192.168.1.112)

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build shard1-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build shard1-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build shard1-primary

如下图所示:


c、分片2副本集部署(192.168.1.113)

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build shard2-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build shard2-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build shard2-primary

如下图所示:


d、路由节点部署(192.168.1.114)

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build route1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build route2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh build route3

如下图所示:


6、检查集群状态

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check route3

如下图所示:


五、其它操作

5.1、启动

说明:分别在对应主机节点上执行。

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start config-secondary2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start config-secondary1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start config-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh start shard1-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start shard1-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start shard1-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh start shard2-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start shard2-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start shard2-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh start route1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start route2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh start route3

5.2、停止

说明:分别在对应主机节点上执行。

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop config-secondary2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop config-secondary1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop config-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop shard1-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop shard1-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop shard1-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop shard2-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop shard2-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop shard2-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop route1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop route2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh stop route3

5.3、检查

说明:分别在对应主机节点上执行。

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check config-secondary2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check config-secondary1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check config-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh check shard1-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check shard1-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check shard1-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh check shard2-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check shard2-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check shard2-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh check route1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check route2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh check route3

5.4、卸载

说明:分别在对应主机节点上执行。

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear config-secondary2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear config-secondary1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear config-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear shard1-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear shard1-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear shard1-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear shard2-arbiter
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear shard2-secondary
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear shard2-primary

root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear route1
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear route2
root@localhost:/opt/mongodb-shard-cluster# ./op.sh clear route3

5.5、连接

说明:分别在对应主机节点上执行。

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect config-secondary2
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect config-secondary1
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect config-primary

root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect shard1-arbiter
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect shard1-secondary
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect shard1-primary

root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect shard2-arbiter
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect shard2-secondary
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect shard2-primary

root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect route1
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect route2
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_connect.sh connect route3

5.6、备份(支持单机和分片集群模式)

说明:在route3节点上执行。

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# cd tools
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_backup.sh backup route3

注意:如果你想手动备份还原,建议参考《Linux运维总结:使用 MongoDB工具备份和恢复mongodb 7.0.14分片集群(方案一)》


5.7、还原(仅支持单机模式)

bash 复制代码
root@localhost:/opt/mongodb-shard-cluster# cd tools
root@localhost:/opt/mongodb-shard-cluster/tools# ./auto_backup.sh backup single

注意:由于分片集群还原操作,比较复杂,建议参考《Linux运维总结:使用 MongoDB工具备份和恢复mongodb 7.0.14分片集群(方案一)》


总结:整理不易,如果对你有帮助,可否点赞关注一下?

更多详细内容请参考:《Linux运维篇:Linux系统运维指南》

相关推荐
x18783375844 分钟前
k8s的安装和部署
linux·服务器·kubernetes
weixin_439469455 分钟前
【外部配置文件映射到docker容器内部】
运维·docker·容器
南希夜酒5 分钟前
在Linux系统安装Nginx
java·linux·运维·服务器·centos·云计算
小胖胖吖7 分钟前
docker compose一键部署容器监控 CAdvisor+InfluxDB+Granfana
运维·docker·容器
微刻时光10 分钟前
Docker镜像命令汇总笔记
linux·运维·笔记·ubuntu·docker·容器·centos
华为云PaaS服务小智10 分钟前
搭建Serverless社交媒体分析平台,Dataxet运维成本降低50%
运维·serverless·媒体
大吱佬12 分钟前
CentOS快速配置网络&&Docker快速部署
linux·服务器·tcp/ip
Hello_shuo23 分钟前
【操作系统】引导(Boot)电脑的奇妙开机过程
linux·windows
FANGhelloworld27 分钟前
TCP网络通信——多线程
linux·服务器·网络·信息与通信·tcp
wait a minutes1 小时前
【ubuntu】ubuntu20.04 install vscode
linux·vscode·ubuntu