手把手教你用Docker部署一个MongoDB集群
http://dockerone.com/article/181?ref=myread
【编者的话】MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中最像关系数据库的。支持类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。本文介绍了如何使用Docker搭建MongoDB集群。
本文我会向大家介绍如何使用Docker部署一个MongoDB集群,具体如下:
root@node *:/# export node1=10.11.32.174 root@node *:/# export node2=10.11.33.37 root@node *:/# export node3=10.11.31.176
root@node *:/# mkdir -p /home/core root@node *:/# cd /home/core root@node *:/# openssl rand -base64 741 > mongodb-keyfile root@node *:/# chmod 600 mongodb-keyfile root@node *:/# sudo chown 999 mongodb-keyfile
root@node1:/# docker run --name mongo \ -v /home/core/mongo-files/data:/data/db \ -v /home/core/mongo-files:/opt/keyfile \ --hostname="node1.example.com" \ -p 27017:27017 \ -d mongo:2.6.5 --smallfiles
root@node1:/# docker exec -it mongo /bin/bash
root@node1:/# mongo
MongoDB shell version: 2.6.5 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user >
> use admin switched to db admin
> db.createUser( { user: "siteUserAdmin", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] });
Successfully added user: { "user" : "siteUserAdmin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ]}
> db.createUser( { user: "siteRootAdmin", pwd: "password", roles: [ { role: "root", db: "admin" } ] });
Successfully added user: { "user" : "siteRootAdmin", "roles" : [ { "role" : "root", "db" : "admin" } ]}
> exit bye root@node1:/# exit
root@node1:/# docker stop mongo
root@node1:/# docker rm mongo root@node1:/# docker run \ --name mongo \ -v /home/core/mongo-files/data:/data/db \ -v /home/core/mongo-files:/opt/keyfile \ --hostname="node1.example.com" \ --add-host node1.example.com:${node1} \ --add-host node2.example.com:${node2} \ --add-host node3.example.com:${node3} \ -p 27017:27017 -d mongo:2.6.5 \ --smallfiles \ --keyFile /opt/keyfile/mongodb-keyfile \ --replSet "rs0"
--keyFile
的路径是/opt/keyfile/mongodb-keyfile
,这是正确的。这是密钥文件在Docker内部的地址,我们用-v
选项将密钥文件映射到容器内部的那个路径上(即:/opt/keyfile/mongodb-keyfile)。--add-host
把这些信息添加到Docker容器的/etc/hosts
文件中,所以我们可以使用域名而不是IP地址了。在实际的生产环境中这些信息都是DNS,这些参数都可以忽略。root@node1:/# docker exec -it mongo /bin/bash root@node1:/# mongo MongoDB shell version: 2.6.5 >
> use admin switched to db admin
> db.auth("siteRootAdmin", "password"); 1
> rs.initiate() { "info2" : "no configuration explicitly specified -- making one", "me" : "node1.example.com:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1 } >
> rs0:PRIMARY> rs.conf() { "_id" : "rs0", "version" : 1,r "members" : [ { "_id" : 0, "host" : "node1.example.com:27017" } ]}
root@node2:/# docker run \ --name mongo \ -v /home/core/mongo-files/data:/data/db \ -v /home/core/mongo-files:/opt/keyfile \ --hostname="node2.example.com" \ --add-host node1.example.com:${node1} \ --add-host node2.example.com:${node2} \ --add-host node3.example.com:${node3} \ -p 27017:27017 -d mongo:2.6.5 \ --smallfiles \ --keyFile /opt/keyfile/mongodb-keyfile \ --replSet "rs0"
root@node3:/# docker run \ --name mongo \ -v /home/core/mongo-files/data:/data/db \ -v /home/core/mongo-files:/opt/keyfile \ --hostname="node3.example.com" \ --add-host node1.example.com:${node1} \ --add-host node2.example.com:${node2} \ --add-host node3.example.com:${node3} \ -p 27017:27017 -d mongo:2.6.5 \ --smallfiles \ --keyFile /opt/keyfile/mongodb-keyfile \ --replSet "rs0"
rs0:PRIMARY> rs.add("node2.example.com") rs0:PRIMARY> rs.add("node3.example.com")
rs0:PRIMARY> rs.status()
root@node*:/# docker logs -ft mongo