美文网首页
docker mongodb集群搭建

docker mongodb集群搭建

作者: 青果山圣斗士 | 来源:发表于2021-05-12 18:22 被阅读0次

使用3台服务器,搭建一主一从一仲裁节点
服务ip如下:

10.53.253.215
10.53.253.216
10.53.253.217

每台服务使用docker镜像启动mongodb

docker run -itd --restart=always --name mongo_test -p 27001:27017 -v /data/mongodb_data:/data/db mongo:latest --replSet test

三台启动完毕,使用docker exec指令进入想成为首个master的节点

docker exec -it mongo_test mongo
MongoDB shell version v4.4.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("32cd4e58-5e46-4d9d-ac72-4ab73e523c67") }
MongoDB server version: 4.4.1
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: 
        2021-05-12T09:48:35.141+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2021-05-12T09:48:35.142+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2021-05-12T09:48:35.142+00:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
---
---
        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()
---

使用rs.initiate指令进行初始化

>config = { _id:"test", members:[{_id:0,host:"10.53.253.215:27001"},{_id:1,host:"10.53.253.216:27001"},{_id:2,host:"10.53.253.217:27001"}]}
{
    "_id" : "test",
    "members" : [
        {
            "_id" : 0,
            "host" : "10.53.253.215:27001"
        },
        {
            "_id" : 1,
            "host" : "10.53.253.216:27001"
        },
        {
            "_id" : 2,
            "host" : "10.53.253.217:27001"
        }
    ]
}
> rs.initiate(config)
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1620812938, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1620812938, 1)
}

rs.status()查看集群状态

{
    "set" : "test",
    "date" : ISODate("2021-05-12T09:49:04.155Z"),
    "myState" : 2,
    "term" : NumberLong(0),
    "syncSourceHost" : "",
    "syncSourceId" : -1,
    "heartbeatIntervalMillis" : NumberLong(2000),
    "majorityVoteCount" : 2,
    "writeMajorityCount" : 2,
    "votingMembersCount" : 3,
    "writableVotingMembersCount" : 3,
    "optimes" : {
        "lastCommittedOpTime" : {
            "ts" : Timestamp(0, 0),
            "t" : NumberLong(-1)
        },
        "lastCommittedWallTime" : ISODate("1970-01-01T00:00:00Z"),
        "appliedOpTime" : {
            "ts" : Timestamp(1620812938, 1),
            "t" : NumberLong(-1)
        },
        "durableOpTime" : {
            "ts" : Timestamp(1620812938, 1),
            "t" : NumberLong(-1)
        },
        "lastAppliedWallTime" : ISODate("2021-05-12T09:48:58.634Z"),
        "lastDurableWallTime" : ISODate("2021-05-12T09:48:58.634Z")
    },
    "lastStableRecoveryTimestamp" : Timestamp(0, 0),
    "members" : [
        {
            "_id" : 0,
            "name" : "10.53.253.215:27001",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 30,
            "optime" : {
                "ts" : Timestamp(1620812938, 1),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("2021-05-12T09:48:58Z"),
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "Could not find member to sync from",
            "configVersion" : 1,
            "configTerm" : 0,
            "self" : true,
            "lastHeartbeatMessage" : ""
        },
        {
            "_id" : 1,
            "name" : "10.53.253.216:27001",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 5,
            "optime" : {
                "ts" : Timestamp(1620812938, 1),
                "t" : NumberLong(-1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1620812938, 1),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("2021-05-12T09:48:58Z"),
            "optimeDurableDate" : ISODate("2021-05-12T09:48:58Z"),
            "lastHeartbeat" : ISODate("2021-05-12T09:49:03.657Z"),
            "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 1,
            "configTerm" : 0
        },
        {
            "_id" : 2,
            "name" : "10.53.253.217:27001",
            "health" : 1,
            "state" : 2,
            "stateStr" : "SECONDARY",
            "uptime" : 5,
            "optime" : {
                "ts" : Timestamp(1620812938, 1),
                "t" : NumberLong(-1)
            },
            "optimeDurable" : {
                "ts" : Timestamp(1620812938, 1),
                "t" : NumberLong(-1)
            },
            "optimeDate" : ISODate("2021-05-12T09:48:58Z"),
            "optimeDurableDate" : ISODate("2021-05-12T09:48:58Z"),
            "lastHeartbeat" : ISODate("2021-05-12T09:49:03.658Z"),
            "lastHeartbeatRecv" : ISODate("2021-05-12T09:49:03.799Z"),
            "pingMs" : NumberLong(0),
            "lastHeartbeatMessage" : "",
            "syncSourceHost" : "",
            "syncSourceId" : -1,
            "infoMessage" : "",
            "configVersion" : 1,
            "configTerm" : 0
        }
    ],
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1620812938, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1620812938, 1)
}

等节点同步完成,投票选出master节点,因为config配置的是三个节点,因此目前是一主二从,需将其中一个节点删掉,作为仲裁节点

rs.remove("ip:port")删除节点
rs.addArb("ip:port")增加仲裁节点

test:PRIMARY> rs.remove("10.53.253.217:27001")
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1620813102, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1620813102, 1)
}
test:PRIMARY> rs.addArb("10.53.253.217:27001")
{
    "ok" : 1,
    "$clusterTime" : {
        "clusterTime" : Timestamp(1620813119, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    },
    "operationTime" : Timestamp(1620813119, 1)
}

至此,使用docker搭建mongodb一主一从一仲裁完毕。

相关文章

网友评论

      本文标题:docker mongodb集群搭建

      本文链接:https://www.haomeiwen.com/subject/mnaadltx.html