Day12 as #100DaysofMongoDB as 100daysofcode
After Day11, where we saw how to deploy a database in kubernetes environment using both deployment as well as statefulsets model of deployments.
Let’s understand how replica sets are created in kubernetes environment and how to access these replica sets.
The replica sets in the kubernetes environment can be accessed both inside and outside of the cluster.
Creating a replica set
Once all the pods are up and running, do
kubectl exec -it <pod-name> mongo/mongosh/bash
do the following steps:
1. rs.initiate()
2. rs.add("mongo-1.mongo")
3. rs.add("mongo-2.mongo")
4. rs.status()
The output will look like:
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2022-03-22T13:48:33.664Z"),
"myState" : 1,
"term" : NumberLong(23),
"syncSourceHost" : "",
"syncSourceId" : -1,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 3,
"writeMajorityCount" : 3,
"votingMembersCount" : 4,
"writableVotingMembersCount" : 4,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
"appliedOpTime" : {
"ts" : Timestamp(1647956913, 1),
"t" : NumberLong(23)
},
"durableOpTime" : {
"ts" : Timestamp(1647956913, 1),
"t" : NumberLong(23)
},
"lastAppliedWallTime" : ISODate("2022-03-22T13:48:33.284Z"),
"lastDurableWallTime" : ISODate("2022-03-22T13:48:33.284Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1647952973, 1),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2022-03-22T13:48:13.230Z"),
"electionTerm" : NumberLong(23),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1647953024, 1),
"t" : NumberLong(21)
},
"numVotesNeeded" : 3,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"numCatchUpOps" : NumberLong(0),
"newTermStartDate" : ISODate("2022-03-22T13:48:13.268Z")
},
"members" : [
{
"_id" : 0,
"name" : "mongo-0.mongo:27017",
"health" : 0,
"state" : 6,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastAppliedWallTime" : ISODate("1970-01-01T00:00:00Z"),
"lastDurableWallTime" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2022-03-22T13:48:33.285Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"authenticated" : false,
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : -1,
"configTerm" : -1
},
{
"_id" : 1,
"name" : "mongo-1.mongo:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 39,
"optime" : {
"ts" : Timestamp(1647956913, 1),
"t" : NumberLong(23)
},
"optimeDate" : ISODate("2022-03-22T13:48:33Z"),
"lastAppliedWallTime" : ISODate("2022-03-22T13:48:33.284Z"),
"lastDurableWallTime" : ISODate("2022-03-22T13:48:33.284Z"),
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"electionTime" : Timestamp(1647956893, 1),
"electionDate" : ISODate("2022-03-22T13:48:13Z"),
"configVersion" : 25,
"configTerm" : 23,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 2,
"name" : "mongo-2.mongo:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 34,
"optime" : {
"ts" : Timestamp(1647953024, 1),
"t" : NumberLong(21)
},
"optimeDurable" : {
"ts" : Timestamp(1647953024, 1),
"t" : NumberLong(21)
},
"optimeDate" : ISODate("2022-03-22T12:43:44Z"),
"optimeDurableDate" : ISODate("2022-03-22T12:43:44Z"),
"lastAppliedWallTime" : ISODate("2022-03-22T12:43:44.038Z"),
"lastDurableWallTime" : ISODate("2022-03-22T12:43:44.038Z"),
"lastHeartbeat" : ISODate("2022-03-22T13:48:33.286Z"),
"lastHeartbeatRecv" : ISODate("2022-03-22T13:48:31.802Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "mongo-1.mongo:27017",
"syncSourceId" : 4,
"infoMessage" : "",
"configVersion" : 25,
"configTerm" : 23
},
{
"_id" : 3,
"name" : "mongo-3.mongo:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 30,
"optime" : {
"ts" : Timestamp(1647953024, 1),
"t" : NumberLong(21)
},
"optimeDurable" : {
"ts" : Timestamp(1647953024, 1),
"t" : NumberLong(21)
},
"optimeDate" : ISODate("2022-03-22T12:43:44Z"),
"optimeDurableDate" : ISODate("2022-03-22T12:43:44Z"),
"lastAppliedWallTime" : ISODate("2022-03-22T12:43:44.038Z"),
"lastDurableWallTime" : ISODate("2022-03-22T12:43:44.038Z"),
"lastHeartbeat" : ISODate("2022-03-22T13:48:33.285Z"),
"lastHeartbeatRecv" : ISODate("2022-03-22T13:48:32.307Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncSourceHost" : "mongo-1.mongo:27017",
"syncSourceId" : 4,
"infoMessage" : "",
"configVersion" : 25,
"configTerm" : 23
}
],
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1647956913, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1647956913, 1)
}
rs0:PRIMARY>
This setup consists of one Primary and two secondaries.
Now to access these replica sets.
Access the replica sets within the same cluster.
aasawari.sahasrabuddhe@Aasawaris-MacBook-Pro kubernetes-mongodb % kubectl run mongo --rm -it --image mongo -- sh
If you don't see a command prompt, try pressing enter.
#
# mongo mongodb://mongo-0.mongo,mongo-1.mongo,mongo-2.mongo
MongoDB shell version v5.0.6
connecting to: mongodb://mongo-0.mongo:27017,mongo-1.mongo:27017,mongo-2.mongo:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("847d3424-dc49-4242-8443-effdbf732682") }
MongoDB server version: 5.0.6
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
https://community.mongodb.com
---
The server generated these startup warnings when booting:
2022-03-22T13:47:54.264+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2022-03-22T13:47:54.797+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2022-03-22T13:47:54.797+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()
---
rs0:PRIMARY> cfg.members
[
{
"_id" : 0,
"host" : "mongo-0.mongo:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongo-1.mongo:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongo-2.mongo:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "mongo-3.mongo:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"secondaryDelaySecs" : NumberLong(0),
"votes" : 1
}
]
rs0:PRIMARY>
Accessing the MongoDb outside the cluster
In this case, the pods are needed to be exposed as LoadBalancers and then one can access the replica set outside the kubernetes cluster
kubectl expose pod mongo-0 27017 --target 27017 --type LoadBalancer
kubectl expose pod mongo-1 27017 --target 27017 --type LoadBalancer`
kubectl expose pod mongo-3 27017 --target 27017 --type LoadBalancer`
After the three services are up, the
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d7h
service/mongo ClusterIP None <none> 27017/TCP 3d5h
service/mongo-0 LoadBalancer 10.100.162.172 172.42.43.200 27017:31701/TCP 3h45m
service/mongo-1 LoadBalancer 10.109.181.110 172.42.43.208 27017:31111/TCP 3h44m
service/mongo-2 LoadBalancer 10.104.155.2 172.42.43.289 27017:30028/TCP 3h44m
service/mongo-nodeport-svc NodePort 10.101.236.24 <none> 27017:32000/TCP 7d
And then you can access the three replica sets, you can access it using the following url
mongo mongodb://172.42.43.200:31701,172.42.43.208:31111,172.42.43.289:30028
Hence these are the two ways where you can connect to the MongoDB replica set and explore more.
Let me know if you have suggestions and views on the above topics.
Thanks
Aasawari
Share on twitter: https://twitter.com/Aasawari_24