docker部署mongo集群
Posted doufy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker部署mongo集群相关的知识,希望对你有一定的参考价值。
无聊,自建轮子
创建mongo镜像,dockerfile如下
from centos:7 RUN yum install net-tools vim -y RUN mkdir -p /mongodb/bin && mkdir -p /mongodb/conf && mkdir -p /mongodb/log && touch /mongodb/log/mongodb.log && mkdir -p /mongodb/data && chmod 755 /mongodb -R ENV MONGOD_HOME /mongodb ENV PATH $MONGOD_HOME/bin:$PATH COPY ./mongodb/bin/ /mongodb/bin/ COPY mongod.conf /mongodb/conf/mongod.conf COPY run.sh /mongodb/bin/run.sh RUN chmod a+x /mongodb/bin/run.sh CMD ["/mongodb/bin/run.sh"]
run.sh如下
#!/bin/bash echo "######启动程序##########" mongod -f /mongodb/conf/mongod.conf echo "######查看端口##########" netstat -ntulp echo "######查看进程##########" ps -ef |grep -v grep |grep mongod while true do sleep 1h done
mongod.conf如下
systemLog: destination: file path: /mongodb/log/mongodb.log logAppend: true storage: journal: enabled: true dbPath: /mongodb/data directoryPerDB: true #engine: wiredTiger wiredTiger: engineConfig: # cacheSizeGB: 1 directoryForIndexes: true collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: true processManagement: fork: true net: bindIpAll: true port: 27017 replication: oplogSizeMB: 2048 replSetName: my_repl
docker-compose.yml如下,ip说明:我单机创建的虚拟网
version: ‘3.4‘ x-logging: &default-logging options: max-size: ‘12m‘ max-file: ‘5‘ driver: json-file services: mongo1: image: mongo:v15 logging: *default-logging restart: always hostname: mongo1 networks: doufy: ipv4_address: 172.19.0.71 volumes: - /etc/localtime:/etc/localtime:ro - /data/mongo/1/data:/mongodb/data - /data/mongo/1/log:/mongodb/log mongo2: image: mongo:v15 logging: *default-logging restart: always hostname: mongo2 networks: doufy: ipv4_address: 172.19.0.72 volumes: - /etc/localtime:/etc/localtime:ro - /data/mongo/2/data:/mongodb/data - /data/mongo/2/log:/mongodb/log mongo3: image: mongo:v15 logging: *default-logging restart: always hostname: mongo3 networks: doufy: ipv4_address: 172.19.0.73 volumes: - /etc/localtime:/etc/localtime:ro - /data/mongo/3/data:/mongodb/data - /data/mongo/3/log:/mongodb/log networks: doufy: external: true
mongodb为官网下载,版本为4.2
链接 https://www.mongodb.com/download-center/enterprise
具体配置参考配置文件。
创建复制集群
登陆数据库,配置mongodb复制
shell> mongo --port 27017 config = {_id: ‘my_repl‘, members: [ {_id: 0, host: ‘172.19.0.71:27017‘}, {_id: 1, host: ‘172.19.0.72:27017‘}, {_id: 2, host: ‘172.19.0.73:27017‘}] }
初始化这个配置
> rs.initiate(config)
查看状态,数据库已变为 PRIMARY或者SECONDARY(下面标红部分),rs.status可查看具体配置信息
[root@mongo2 /]# mongo MongoDB shell version v4.2.2 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("93bc73e6-19fa-4661-9d6f-9ab55cecd631") } MongoDB server version: 4.2.2 Server has startup warnings: 2019-12-16T16:53:20.003+0800 I STORAGE [initandlisten] 2019-12-16T16:53:20.003+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2019-12-16T16:53:20.003+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2019-12-16T16:53:23.351+0800 I CONTROL [initandlisten] 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user,which is not recommended. 2019-12-16T16:53:23.356+0800 I CONTROL [initandlisten] --- 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() --- my_repl:PRIMARY> rs.status() { "set" : "my_repl", "date" : ISODate("2019-12-16T09:17:56.194Z"), "myState" : 1, "term" : NumberLong(3), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "heartbeatIntervalMillis" : NumberLong(2000), "majorityVoteCount" : 2, "writeMajorityCount" : 2, "optimes" : { "lastCommittedOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "lastCommittedWallTime" : ISODate("2019-12-16T09:17:47.805Z"), "readConcernMajorityOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "readConcernMajorityWallTime" : ISODate("2019-12-16T09:17:47.805Z"), "appliedOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "durableOpTime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "lastAppliedWallTime" : ISODate("2019-12-16T09:17:47.805Z"), "lastDurableWallTime" : ISODate("2019-12-16T09:17:47.805Z") }, "lastStableRecoveryTimestamp" : Timestamp(1576487837, 1), "lastStableCheckpointTimestamp" : Timestamp(1576487837, 1), "electionCandidateMetrics" : { "lastElectionReason" : "electionTimeout", "lastElectionDate" : ISODate("2019-12-16T08:58:41.635Z"), "electionTerm" : NumberLong(3), "lastCommittedOpTimeAtElection" : { "ts" : Timestamp(1576486664, 1), "t" : NumberLong(2) }, "lastSeenOpTimeAtElection" : { "ts" : Timestamp(1576486664, 1), "t" : NumberLong(2) }, "numVotesNeeded" : 2, "priorityAtElection" : 1, "electionTimeoutMillis" : NumberLong(10000), "numCatchUpOps" : NumberLong(0), "newTermStartDate" : ISODate("2019-12-16T08:58:47.761Z"), "wMajorityWriteAvailabilityDate" : ISODate("2019-12-16T08:58:48.557Z") }, "electionParticipantMetrics" : { "votedForCandidate" : true, "electionTerm" : NumberLong(2), "lastVoteDate" : ISODate("2019-12-16T08:53:34.481Z"), "electionCandidateMemberId" : 0, "voteReason" : "", "lastAppliedOpTimeAtElection" : { "ts" : Timestamp(1576486389, 1), "t" : NumberLong(1) }, "maxAppliedOpTimeInSet" : { "ts" : Timestamp(1576486389, 1), "t" : NumberLong(1) }, "priorityAtElection" : 1 }, "members" : [ { "_id" : 0, "name" : "172.19.0.71:27017", "ip" : "172.19.0.71", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1138, "optime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2019-12-16T09:17:47Z"), "optimeDurableDate" : ISODate("2019-12-16T09:17:47Z"), "lastHeartbeat" : ISODate("2019-12-16T09:17:55.713Z"), "lastHeartbeatRecv" : ISODate("2019-12-16T09:17:55.705Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.19.0.73:27017", "syncSourceHost" : "172.19.0.73:27017", "syncSourceId" : 2, "infoMessage" : "", "configVersion" : 1 }, { "_id" : 1, "name" : "172.19.0.72:27017", "ip" : "172.19.0.72", "health" : 1, "state" : 1, "stateStr" : "PRIMARY", "uptime" : 1477, "optime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2019-12-16T09:17:47Z"), "syncingTo" : "", "syncSourceHost" : "", "syncSourceId" : -1, "infoMessage" : "", "electionTime" : Timestamp(1576486721, 1), "electionDate" : ISODate("2019-12-16T08:58:41Z"), "configVersion" : 1, "self" : true, "lastHeartbeatMessage" : "" }, { "_id" : 2, "name" : "172.19.0.73:27017", "ip" : "172.19.0.73", "health" : 1, "state" : 2, "stateStr" : "SECONDARY", "uptime" : 1158, "optime" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDurable" : { "ts" : Timestamp(1576487867, 1), "t" : NumberLong(3) }, "optimeDate" : ISODate("2019-12-16T09:17:47Z"), "optimeDurableDate" : ISODate("2019-12-16T09:17:47Z"), "lastHeartbeat" : ISODate("2019-12-16T09:17:55.714Z"), "lastHeartbeatRecv" : ISODate("2019-12-16T09:17:54.613Z"), "pingMs" : NumberLong(0), "lastHeartbeatMessage" : "", "syncingTo" : "172.19.0.72:27017", "syncSourceHost" : "172.19.0.72:27017", "syncSourceId" : 1, "infoMessage" : "", "configVersion" : 1 } ], "ok" : 1, "$clusterTime" : { "clusterTime" : Timestamp(1576487867, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "operationTime" : Timestamp(1576487867, 1) } my_repl:PRIMARY>
到此复制集配置完成,分片学习中,后续更新
以上是关于docker部署mongo集群的主要内容,如果未能解决你的问题,请参考以下文章