docker部署mongodb集群
时间: 2023-11-09 10:09:30 浏览: 119
要在Docker中部署MongoDB集群,可以按照以下步骤进行:
1. 创建一个Docker网络,以便容器之间可以相互通信。
```
docker network create mongo-cluster
```
2. 运行三个MongoDB容器,分别作为Primary、Secondary和Arbiter节点。其中,Primary和Secondary节点需要挂载一个数据卷,以便数据可以持久化保存。Arbiter节点不需要数据卷。
```
docker run -d --name mongo-primary -p 27017:27017 -v /data/mongo-primary:/data/db --net mongo-cluster mongo --replSet rs0
docker run -d --name mongo-secondary -p 27018:27017 -v /data/mongo-secondary:/data/db --net mongo-cluster mongo --replSet rs0
docker run -d --name mongo-arbiter --net mongo-cluster mongo --replSet rs0 --smallfiles --nojournal
```
3. 进入Primary节点的容器,并初始化副本集。
```
docker exec -it mongo-primary bash
mongo
rs.initiate({_id: "rs0", members: [{_id: 0, host: "mongo-primary:27017"}, {_id: 1, host: "mongo-secondary:27017"}, {_id: 2, host: "mongo-arbiter:27017", arbiterOnly: true}]})
```
4. 在Secondary节点的容器中,执行以下命令,将其加入到副本集中。
```
docker exec -it mongo-secondary bash
mongo
rs.add("mongo-secondary:27017")
```
5. 验证副本集是否正常工作。
```
docker exec -it mongo-primary bash
mongo
rs.status()
```
阅读全文
相关推荐















