转载

Docker&MongoDB实战(一)单机部署

【编者的话】本系列文章将重点介绍如何在Docker中运行MongoDB容器,作者有在生产环境中使用Docker部署MongoDB的经验,所以一些好的思路也可以借鉴。本文是系列文章的第一篇,主要介绍了基本的单机安装,干货还在后面,敬请关注。

Docker 是一个轻量级的Linux容器,它现在正在变得越来越流行。如果你是一个Docker新手,并且想知道它和虚拟机有什么不以及它的优势,可以看看 这篇文章 。

本系列文章将重点介绍如何在Docker中使用MongoDB,从单一的开发/测试环境设置到复杂的多主机MongoDB集群都会涉及,包括日志管理、性能、数据持久化等。下面是虚拟机和Docker的对比:

Docker&MongoDB实战(一)单机部署

网上有很多介绍在Docker与MongoDB的文章,但是大多都是基于Ubuntu,并且也仅仅只是在单机上部署,也没有准备在生产环境中使用。我并不是说Ubuntu或者Debian系统不好,只是觉得应该有一些基于RPM发行版部署的例子,比如Fedora/RHEL和openSUSE/SLES。让我们开始吧,本文中我们使用的是最新的CentOS系统(CentOS 7),先来看下下面这个简单的Dockerfile文件:

# mongod process on latest CentOS
# See https://docs.docker.com/articles/dockerfile_best-practices/

FROM centos # Base container on latest CentOS release
MAINTAINER James Tan <james.tan@mongodb.com>

COPY mongodb.repo /etc/yum.repos.d/ # Copy yum repo config file to add MongoDB repo
RUN yum install -y mongodb-org-server # Install mongod
RUN mkdir -p /data/db # Create default dbpath

ENTRYPOINT [ "mongod" ] # Execute the mongod binary on container startup

然后在同一个目录下创建 mongodb.repo 文件,如下:

[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/
gpgcheck=0
enabled=1

现在我们可以在终端运行 docker build -t mongod . 命令来构建Docker镜像。可能第一次运行时间会比较长,因为构建过程中要下载安装CentOS以及MongoDB的依赖文件,第一次之后的构建都会非常快。比如:

$ ls -l
-rw-r--r--. 1 vagrant vagrant 283 Jan 13 22:27 Dockerfile
-rw-r--r--. 1 vagrant vagrant 122 Jan 5 20:21 mongodb.repo

$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
centos latest 8efe422e6104 8 days ago 224 MB

$ docker build -t mongod .
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon
Step 0 : FROM centos
---> 8efe422e6104
Step 1 : MAINTAINER James Tan <james.tan@mongodb.com>
---> Running in 4e5110a62897
---> 299f6b443463
Removing intermediate container 4e5110a62897
Step 2 : COPY mongodb.repo /etc/yum.repos.d/
---> 93e9e1c7080c
Removing intermediate container 365f1f6a898e
Step 3 : RUN yum install -y mongodb-org-server
---> Running in 6139c81c9b03
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: ftp-stud.fht-esslingen.de
* extras: mirror.euserv.net
* updates: ftp-stud.fht-esslingen.de
Resolving Dependencies
--> Running transaction check
---> Package mongodb-org-server.x86_64 0:2.6.6-1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================

Package Arch Version Repository Size

Installing:

mongodb-org-server        x86_64        2.6.6-1           mongodb        9.0 M

Transaction Summary

Install  1 Package

Total download size: 9.0 M

Installed size: 23 M

Downloading packages:

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

Installing : mongodb-org-server-2.6.6-1.x86_64                            1/1

Verifying  : mongodb-org-server-2.6.6-1.x86_64                            1/1

Installed:

mongodb-org-server.x86_64 0:2.6.6-1

Complete!

---> 1c66942e38f2

Removing intermediate container 6139c81c9b03

Step 4 : RUN mkdir -p /data/db

---> Running in a9eaa1e46fde

---> 74b32e948366

Removing intermediate container a9eaa1e46fde

Step 5 : ENTRYPOINT mongod

---> Running in fbc8e63727b0

---> d491f0032d89

Removing intermediate container fbc8e63727b0

Successfully built d491f0032d89

运行 docker images 命令来确认现在是否已经有mongod镜像,如下:

$ docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
mongod latest d491f0032d89 About a minute ago 330.3 MB
centos latest 8efe422e6104 8 days ago 224 MB

我们可以通过运行 docker run –d –P mongod –smallfiles 来启动容器,并运行 docker ps 来确认当前运行的容器以及端口映射情况。如下:

$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

$ docker run -d -P mongod --smallfiles
f81a716e1f77a0b0ab1e3d6ca0e20b084897c0591bcdef9df4a6a927a9b5a1d2

$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f81a716e1f77 mongod:latest "mongod" 6 seconds ago Up 5 seconds 0.0.0.0:49155->27017/tcp sharp_goodall

运行 docker logs CONTAINER_ID 命令来查看当前容器的日志输出。如下:

$ docker logs f81a716e1f77
mongod --help for help and startup options
2015-01-13T23:21:12.876+0000 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=f81a716e1f77
2015-01-13T23:21:12.877+0000 [initandlisten] db version v2.6.6
2015-01-13T23:21:12.877+0000 [initandlisten] git version: 608e8bc319627693b04cc7da29ecc300a5f45a1f
2015-01-13T23:21:12.877+0000 [initandlisten] build info: Linux build10.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-01-13T23:21:12.877+0000 [initandlisten] allocator: tcmalloc
2015-01-13T23:21:12.877+0000 [initandlisten] options: {}
2015-01-13T23:21:12.934+0000 [initandlisten] journal dir=/data/db/journal
2015-01-13T23:21:12.934+0000 [initandlisten] recover : no journal files present, no recovery needed
2015-01-13T23:21:13.516+0000 [initandlisten] preallocateIsFaster=true 7.7
2015-01-13T23:21:14.070+0000 [initandlisten] preallocateIsFaster=true 7
2015-01-13T23:21:15.577+0000 [initandlisten] preallocateIsFaster=true 5.8
2015-01-13T23:21:15.577+0000 [initandlisten] preallocating a journal file /data/db/journal/prealloc.0
2015-01-13T23:21:18.743+0000 [initandlisten] preallocating a journal file /data/db/journal/prealloc.1
2015-01-13T23:21:22.015+0000 [initandlisten] preallocating a journal file /data/db/journal/prealloc.2
2015-01-13T23:21:26.188+0000 [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-01-13T23:21:26.278+0000 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-01-13T23:21:26.278+0000 [FileAllocator] creating directory /data/db/_tmp
2015-01-13T23:21:26.324+0000 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0.01 secs
2015-01-13T23:21:26.329+0000 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2015-01-13T23:21:26.330+0000 [initandlisten] added index to empty collection
2015-01-13T23:21:26.332+0000 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0 reslen:37 147ms
2015-01-13T23:21:26.335+0000 [initandlisten] waiting for connections on port 27017

docker ps 的命令输出可以看到,容器内的27017端口默认被映射到了主机的49155端口,因此我们可以通过运行 mongo –port 49155 来连接到容器里的mongo进程。如:

$ mongo --port 49155
MongoDB shell version: 2.6.6
connecting to: 127.0.0.1:49155/test
>

好吧,就这些啦,下一篇我们将研究各种各样的Docker网络选项。

原文链接: MONGODB & DOCKER – PART 1 (翻译:温宏强 校对:宋瑜)

正文到此结束
Loading...