Integration of Mesos and Docker

Abstract:  As we all know, Mesos fully supports Docker. But what does this mean? Run docker run... from the command line to use Docker. What more needs to be done? Let's take a look at the advanced features of Mesos - the integration with Docker. This article is selected from "Building Distributed Applications with the Mesos Framework".

  Because Docker itself wants to manage the entire container, from chroot, namespaces, to cgroups for the entire namespace, it conflicts with the default Mesos container. Therefore, Mesos adds support for container machines, a pluggable mechanism that makes Mesos' container machine subsystem extensible: initially Mesos' LXC/cgroup-based containers were introduced into the container machine API, and Docker was the first to be added. A new container machine, and now also has a comprehensive documentation protocol on how to add new container machines, such as KVM virtual machines.

Use Docker

  In order to use the Docker container machine technology, it must be included in the command line of the Mesos slave. For example, mesos-slave --containerizers=docker,mesos... allows Docker and Mesos containers to be used on this slave.
  Might also want to increase the executor's registration timeout so that Mesos doesn't think the container is down while it's still downloading. It can be set to five minutes at the beginning to ensure that there is enough time to download the Docker image. So, the slave command line is similar:

mesos-slave --containerizers=docker,mesos \
--executor_registration_timeout=5mins ...

  Using Docker with an application is very simple - once Docker support is enabled, just set the container field (of type ContainerInfo ) in TaskInfo or ExecutorInfor .

Confusingly, the message CommandInfo.ContainerInfo is not the correct message - the highest level ContainerInfo needs to be set in mesos.proto with Docker related fields.

  要想使用Docker,需要将ContainerInfo里的type设置为DOCKER,并且将docker字段设置到ContainerInfo.Docker消息的一个实例里,该消息的image属性设置为Docker镜像的名称(比如myusername/webapp)。这里可以配置很多Docker参数,比如是使用HOST还是BRIDGE网络,映射使用哪些端口或者额外的Docker命令行参数。如果想让Docker容器使用Dockerfile里指定的docker run ...,还必须将TaskInfo的CommandInfo设置成shell=false。如果设置成shell=true,就需要禁用Dockerfile里的run,指定的command会由sh -c “”来运行。
  当启动Docker容器机任务时,slave会首先获取(并且解包)沙箱里所有指定的URI,并且将Docker镜像拉取到本地。然后,slave通过运行docker启动Docker镜像。docker命令的HOME环境变量指向该沙箱,因此可以通过获取到的URI来配置Docker(详见下面的注意事项)。在Docker镜像里可以使用该沙箱,其路径保存在MESOS_SANDBOX环境变量里。最后,Docker的stdout和stderr会被重定向到Mesos沙箱里名为stdout和stderr的文件上。

高级Docker配置
必须记住的一点是,Docker容器机总是会尝试从registry里拉取Docker镜像。这意味着无法使用仅在本地安装了的Docker镜像——必须在某个地方部署该镜像。如果想要使用私有registry,可以提供一个.dockercfg文件。该文件由一个URI指定,这样Mesos slave就能够使用其自动获取URL的功能将.dockercfg文件拷贝到Docker进程所使用的HOME目录下。

  相同的API也适用于基于Docker的执行器,唯一不同之处在于,执行器代码实际上可以在Docker容器内运行。要实现这一目的,需要完成上文所述的所有事情,但是是在ExecutorInfo消息里,而不是TaskInfo消息里。
本文选自《用Mesos框架构建分布式应用》,点此链接可在博文视点官网查看此书。
                    图片描述

  想及时获得更多精彩文章,可在微信中搜索“博文视点”或者扫描下方二维码并关注。
                       图片描述

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313974&siteId=291194637