使用docker部署mongo集群

MongoDB 的 Docker 常用命令

Docker下,实战mongodb副本集(Replication)

# 下载镜像
docker pull mongo

# 创建并运行单节点实例
docker run -p 27017:27017 -v ~/code/Tools/mongo/data:/data/db --name mongodb -d mongo
  • 基于docker network创建局域网
# 查看已有network列表
docker network ls
# 移除已创建的网络
docker network rm mongo-network

# 初始化 Swarm
docker swarm init
docker swarm join-token manager

# 创建桥接网络,在网段 172.28.0.0/16 上创建网关为 172.28.5.254 、IP范围为 172.28.5.0/24 的桥接网络
docker network create \
  --driver=bridge \
  --subnet=172.28.0.0/16 \
  --ip-range=172.28.5.0/24 \
  --gateway=172.28.5.254 \
  br0
# 查看网络详情
docker network inspect br0
  • 创建并部署集群实例
# 创建主节点,并加入mongo-network网络,指定ip;并将mongo监听绑定到ip上
docker run --name m0 -p 27017:27017 \
--network br0 \
--ip 172.28.5.100 \
-v ~/code/Tools/mongo/data0:/data/db \
-d mongo /bin/bash -c 'mongod --replSet replset0 --bind_ip_all'

# 创建复制节点,并加入mongo-network网络,指定ip;并将mongo监听绑定到ip上;指定不同端口
docker run --name m1 -p 27018:27018 \
--network br0 \
--ip 172.28.5.101 \
-v ~/code/Tools/mongo/data1:/data/db \
-d mongo /bin/bash -c 'mongod --replSet replset0 --bind_ip_all --port 27018'

# 查看节点ip
docker exec m0 cat /etc/hosts
docker exec m1 cat /etc/hosts
# 配置集群
docker exec -it m0 bash
# 执行命令进行mongo控制台
mongo 172.28.5.100:27017
# 使用admin数据库
use admin
# 添加配置
config = { _id:"replset0", members:[{_id:0,host:"172.28.5.100:27017"},{_id:1,host:"172.28.5.101:27018"}]}
rs.initiate(config)
# 返回如下结果
{
  "ok" : 1,
  "operationTime" : Timestamp(1556158794, 1),
  "$clusterTime" : {
    "clusterTime" : Timestamp(1556158794, 1),
    "signature" : {
      "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
      "keyId" : NumberLong(0)
    }
  }
}
# 进入副本节点
docker exec -it m1 mongo 172.28.5.101:27018
# 允许副本节点进行读操作
db.getMongo().setSlaveOk()

# 查看状态
rs.status()

最终结果

{
	"set" : "replset0",
	"date" : ISODate("2019-04-26T09:14:56.070Z"),
	"myState" : 2,
	"term" : NumberLong(3),
	"syncingTo" : "172.28.5.101:27018",
	"syncSourceHost" : "172.28.5.101:27018",
	"syncSourceId" : 1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1556270095, 1),
			"t" : NumberLong(3)
		},
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1556270095, 1),
			"t" : NumberLong(3)
		},
		"appliedOpTime" : {
			"ts" : Timestamp(1556270095, 1),
			"t" : NumberLong(3)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1556270095, 1),
			"t" : NumberLong(3)
		}
	},
	"lastStableCheckpointTimestamp" : Timestamp(1556190289, 1),
	"members" : [
		{
			"_id" : 0,
			"name" : "172.28.5.100:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 68,
			"optime" : {
				"ts" : Timestamp(1556270095, 1),
				"t" : NumberLong(3)
			},
			"optimeDate" : ISODate("2019-04-26T09:14:55Z"),
			"syncingTo" : "172.28.5.101:27018",
			"syncSourceHost" : "172.28.5.101:27018",
			"syncSourceId" : 1,
			"infoMessage" : "",
			"configVersion" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "172.28.5.101:27018",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 41,
			"optime" : {
				"ts" : Timestamp(1556270085, 1),
				"t" : NumberLong(3)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1556270085, 1),
				"t" : NumberLong(3)
			},
			"optimeDate" : ISODate("2019-04-26T09:14:45Z"),
			"optimeDurableDate" : ISODate("2019-04-26T09:14:45Z"),
			"lastHeartbeat" : ISODate("2019-04-26T09:14:55.413Z"),
			"lastHeartbeatRecv" : ISODate("2019-04-26T09:14:54.927Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncingTo" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1556270064, 1),
			"electionDate" : ISODate("2019-04-26T09:14:24Z"),
			"configVersion" : 1
		}
	],
	"ok" : 1,
	"operationTime" : Timestamp(1556270095, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1556270095, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}
  • docker 外部访问

本地,非docker环境可使用 mongodb://localhost:27017 访问主节点,使用 mongodb://localhost:27018 访问复制节点

你可能感兴趣的:(运维)