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可查看具体配置信息

[[email protected] /]# 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>

到此复制集配置完成,分片学习中,后续更新

原文地址:https://www.cnblogs.com/doufy/p/12050182.html

时间: 2024-10-10 02:36:11

docker部署mongo集群的相关文章

使用Docker部署RabbitMQ集群

概述 本文重点介绍的Docker的使用,以及如何部署RabbitMQ集群,最基础的Docker安装,本文不做过多的描述,读者可以自行度娘. Windows10上Docker的安装 因为本人用的是Windows系统,所有推荐一个不错的安装文章,详见:https://blog.csdn.net/xiaoping0915/article/details/75094857 注意:设置阿里云Docker Hub加速,配置Registry mirrors地址"https://j0andt2p.mirror.

利用Docker部署mongodb集群--分片与副本集

环境 Docker version 1.6.2  mongodb 3.0.4 第一步  编写Dockerfile并生成镜像 主意包含两个Dockerfile镜像,一个mongod的,一个mongos(在集群中负责路由) 编写Mongod的Dockerfile: FROM ubuntu:14.04 RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 ENV MONGO_MAJOR 3.0 RUN ech

Docker部署Hadoop集群

一.主机规划 3台主机:1个master.2个slaver/worker ip地址使用docker默认的分配地址: master: 主机名: hadoop2.ip地址: 172.17.0.2 slaver1: 主机名: hadoop3.ip地址: 172.17.0.3 主机名: hadoop4.ip地址: 172.17.0.4 二.软件安装 1.在docker中安装centos镜像,并启动centos容器,安装ssh.--详见"docker上安装centos镜像"一文. 2.通过ssh

Ubuntu 16.04下使用docker部署ceph集群

ceph集群docker部署 通过docker可以快速部署小规模Ceph集群的流程,可用于开发测试. 以下的安装流程是通过linux shell来执行的:假设你只有一台机器,装了linux(如Ubuntu)系统和docker环境,那么可以参考以下步骤安装Ceph: # 要用root用户创建, 或有sudo权限 # 注: 建议使用这个docker镜像源:https://registry.docker-cn.com # 1. 修改docker镜像源 cat > /etc/docker/daemon.

docker部署redis-cluster集群

https://segmentfault.com/a/1190000010131816 1>拉取reids镜像:docker pull redis 2>创建虚拟网卡 :docker network create redis-net 3>创建redis配置文件 mkdir docker/redisContainer/redis-cluster,进入redis-cluster目录执行下面指令 for port in `seq 7000 7005`; do \ mkdir -p ./${por

docker部署lnmp集群

LNMP的网段为:172.16.10.0/24 服务容器与各个ip:nginx 172.16.10.10mysql 172.16.10.20php 172.16.10.30** 导入一下镜像包:mysql5.7php.7.2-fpm.tarwordpress.tarnginx.tar 将导入的镜像包导成docker可用的镜像:docker load < nginx.tar && docker load < wordpress.tar && docker load

在Docker中安装和部署MongoDB集群

在Docker中安装mongodb 采用的mongodb镜像:https://registry.hub.docker.com/u/tutum/mongodb/ 以该镜像启动一个容器(注意此时mongodb是standalone模式): docker run -d --name=mongodb -p 27017:27017 -p 28017:28017 tutum/mongodb:3.0 docker logs mongodb 输出信息: =============================

使用docker安装部署Spark集群来训练CNN(含Python实例)

使用docker安装部署Spark集群来训练CNN(含Python实例) 本博客仅为作者记录笔记之用,不免有很多细节不对之处. 还望各位看官能够见谅,欢迎批评指正. 博客虽水,然亦博主之苦劳也. 如需转载,请附上本文链接,不甚感激! http://blog.csdn.net/cyh_24/article/details/49683221 实验室有4台神服务器,每台有8个tesla-GPU,然而平时做实验都只使用了其中的一个GPU,实在暴遣天物! 于是想用spark来把这些GPU都利用起来.听闻d

7条命令在docker中部署Mesos集群

7条命令在docker中部署Mesos集群 所有使用的Docker容器构建文件是有也.您可以在本地构建每个容器或只使用位于Docker Hub预构建的容器.下面的命令会自动下载所需的预建的容器为您服务.ZooKeeper?-?https://registry.hub.docker.com/u/garland/zookeeper/Meso Master?-?https://registry.hub.docker.com/u/garland/mesosphere-docker-mesos-maste