Racncher搭建MongoDB 4.0 分片

一、概念

       搭建mongodb分片的主要原因是为了提高数据的读写性能,分片将数据库拆分,将其分散在不同的机器上,每个片只负责总数据的一部分,最后通过路由来对各个分片进行数据访问。
搭建分片前要了解以下三个组件:

  • Shard:分片服务器,用于存储实际的数据块,实际生产环境中一个shard server 角色可以由几台服务器组成一个Peplica Set 承担,防止主机单点故障.
  • Config Server:配置服务器,存储了整个分片群集的配置信息,其中包括chunk信息。
  • Routers:前端路由,客户端由此接入,且让整个群集看上去像单一数据库,前端应用可以透明使用。
image.png

二、部署安装

        使用docker进行部署,总共部署三个环境:

三台配置服务(27018)形成复制集,分片1、2、3也在各机器都部署一个实例,它们之间形成复制集,客户端直接连接3个路由服务与之交互,配置服务和分片服务对客户端是透明的。

image.png

安装之前先生存KeyFile文件进行认证

openssl rand -base64 741 > /mongodb/key/mongodb-keyfile
chown -R 999 key/
chmod 600 /mongodb/key/mongodb-keyfile

1、安装配置服务器

配置服务器 配置服务端口
10.42.200.101 27018
10.42.200.102 27018
10.42.200.103 27018
1、新建mongodb.conf配置文件

配置文件中没有使用logpath,因为docker的目录映射问题,一直解决不了。所以最后使用了syslog方式

sudo vim /mongodb/config/config.conf

#数据目录
dbpath=/data/db
#日志文件
#logpath=/data/mongodb.log
#日志追加
logappend=true
#端口
port = 27018
#守护进程模式
#fork = true
#最大连接数
maxConns = 5000
#配置服务器
configsvr = true
#副本集名称
replSet = configs
#日志,redo log
journal = true
#刷写提交机制
journalCommitInterval = 200
#刷写数据到日志的频率
syncdelay = 60
#引擎
storageEngine = wiredTiger
#操作日志,单位M
oplogSize = 1000
#命名空间的文件大小,默认16M,最大2G。
nssize = 16
#绑定地址
bind_ip=0.0.0.0
#认证
auth = true
#keyfile
keyFile = /key/mongodb-keyfile
2、使用配置文件启动三个容器,并设置固定ip

MongodbConfig mongodb分片集群配置服务器
编排文件:
rancher-compose.yml

version: '2'
services:
  config2:
    scale: 1
    start_on_create: true
  config3:
    scale: 1
    start_on_create: true
  config1:
    retain_ip: true
    scale: 1
    start_on_create: true

docker-compose.yml

version: '2'
services:
  config2:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/configs/config2:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/config.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.102
  config3:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/configs/config3:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/config.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.103
  config1:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/configs/config1:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime 
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/config.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.101

3、 配置复制集

然后连接任意一台mongo

mongo  --port 27018
#切换数据库:
use admin
#认证
db.auth('jfwang','123457')
#初始化复制集:
rs.initiate({_id:"configs",members:[{_id:0,host:"10.42.200.101:27018"},{_id:1,host:"10.42.200.102:27018"}, {_id:2,host:"10.42.200.103:27018"}]})

2、分片服务部署

总共启动9个容器。一个容器一个实例,每三个容器形成一个复制集分片。总共三个分片。

分片服务器 分片服务端口
10.42.200.111 27001
10.42.200.112 27001
10.42.200.113 27001
10.42.200.121 27002
10.42.200.122 27002
10.42.200.123 27002
10.42.200.131 27003
10.42.200.132 27003
10.42.200.133 27003
1、 新建shard.conf配置文件
sudo vim /mongodb/config/shard1.conf

#数据目录
dbpath=/data/db
#日志追加
logappend=true
#端口
port = 27001 #其他2个分片对应修改为27002、27003
#最大连接数
maxConns = 5000
#引擎
storageEngine=mmapv1
#分片服务器
shardsvr=true
replSet=shard1 #其他2个分片对应修改为shard2、shard3
#绑定地址
bind_ip=0.0.0.0
#认证
auth = true
#keyfile
keyFile = /key/mongodb-keyfile

2、启动容器

MongodbShard mongodb分片集群分片服务器
编排文件:
rancher-compose.yml

version: '2'
services:
  shard13:
    scale: 1
    start_on_create: true
  shard31:
    scale: 1
    start_on_create: true
  shard21:
    scale: 1
    start_on_create: true
  shard32:
    scale: 1
    start_on_create: true
  shard11:
    scale: 1
    start_on_create: true
  shard22:
    scale: 1
    start_on_create: true
  shard33:
    scale: 1
    start_on_create: true
  shard12:
    scale: 1
    start_on_create: true
  shard23:
    scale: 1
    start_on_create: true

docker-compose.yml

version: '2'
services:
  shard13:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard13:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard1.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.113
  shard31:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard31:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard3.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.131
  shard21:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard21:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard2.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.121
  shard32:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard32:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard3.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.132
  shard11:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard11:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard1.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.111
  shard22:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard22:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard2.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.122
  shard33:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard33:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard3.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.133
  shard12:
    image: mongo
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard12:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard1.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.112
  shard23:
    environment:
      MONGO_INITDB_ROOT_USERNAME: jfwang
      MONGO_INITDB_ROOT_PASSWORD: 123457
    image: mongo
    stdin_open: true
    volumes:
    - /mongodb/data/shards/shard23:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongod
    - --config
    - /etc/config/shard2.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.200.123

3、将分片配置为复制集

连接mongo,只需在任意一台机器执行即可:

mongo --port 27001 //这里以shard1为例
# 切换数据库
use admin
#认证
db.auth('jfwang','123457')
# 初始化复制集
rs.initiate({_id:"shard1",members:[{_id:0,host:"10.42.200.111:27001"},{_id:1,host:"10.42.200.112:27001"},{_id:2,host:"10.42.200.113:27001"}]})

以上是基于分片1来操作,同理,其他2个分片也要连到各自的端口来执行一遍上述的操作,让3个分片各自形成1主2从的复制集,注意端口及仲裁节点的问题即可,操作完成后3个分片都启动完成,并完成复制集模式。

3、路由服务部署

启动3个容器。形成一个复制集

路由服务器 服务端口
10.42.250.111 27017
10.42.250.112 27017
10.42.250.113 27017
1、新建mongos.conf配置文件
sudo vim /mongodb/config/mongos.conf


#日志追加
logappend=true
#端口
port = 27017 
#最大连接数
maxConns = 20000
configdb = aconfigs/10.42.200.101:27018,10.42.200.102:27018,10.42.200.103:27018
#绑定地址
bind_ip=0.0.0.0
#keyfile
keyFile = /key/mongodb-keyfile
2、启动容器

编排文件:
rancher-compose.yml

version: '2'
services:
  route3:
    scale: 1
    start_on_create: true
  route2:
    scale: 1
    start_on_create: true
  route1:
    scale: 1
    start_on_create: true

docker-compose.yml

version: '2'
services:
  route3:
    image: mongo
    stdin_open: true
    volumes:
    - /mongodb/data/routes/route3:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongos
    - --config
    - /etc/config/mongos.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.250.113
  route2:
    image: mongo
    stdin_open: true
    volumes:
    - /mongodb/data/routes/route2:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongos
    - --config
    - /etc/config/mongos.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.250.112
  route1:
    image: mongo
    stdin_open: true
    volumes:
    - /mongodb/data/routes/route1:/data
    - /mongodb/config:/etc/config
    - /mongodb/key:/key
    - /etc/localtime:/etc/localtime
    tty: true
    command:
    - mongos
    - --config
    - /etc/config/mongos.conf
    labels:
      io.rancher.container.pull_image: always
      io.rancher.container.requested_ip: 10.42.250.111

3、启动分片功能
#连接mongo
mongo --port 27017
#切换数据库
use admin
#添加分片,只需在一台机器执行即可:
sh.addShard("shard1/10.42.200.111:27001,10.42.200.112:27001,10.42.200.113:27001")
sh.addShard("shard2/10.42.200.121:27002,10.42.200.122:27002,10.42.200.123:27002")
sh.addShard("shard3/10.42.200.131:27003,10.42.200.132:27003,10.42.200.133:27003")
#查看集群状态
sh.status()
4、配置分片功能
# 块的大小默认是64M,可以修改
use config
db.settings.save({"_id":"chunksize","value":32})
5、实现分片功能
sh.enableSharding("库名")
sh.shardCollection("库名.集合名",{"key":1})
6、删除片键
use config
db.collections.remove({ _id: "NGA.Replay" })
db.chunks.remove({ ns: "NGA.Replay" })
db.locks.remove({ _id: "NGA.Replay" })
use admin
db.adminCommand("flushRouterConfig") #刷新配置服务器

4、测试

略过

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容

  • 标签: 翻译 虚拟化 rancher 自从Rancher的Beta版本发布的几周以来,平台新的调度和服务发现的功能...
    cheneydc阅读 2,808评论 1 11
  • 一 、什么是 Docker Docker 最初是 dotCloud 公司创始人 Solomon Hykes 在法国...
    Blazzer阅读 3,136评论 0 13
  • 今天呢,是开学的日子,其实并没有什么,去了还能回来,回来了呆一个月再走,这样来来回回反反复复,很平常不过。 但是,...
    c7cko阅读 207评论 0 0
  • 我愿意用此生偿还所有业力;我愿意用爱偿还一切无论这一生有多长... 我本是一粒尘埃,归于无限,何必放大自我,感恩神...
    安妮_beijing阅读 459评论 7 4
  • 春雨迷蒙时,又见龙抬头。 忽然梦还乡,误是昨日景。 谁知时光去,如今已十年。 白纱蒙头盖,声声唤不醒。 未及泪两行...
    A离离上草阅读 245评论 0 4