# ansible-mongodb-cluster **Repository Path**: brianchou/ansible-mongodb-cluster ## Basic Information - **Project Name**: ansible-mongodb-cluster - **Description**: Ansible 一键部署mongodb cluster集群 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 3 - **Created**: 2021-06-05 - **Last Updated**: 2024-07-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Ansible 部署 MongoDB Cluster 集群 ## 1. 服务器规划 3台虚拟机,操作系统为CentOS7.5 集群规模:3个shard、3个config server、3个mongos **虚拟机一** | 角色 | IP | 端口 | 副本集名称 | | ------------- | --------------- | ---- | ------------- | | mongos | 192.168.157.131 | 7017 | | | config server | 192.168.157.131 | 7000 | config_server | | shard1 | 192.168.157.131 | 7001 | shard_server1 | | shard2 | 192.168.157.131 | 7002 | shard_server2 | | shard3 | 192.168.157.131 | 7003 | shard_server3 | **虚拟机二** | 角色 | IP | 端口 | 副本集名称 | | ------------- | --------------- | ---- | ------------- | | mongos | 192.168.157.132 | 7017 | | | config server | 192.168.157.132 | 7000 | config_server | | shard1 | 192.168.157.132 | 7001 | shard_server1 | | shard2 | 192.168.157.132 | 7002 | shard_server2 | | shard3 | 192.168.157.132 | 7003 | shard_server3 | **虚拟机三** | 角色 | IP | 端口 | 副本集名称 | | ------------- | --------------- | ---- | ------------- | | mongos | 192.168.157.133 | 7017 | | | config server | 192.168.157.133 | 7000 | config_server | | shard1 | 192.168.157.133 | 7001 | shard_server1 | | shard2 | 192.168.157.133 | 7002 | shard_server2 | | shard3 | 192.168.157.133 | 7003 | shard_server3 | ## 2. Ansible文件结构 执行以下命令将相关文件下载至任意目录 ```shell git clone https://gitee.com/brianchou/ansible-mongodb-cluster.git ``` 相关文件如下所示 ```shell ansible-mongodb-cluster/ ├── roles │   ├── hosts │   └── mongodb │   ├── files │   │   ├── mongodb_version │   │   ├── mongodb-linux-x86_64-rhel70-4.2.14.tgz │   │   └── mongo.key │   ├── tasks │   │   ├── init_servers.yml │   │   ├── main.yml │   │   └── setup.yml │   ├── templates │   │   ├── config.conf.j2 │   │   ├── mongodb.j2 │   │   ├── mongos.conf.j2 │   │   ├── passwd.j2 │   │   ├── shard1.conf.j2 │   │   ├── shard2.conf.j2 │   │   └── shard3.conf.j2 │   └── vars │   └── main.yml └── site.yml 6 directories, 16 files ``` mongodb 版本:`mongodb-linux-x86_64-rhel70-4.2.14`,到[MongoDB官网](https://www.mongodb.com/try/download/community?tck=docs_server)下载二进制文件`mongodb-linux-x86_64-rhel70-4.2.14.tgz`并上传至files目录下 ### 2.1 hosts 此文件的内容是mongodb集群的主机信息,需要将此内容添加至`/etc/ansible/hosts`中,可根据实际情况修改IP,其余的不能改 ```shell [mongosrvs] 192.168.157.131 init_cluster=true 192.168.157.132 192.168.157.133 [mongosrvs:vars] node1=192.168.157.131 node2=192.168.157.132 node3=192.168.157.133 ``` ### 2.2 mongo.key 启用访问控制时会用到,也可以在ansible主机上重新生成一个,并拷贝到files目录下。 ```shell openssl rand -base64 756 > mongo.key ``` ### 2.3 tasks `setup.yml`主要是集群初始化的准备工作,创建账号,系统优化,拷贝文件,解压二进制mongodb文件等,最后一步是重启目标主机。 `init_servers.yml` 主要是初始化相关集群,并创建`root`账号。 `main.yml`则是两个任务的集合,表示先执行`setup.yml`然后再执行`init_servers.yml` ### 2.4 templates 此目录下存放相关模板文件 #### 2.4.1 mongodb.j2 `cat -A mongodb.j2` 查看是否含有`^M` 如果存在则处理 注意:`^M要用 Ctrl + V Ctrl + M 来输入` ```shell sed -i 's/^M//g' mongodb.j2 ``` ### 2.5 vars 除了`hosts`中定义的4个变量外,其它变量均定义在`main.yml`中,端口号,数据目录,日志目录可根据实际情况修改,注:如果mongodb版本不是4.2.14 则需要将`mongodb_version` 改成对应的版本 ```yml --- cache_size_gb: 0.5 mongodb_version: 4.2.14 mongodb_user: mongodb shard1_port: 7001 shard2_port: 7002 shard3_port: 7003 config_port: 7000 mongos_port: 7017 shard1_name: shard_server1 shard2_name: shard_server2 shard3_name: shard_server3 config_name: config_server mongos_name: mongos conf_dir: /opt/mongodb/conf shard1_dir: /opt/mongodb/shard_server1 shard2_dir: /opt/mongodb/shard_server2 shard3_dir: /opt/mongodb/shard_server3 config_dir: /opt/mongodb/config_server shard1_data_dir: /opt/mongodb/shard_server1/data shard2_data_dir: /opt/mongodb/shard_server2/data shard3_data_dir: /opt/mongodb/shard_server3/data config_data_dir: /opt/mongodb/config_server/data shard1_log_dir: /opt/mongodb/shard_server1/log shard2_log_dir: /opt/mongodb/shard_server2/log shard3_log_dir: /opt/mongodb/shard_server3/log config_log_dir: /opt/mongodb/config_server/log mongos_logdir: /opt/mongodb/mongos/log basedir: /usr/local/mongodb root_user: root root_password: 123456 ``` ## 3. 执行 ### 3.1 添加免密 ansible主机上执行 ```shell # 如果主机上已有密钥对,则不再执行ssh-keygen -f /root/.ssh/id_rsa -P '' ssh-keygen -f /root/.ssh/id_rsa -P '' # 将公钥文件拷贝至目标主机 ssh-copy-id root@192.168.157.131 ssh-copy-id root@192.168.157.132 ssh-copy-id root@192.168.157.133 ``` ### 3.2 执行playbook 在`ansible-mongodb-cluster` 目录下执行,此次执行的控制台输出已经保存至`process.log` ```shell ansible-playbook site.yml ``` 验证: ```shell [root@brian-node-04 ~]# mongo --port 7017 MongoDB shell version v4.2.14 connecting to: mongodb://127.0.0.1:7017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("77d2a43a-ec9a-4a22-b178-4788acf8ac72") } MongoDB server version: 4.2.14 mongos> use admin; switched to db admin mongos> db.auth('root','123456') 1 mongos> sh.status(); --- Sharding Status --- sharding version: { "_id" : 1, "minCompatibleVersion" : 5, "currentVersion" : 6, "clusterId" : ObjectId("60bb82c2b723234f3ae32e69") } shards: { "_id" : "shard_server1", "host" : "shard_server1/192.168.157.131:7001,192.168.157.132:7001,192.168.157.133:7001", "state" : 1 } { "_id" : "shard_server2", "host" : "shard_server2/192.168.157.131:7002,192.168.157.132:7002,192.168.157.133:7002", "state" : 1 } { "_id" : "shard_server3", "host" : "shard_server3/192.168.157.131:7003,192.168.157.132:7003,192.168.157.133:7003", "state" : 1 } active mongoses: "4.2.14" : 3 autosplit: Currently enabled: yes balancer: Currently enabled: yes Currently running: no Failed balancer rounds in last 5 attempts: 0 Migration Results for the last 24 hours: 682 : Success databases: { "_id" : "config", "primary" : "config", "partitioned" : true } config.system.sessions shard key: { "_id" : 1 } unique: false balancing: true chunks: shard_server1 342 shard_server2 341 shard_server3 341 too many chunks to print, use verbose if you want to force print mongos> ``` ### 3.3 存在的问题 在`Reboot all nodes`步骤时,有时候会卡很久,目标主机都已经重启了,但是ansible这边没反馈,这个时候结束进程,并注释tasks目录下`main.yml`中的以下内容,然后再执行`ansible-playbook site.yml` ```yml - name: setup environment include: setup.yml ```