본문 바로가기

리눅스

[리눅스] 도커 컨테이너로 몽고디비 클러스터 구성하기(mongodb shard cluster)

반응형

도커 컨테이너로 몽고디비 클러스터 구성하기(mongodb shard cluster)

MongoDB의 복제

출처-https://www.mongodb.com/docs/v4.4/replication/

Primary with Two Secondary Members (PSS)

출처-https://www.mongodb.com/docs/v4.4/images/replica-set-primary-with-two-secondaries.bakedsvg.svg

 

Primary with a Secondary and an Arbiter (PSA)

출처-https://www.mongodb.com/docs/v4.4/images/replica-set-primary-with-secondary-and-arbiter.bakedsvg.svg

 

테스트 환경

$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
$ docker version
Client: Docker Engine - Community
 Version:           20.10.21
 API version:       1.41
 Go version:        go1.18.7
 Git commit:        baeda1f
 Built:             Tue Oct 25 18:04:24 2022
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.21
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.18.7
  Git commit:       3056208
  Built:            Tue Oct 25 18:02:38 2022
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          1.6.9
  GitCommit:        1c90a442489720eec95342e1789ee8a5e1b9536f
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
$ docker-compose version
docker-compose version 1.29.2, build 5becea4c
docker-py version: 5.0.0
CPython version: 3.7.10
OpenSSL version: OpenSSL 1.1.0l  10 Sep 2019

몽고디비 클러스터 구성하기(mongodb shard cluster)

출처-https://player.slideplayer.com/80/13419974/slides/slide_4.jpg

MongoDB dockerhub : https://hub.docker.com/_/mongo

*** avx 미지원으로 mongo:4.4.18 버전으로 구성하였습니다.

github clone

git clone https://github.com/anti1346/docker-mongodb-sharded-cluster.git
cd docker-mongodb-sharded-cluster

sharded-cluster 컨테이너 실행

docker-compose up -d

출처-https://orproduction.wpenginepowered.com/wp-content/uploads/2018/01/MongoSitecoreSharded-1.png

Config Servers 초기화(Initialize Config Servers)

docker-compose exec config1 bash
mongo
rs.initiate( {
	_id: "configReplica",
	configsvr: true,
	members: [
		{ "_id": 0, "host": "config1:27017" },
		{ "_id": 1, "host": "config2:27017" },
		{ "_id": 2, "host": "config3:27017" }]
	} )
> rs.initiate( {
... _id: "configReplica",
... configsvr: true,
... members: [
... { "_id": 0, "host": "config1:27017" },
... { "_id": 1, "host": "config2:27017" },
... { "_id": 2, "host": "config3:27017" }]
... } )
{
	"ok" : 1,
	"$gleStats" : {
		"lastOpTime" : Timestamp(1672560894, 1),
		"electionId" : ObjectId("000000000000000000000000")
	},
	"lastCommittedOpTime" : Timestamp(0, 0)
}
quit()
mongo
rs.hello()
rs1:PRIMARY> rs.hello()
{
	"topologyVersion" : {
		"processId" : ObjectId("63b1643379e6282bb4338d27"),
		"counter" : NumberLong(8)
	},
	"hosts" : [
		"shard11:27017",
		"shard12:27017"
	],
	"arbiters" : [
		"shard13:27017"
	],
	"setName" : "rs1",
	"setVersion" : 2,
	"isWritablePrimary" : true,
	"secondary" : false,
	"primary" : "shard11:27017",
	"me" : "shard11:27017",
	"electionId" : ObjectId("7fffffff0000000000000001"),
	"lastWrite" : {
		"opTime" : {
			"ts" : Timestamp(1672574840, 2),
			"t" : NumberLong(1)
		},
		"lastWriteDate" : ISODate("2023-01-01T12:07:20Z"),
		"majorityOpTime" : {
			"ts" : Timestamp(1672574840, 2),
			"t" : NumberLong(1)
		},
		"majorityWriteDate" : ISODate("2023-01-01T12:07:20Z")
	},
	"maxBsonObjectSize" : 16777216,
	"maxMessageSizeBytes" : 48000000,
	"maxWriteBatchSize" : 100000,
	"localTime" : ISODate("2023-01-01T12:07:21.365Z"),
	"logicalSessionTimeoutMinutes" : 30,
	"connectionId" : 112,
	"minWireVersion" : 0,
	"maxWireVersion" : 9,
	"readOnly" : false,
	"ok" : 1,
	"$gleStats" : {
		"lastOpTime" : Timestamp(0, 0),
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1672574840, 2),
	"$configServerState" : {
		"opTime" : {
			"ts" : Timestamp(1672574831, 2),
			"t" : NumberLong(1)
		}
	},
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672574840, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1672574840, 2)
}
rs.config()
configReplica:PRIMARY> rs.config()
{
	"_id" : "configReplica",
	"version" : 1,
	"term" : 1,
	"configsvr" : true,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "config1:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "config2:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "config3:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {

		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("63b1647e85c93578404b3d5d")
	}
}
rs.status()
configReplica:PRIMARY> rs.status()
{
	"set" : "configReplica",
	"date" : ISODate("2023-01-01T10:47:08.221Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"configsvr" : true,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 3,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1672570027, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1672570027, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1672570027, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1672570027, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
		"lastDurableWallTime" : ISODate("2023-01-01T10:47:07.438Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1672569994, 4),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2023-01-01T10:46:34.187Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1672569983, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2023-01-01T10:46:34.413Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2023-01-01T10:46:35.997Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "config1:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 122,
			"optime" : {
				"ts" : Timestamp(1672570027, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:47:07Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1672569994, 1),
			"electionDate" : ISODate("2023-01-01T10:46:34Z"),
			"configVersion" : 1,
			"configTerm" : 1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "config2:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 44,
			"optime" : {
				"ts" : Timestamp(1672570025, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1672570025, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:47:05Z"),
			"optimeDurableDate" : ISODate("2023-01-01T10:47:05Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
			"lastHeartbeat" : ISODate("2023-01-01T10:47:06.247Z"),
			"lastHeartbeatRecv" : ISODate("2023-01-01T10:47:07.793Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "config1:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		},
		{
			"_id" : 2,
			"name" : "config3:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 44,
			"optime" : {
				"ts" : Timestamp(1672570025, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1672570025, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:47:05Z"),
			"optimeDurableDate" : ISODate("2023-01-01T10:47:05Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:47:07.438Z"),
			"lastHeartbeat" : ISODate("2023-01-01T10:47:06.247Z"),
			"lastHeartbeatRecv" : ISODate("2023-01-01T10:47:07.793Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "config1:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : 1
		}
	],
	"ok" : 1,
	"$gleStats" : {
		"lastOpTime" : Timestamp(0, 0),
		"electionId" : ObjectId("7fffffff0000000000000001")
	},
	"lastCommittedOpTime" : Timestamp(1672570027, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672570027, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1672570027, 1)
}
use admin
> use admin
switched to db admin
db.createUser({user:"root",pwd:"changeme",roles:[{role:"root",db:"admin"}]});
configReplica:PRIMARY> db.createUser({user:"root",pwd:"changeme",roles:[{role:"root",db:"admin"}]});
Successfully added user: {
	"user" : "root",
	"roles" : [
		{
			"role" : "root",
			"db" : "admin"
		}
	]
}
quit()
mongo --port 27017 -u "root" -p "changeme" --authenticationDatabase "admin"
728x90

 

Shard Servers 초기화(Initialize Shard Servers)

docker-compose exec shard11 bash
mongo
rs.initiate({
    _id: "rs1",
    members: [
        { _id: 0, host : "shard11:27017" },
        { _id: 1, host : "shard12:27017" },
        { _id: 2, host : "shard13:27017", arbiterOnly:true }
    ]
})
> rs.initiate({
...     _id: "rs1",
...     members: [
...         { _id: 0, host : "shard11:27017" },
...         { _id: 1, host : "shard12:27017" },
...         { _id: 2, host : "shard13:27017", arbiterOnly:true }
...     ]
... })
{ "ok" : 1 }
rs.config()
rs1:PRIMARY> rs.config()
{
	"_id" : "rs1",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "shard11:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "shard12:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "shard13:27017",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {

		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("63b1668479e6282bb4338d39")
	}
}
rs.status()
rs1:PRIMARY> rs.status()
{
	"set" : "rs1",
	"date" : ISODate("2023-01-01T10:56:26.706Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1672570582, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1672570582, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1672570582, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1672570582, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
		"lastDurableWallTime" : ISODate("2023-01-01T10:56:22.460Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1672570572, 1),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2023-01-01T10:55:12.190Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1672570500, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2023-01-01T10:55:12.386Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2023-01-01T10:55:13.029Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "shard11:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 679,
			"optime" : {
				"ts" : Timestamp(1672570582, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:56:22Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "Could not find member to sync from",
			"electionTime" : Timestamp(1672570512, 1),
			"electionDate" : ISODate("2023-01-01T10:55:12Z"),
			"configVersion" : 1,
			"configTerm" : -1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "shard12:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 85,
			"optime" : {
				"ts" : Timestamp(1672570582, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1672570582, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:56:22Z"),
			"optimeDurableDate" : ISODate("2023-01-01T10:56:22Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:56:22.460Z"),
			"lastHeartbeat" : ISODate("2023-01-01T10:56:26.238Z"),
			"lastHeartbeatRecv" : ISODate("2023-01-01T10:56:24.898Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "shard11:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : -1
		},
		{
			"_id" : 2,
			"name" : "shard13:27017",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 85,
			"lastHeartbeat" : ISODate("2023-01-01T10:56:26.234Z"),
			"lastHeartbeatRecv" : ISODate("2023-01-01T10:56:25.201Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : -1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672570582, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1672570582, 1)
}

---

docker-compose exec shard21 bash
mongo

 

rs.initiate({
    _id: "rs2",
    members: [
        { _id: 0, host : "shard21:27017" },
        { _id: 1, host : "shard22:27017" },
        { _id: 2, host : "shard23:27017", arbiterOnly:true }
    ]
})
> rs.initiate({
...     _id: "rs2",
...     members: [
...         { _id: 0, host : "shard21:27017" },
...         { _id: 1, host : "shard22:27017" },
...         { _id: 2, host : "shard23:27017", arbiterOnly:true }
...     ]
... })
{ "ok" : 1 }

 

rs.config()
rs2:SECONDARY> rs.config()
{
	"_id" : "rs2",
	"version" : 1,
	"protocolVersion" : NumberLong(1),
	"writeConcernMajorityJournalDefault" : true,
	"members" : [
		{
			"_id" : 0,
			"host" : "shard21:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 1,
			"host" : "shard22:27017",
			"arbiterOnly" : false,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 1,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		},
		{
			"_id" : 2,
			"host" : "shard23:27017",
			"arbiterOnly" : true,
			"buildIndexes" : true,
			"hidden" : false,
			"priority" : 0,
			"tags" : {

			},
			"slaveDelay" : NumberLong(0),
			"votes" : 1
		}
	],
	"settings" : {
		"chainingAllowed" : true,
		"heartbeatIntervalMillis" : 2000,
		"heartbeatTimeoutSecs" : 10,
		"electionTimeoutMillis" : 10000,
		"catchUpTimeoutMillis" : -1,
		"catchUpTakeoverDelayMillis" : 30000,
		"getLastErrorModes" : {

		},
		"getLastErrorDefaults" : {
			"w" : 1,
			"wtimeout" : 0
		},
		"replicaSetId" : ObjectId("63b167478cb7e82a74eff243")
	}
}

 

use config
db.shards.find()
mongos> db.shards.find()
{ "_id" : "rs1", "host" : "rs1/shard11:27017,shard12:27017", "state" : 1 }
{ "_id" : "rs2", "host" : "rs2/shard21:27017,shard22:27017", "state" : 1 }

 

rs.status()
rs2:PRIMARY> rs.status()
{
	"set" : "rs2",
	"date" : ISODate("2023-01-01T10:59:05.870Z"),
	"myState" : 1,
	"term" : NumberLong(1),
	"syncSourceHost" : "",
	"syncSourceId" : -1,
	"heartbeatIntervalMillis" : NumberLong(2000),
	"majorityVoteCount" : 2,
	"writeMajorityCount" : 2,
	"votingMembersCount" : 3,
	"writableVotingMembersCount" : 2,
	"optimes" : {
		"lastCommittedOpTime" : {
			"ts" : Timestamp(1672570737, 1),
			"t" : NumberLong(1)
		},
		"lastCommittedWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
		"readConcernMajorityOpTime" : {
			"ts" : Timestamp(1672570737, 1),
			"t" : NumberLong(1)
		},
		"readConcernMajorityWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
		"appliedOpTime" : {
			"ts" : Timestamp(1672570737, 1),
			"t" : NumberLong(1)
		},
		"durableOpTime" : {
			"ts" : Timestamp(1672570737, 1),
			"t" : NumberLong(1)
		},
		"lastAppliedWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
		"lastDurableWallTime" : ISODate("2023-01-01T10:58:57.493Z")
	},
	"lastStableRecoveryTimestamp" : Timestamp(1672570707, 4),
	"electionCandidateMetrics" : {
		"lastElectionReason" : "electionTimeout",
		"lastElectionDate" : ISODate("2023-01-01T10:58:27.234Z"),
		"electionTerm" : NumberLong(1),
		"lastCommittedOpTimeAtElection" : {
			"ts" : Timestamp(0, 0),
			"t" : NumberLong(-1)
		},
		"lastSeenOpTimeAtElection" : {
			"ts" : Timestamp(1672570695, 1),
			"t" : NumberLong(-1)
		},
		"numVotesNeeded" : 2,
		"priorityAtElection" : 1,
		"electionTimeoutMillis" : NumberLong(10000),
		"numCatchUpOps" : NumberLong(0),
		"newTermStartDate" : ISODate("2023-01-01T10:58:27.420Z"),
		"wMajorityWriteAvailabilityDate" : ISODate("2023-01-01T10:58:28.841Z")
	},
	"members" : [
		{
			"_id" : 0,
			"name" : "shard21:27017",
			"health" : 1,
			"state" : 1,
			"stateStr" : "PRIMARY",
			"uptime" : 839,
			"optime" : {
				"ts" : Timestamp(1672570737, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:58:57Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"electionTime" : Timestamp(1672570707, 1),
			"electionDate" : ISODate("2023-01-01T10:58:27Z"),
			"configVersion" : 1,
			"configTerm" : -1,
			"self" : true,
			"lastHeartbeatMessage" : ""
		},
		{
			"_id" : 1,
			"name" : "shard22:27017",
			"health" : 1,
			"state" : 2,
			"stateStr" : "SECONDARY",
			"uptime" : 50,
			"optime" : {
				"ts" : Timestamp(1672570737, 1),
				"t" : NumberLong(1)
			},
			"optimeDurable" : {
				"ts" : Timestamp(1672570737, 1),
				"t" : NumberLong(1)
			},
			"optimeDate" : ISODate("2023-01-01T10:58:57Z"),
			"optimeDurableDate" : ISODate("2023-01-01T10:58:57Z"),
			"lastAppliedWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
			"lastDurableWallTime" : ISODate("2023-01-01T10:58:57.493Z"),
			"lastHeartbeat" : ISODate("2023-01-01T10:59:05.276Z"),
			"lastHeartbeatRecv" : ISODate("2023-01-01T10:59:04.686Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "shard21:27017",
			"syncSourceId" : 0,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : -1
		},
		{
			"_id" : 2,
			"name" : "shard23:27017",
			"health" : 1,
			"state" : 7,
			"stateStr" : "ARBITER",
			"uptime" : 50,
			"lastHeartbeat" : ISODate("2023-01-01T10:59:05.277Z"),
			"lastHeartbeatRecv" : ISODate("2023-01-01T10:59:03.932Z"),
			"pingMs" : NumberLong(0),
			"lastHeartbeatMessage" : "",
			"syncSourceHost" : "",
			"syncSourceId" : -1,
			"infoMessage" : "",
			"configVersion" : 1,
			"configTerm" : -1
		}
	],
	"ok" : 1,
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672570737, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	},
	"operationTime" : Timestamp(1672570737, 1)
}

Mongos(router) Servers 초기화(Initialize Mongos Servers)

docker-compose exec mongos1 bash
mongo

rs1 shard 추가

sh.addShard("rs1/shard11:27017,shard12:27017,shard13:27017")
mongos> sh.addShard("rs1/shard11:27017,shard12:27017,shard13:27017")
{
	"shardAdded" : "rs1",
	"ok" : 1,
	"operationTime" : Timestamp(1672572579, 4),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672572579, 4),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

rs2 shard 추가

sh.addShard("rs2/shard21:27017,shard22:27017,shard23:27017")
mongos> sh.addShard("rs2/shard21:27017,shard22:27017,shard23:27017")
{
	"shardAdded" : "rs2",
	"ok" : 1,
	"operationTime" : Timestamp(1672572616, 2),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672572616, 2),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

 

sh.status()
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("63b1648b85c93578404b3deb")
  }
  shards:
        {  "_id" : "rs1",  "host" : "rs1/shard11:27017,shard12:27017",  "state" : 1 }
        {  "_id" : "rs2",  "host" : "rs2/shard21:27017,shard22:27017",  "state" : 1 }
  active mongoses:
        "4.4.18" : 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:
                25 : Success
  databases:
        {  "_id" : "config",  "primary" : "config",  "partitioned" : true }
                config.system.sessions
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                rs1	999
                                rs2	25
                        too many chunks to print, use verbose if you want to force print

Mongo Express

웹 브라우저 : http://localhost:8081

config > shards

config > mongos

 

router(mongos) 접속 후 데이터 삽입 테스트

docker-compose exec mongos3 bash
mongo

샤드 데이터베이스 테스트 생성

sh.enableSharding("test")
mongos> sh.enableSharding("test")
{
	"ok" : 1,
	"operationTime" : Timestamp(1672574367, 19),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672574367, 24),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

샤드에 컬렉션 추가 및 샤드 필드 설정

sh.shardCollection("test.user", {"_id": "hashed" })
mongos> sh.shardCollection("test.user", {"_id": "hashed" })
{
	"collectionsharded" : "test.user",
	"collectionUUID" : UUID("9caa698c-0ce6-48c3-87e3-64055e0d2986"),
	"ok" : 1,
	"operationTime" : Timestamp(1672574398, 8),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1672574398, 8),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

샤드 라이브러리 전환

use test
mongos> use test
switched to db test

데이터 삽입

for (i = 1; i <= 1000; i=i+1){db.user.insert({'userIndex': 1})}
mongos> for (i = 1; i <= 1000; i=i+1){db.user.insert({'userIndex': 1})}
WriteResult({ "nInserted" : 1 })

데이터 확인

db.getCollection('user').find({}).count()
mongos> db.getCollection('user').find({}).count()
1000

 

 

 

참고URL

- mongodb(config) : https://sangchul.kr/312

- mongodb(shard) : https://sangchul.kr/313

- mongodb(router) : https://sangchul.kr/314

- Replication : https://www.mongodb.com/docs/v4.4/replication

- Three Member Replica Sets : https://www.mongodb.com/docs/manual/core/replica-set-architecture-three-members

- MongoDB Distributed Write and Read : https://slideplayer.com/slide/13419974/

 

728x90
반응형