mongo自动化部署

最近需要研究mongo,集群坏了又要重新搞一遍,为了提高效率,写了自动部署的脚本

1. 三个shard分片

2. 每个shard分片是一个三节点的复制集

3. 执行方法

python mongo.py conf
python mongo.py start
python mongo.py shard

4. 端口分布

    复制集repl0                      复制集repl1                     复制集repl2 
     mongod00: 30000                  mongod10: 30010                  mongod11: 30020
     mongod01: 30001                  mongod11: 30011                  mongod12: 30021
     mongod02: 30002                  mongod12: 30012                  mongod13: 30022
------------------------------------------------------------------------------------------
   config0: 40000                     config1: 40001                   config2: 40002
   mongos0: 50000                     mongos1: 50001                   mongos2: 50002

 

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import sys
import time
from pymongo import MongoClient

REPLICAS = 3
SHARDS = 3
CONFIGS = 3
MONGOS = 3

SVR_PORT = 30000
CONFIG_PORT = 40000
MONGOS_PORT = 50000

svr_config = """
net:
   bindIp: localhost
   port: $port
storage:
   dbPath: $dbPath
systemLog:
   destination: file
   path: $path
   logAppend: true
storage:
   journal:
      enabled: true
replication:
   replSetName: $replSetName
sharding:
   clusterRole: shardsvr
"""

config_config = """
sharding:
  clusterRole: configsvr
storage:
   dbPath: $dbPath
systemLog:
   destination: file
   path: $path
   logAppend: true
replication:
  replSetName: replconf
net:
  bindIp: localhost
  port: $port
"""

mongos_config = """
systemLog:
   destination: file
   path: $path
   logAppend: true
net:
  bindIp: localhost
  port: $port
sharding:
  configDB: $configDB
"""

def server_conf():
    currentdir= os.getcwd()
    for i in xrange(SHARDS):
        shard = "shard%d" % i
        os.mkdir(shard)
        replname = "repl%d" % i
        for j in xrange(REPLICAS):
            port = str(SVR_PORT + i * 10 + j)
            mongodir = "shard%d/mongod%d%d" % (i, i, j)
            os.mkdir(mongodir)
            confpath = currentdir+ "/" + mongodir + "/" + "mongod.conf"
            dbpath = currentdir+ "/" + mongodir
            logpath = currentdir+ "/" + mongodir + "/" + "mongod.log"

            content = svr_config
            content = content.replace("$dbPath", dbpath)
            content = content.replace("$path", logpath)
            content = content.replace("$replSetName", replname)
            content = content.replace("$port", port)
            file = open(confpath , "w")
            file.write(content)
            file.close()


def config_conf():
    currentdir= os.getcwd()
    for i in xrange(CONFIGS):
        configdir = "config%d" % i
        configname = configdir + "/" + "config.conf"
        dbpath = currentdir+ "/" + configdir
        logpath = currentdir+ "/" + configdir + "/" + "config.log"
        os.mkdir(configdir)
        content = config_config
        file = open(currentdir + "/" + configname, "w")
        content = content.replace("$port", str(CONFIG_PORT+i))
        content = content.replace("$dbPath", dbpath)
        content = content.replace("$path", logpath)
        file.write(content)
        file.close()


def mongos_conf():
    currentdir = os.getcwd()
    for i in xrange(MONGOS):
        mongosdir = "mongos%d" % i
        mongosname = mongosdir + "/" + "mongos.conf"
        logpath = currentdir + "/" + mongosdir + "/" + "mongos.log"
        os.mkdir(mongosdir)
        content = mongos_config
        file = open(currentdir + "/" + mongosname, "w")
        content = content.replace("$port", str(MONGOS_PORT+i))
        content = content.replace("$path", logpath)
        confighost = []
        for j in xrange(CONFIGS):
            confighost.append("127.0.0.1:%d" % (CONFIG_PORT + j))
        configdb = "replconf/" + ",".join(confighost)
        content = content.replace("$configDB", configdb)
        file.write(content)
        file.close()


def server_repl():
    currentdir = os.getcwd()
    for i in xrange(SHARDS):
        replname = "repl%d" % i
        repls = {'_id': replname, 'members': []}
        for j in xrange(REPLICAS):
            port = str(SVR_PORT + i * 10 + j)
            mongodir = "shard%d/mongod%d%d" % (i, i, j)
            confpath = currentdir + "/" + mongodir + "/" + "mongod.conf"
            repls["members"].append({'_id': j, 'host': '127.0.0.1:%s' % port})
            cmd = "nohup mongod -f %s &" % confpath
            os.system(cmd)
        port = SVR_PORT + i * 10 + 0
        client = MongoClient('127.0.0.1', port)
        print "replicas ==>", repls
        time.sleep(2)
        for k in xrange(5):
            try:
                client.admin.command("replSetInitiate", repls)
                break
            except:
                print "init %s failed %d time" % (str(repls), k)
                #print traceback.format_exc()
                time.sleep(2)


def config_repl():
    currentdir = os.getcwd()
    for i in xrange(SHARDS):
        replname = "replconf"
        repls = {'_id': replname, "configsvr": True, 'members': []}

        port = CONFIG_PORT + i
        configdir = "config%d" % i
        confpath = currentdir + "/" + configdir + "/" + "config.conf"
        repls["members"].append({'_id': i, 'host': '127.0.0.1:%d' % port})
        cmd = "nohup mongod -f %s &" % confpath
        os.system(cmd)
        client = MongoClient('127.0.0.1', port)
        print "replicas ==>", repls
        time.sleep(2)
        for k in xrange(5):
            try:
                client.admin.command("replSetInitiate", repls)
                break
            except:
                print "init %s failed %d time" % (str(repls), k)
                # print traceback.format_exc()
                time.sleep(2)


def mongos_repl():
    currentdir = os.getcwd()
    for i in xrange(MONGOS):
        mongosdir = "mongos%d" % i
        confpath = currentdir + "/" + mongosdir + "/" + "mongos.conf"
        cmd = "nohup mongos -f %s &" % confpath
        os.system(cmd)


def shard():
    client = MongoClient('127.0.0.1', MONGOS_PORT)
    for i in xrange(SHARDS):
        client.admin.command("addShard", "repl%d/127.0.0.1:%d" % (i, (SVR_PORT+i*10)))

    client.admin.command("enableSharding", "test")
    client.admin.command("shardCollection", "test.person", key={"id": "hashed"})
    client.close()

if __name__ == "__main__":
    argv = sys.argv
    if "conf" in argv:
        server_conf()
        config_conf()
        mongos_conf()

    if "start" in argv:
        server_repl()
        config_repl()
        mongos_repl()

    if "shard" in argv:
        shard()

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值