Docker: Mongo Cluster Kurulumu
version: '3'
services:
mongodb1:
image: mongo:4
restart: always
container_name: mongodb1
volumes:
- mongodata1:/data/db
expose:
- "27017"
entrypoint: [ "/usr/bin/mongod", "--replSet", "rsmongo", "--bind_ip_all"]
mongodb2:
image: mongo:4
restart: always
container_name: mongodb2
volumes:
- mongodata2:/data/db
expose:
- "27017"
entrypoint: [ "/usr/bin/mongod", "--replSet", "rsmongo", "--bind_ip_all"]
mongodb3:
image: mongo:4
restart: always
container_name: mongodb3
volumes:
- mongodata3:/data/db
expose:
- "27017"
entrypoint: [ "/usr/bin/mongod", "--replSet", "rsmongo", "--bind_ip_all" ]
volumes:
mongodata1:
mongodata2:
mongodata3:
Yukarıdaki docker-compose.yaml dosyasını aşağıdaki komut ile up duruma getiriyoruz.
docker-compose up -d
Aşağıdaki komut ile containerları kontrol ediyoruz,
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7e4e96cde9f4 mongo:4 "/usr/bin/mongod --r…" 5 minutes ago Up 2 minutes 27017/tcp mongodb2
673c26d5e0e0 mongo:4 "/usr/bin/mongod --r…" 5 minutes ago Up 2 minutes 27017/tcp mongodb3
b7253224ed72 mongo:4 "/usr/bin/mongod --r…" 5 minutes ago Up 2 minutes 27017/tcp mongodb1
Aşağıdaki komut ile mongodb1 container’da mongo komutu çalıştırıp, mongo shell’e bağlanıyoruz.
docker-compose exec mongodb1 mongo
Açılan shell ‘de aşağıdaki konfigürasyon parametrelerini giriyoruz. Burada mongo cluster üyelerini(containerları) tanımlıyoruz.
rsconf = {
_id : "rsmongo",
members: [
{
"_id": 0,
"host": "mongodb1:27017",
"priority": 4
},
{
"_id": 1,
"host": "mongodb2:27017",
"priority": 2
},
{
"_id": 2,
"host": "mongodb3:27017",
"priority": 1
}
]
}
Yukarıdaki ayarlamaları yaptıktan sonra, Cluster’ı başlatmak için aşağıdaki komutu giriyoruz.
rs.initiate(rsconf);
Artık cluster devrede, İşlemler tamamlandı. Aşağıdaki komut ile nodeların durumunu görebiliriz.
rs.status()
root@ubuntu-1:/home/onur/mongo-docker-compose# docker-compose exec mongodb1 mongo
MongoDB shell version v4.4.18
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("dcfc2ae6-de43-45e6-b91f-f3be339535b5") }
MongoDB server version: 4.4.18
---
The server generated these startup warnings when booting:
2022-12-04T17:32:02.574+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-12-04T17:32:04.156+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2022-12-04T17:32:04.156+00:00: You are running this process as the root user, which is not recommended
---
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
rsmongo:PRIMARY> rs.status()
{
"set" : "rsmongo",
"date" : ISODate("2022-12-04T17:44:15.259Z"),
"myState" : 1,
"term" : NumberLong(3),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"votingMembersCount" : 3,
"writableVotingMembersCount" : 3,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"lastCommittedWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"readConcernMajorityWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"appliedOpTime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"lastAppliedWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"lastDurableWallTime" : ISODate("2022-12-04T17:44:14.393Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1670175834, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2022-12-04T17:32:14.348Z"),
"electionTerm" : NumberLong(3),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1670175116, 1),
"t" : NumberLong(2)
},
"numVotesNeeded" : 2,
"priorityAtElection" : 4,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-12-04T17:32:14.363Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2022-12-04T17:32:14.880Z")
},
"members" : [
{
"_id" : 0,
"name" : "mongodb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 733,
"optime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-12-04T17:44:14Z"),
"lastAppliedWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"lastDurableWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1670175134, 1),
"electionDate" : ISODate("2022-12-04T17:32:14Z"),
"configVersion" : 1,
"configTerm" : 3,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "mongodb2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 730,
"optime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-12-04T17:44:14Z"),
"optimeDurableDate" : ISODate("2022-12-04T17:44:14Z"),
"lastAppliedWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"lastDurableWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"lastHeartbeat" : ISODate("2022-12-04T17:44:14.570Z"),
"lastHeartbeatRecv" : ISODate("2022-12-04T17:44:13.567Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "mongodb1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 3
},
{
"_id" : 2,
"name" : "mongodb3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 730,
"optime" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1670175854, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2022-12-04T17:44:14Z"),
"optimeDurableDate" : ISODate("2022-12-04T17:44:14Z"),
"lastAppliedWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"lastDurableWallTime" : ISODate("2022-12-04T17:44:14.393Z"),
"lastHeartbeat" : ISODate("2022-12-04T17:44:14.569Z"),
"lastHeartbeatRecv" : ISODate("2022-12-04T17:44:15.078Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "mongodb1:27017",
"syncSourceId" : 0,
"infoMessage" : "",
"configVersion" : 1,
"configTerm" : 3
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1670175854, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1670175854, 1)
}
rs.conf komutu ilede cluster hakkında bilgi alabiliriz;
rsmongo:PRIMARY> rs.conf()
{
"_id" : "rsmongo",
"version" : 1,
"term" : 3,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "mongodb1:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 4,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb2:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb3: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("638cd91c0188bc7bb7858f81")
}
}
https://flowygo.com/en/blog/mongodb-and-docker-how-to-create-and-configure-a-replica-set/