以 Blueshift 项目为背景,Puppet 新增了一组 Docker 镜像,用于运行发布到Docker Hub 的Puppet 软件。
这些新Docker 镜像的例子里包含一个让用户可以运行Puppet Server(可以独立运行,也可以和 PuppetDB 同时运行)的 Puppet Server 镜像和一个附带 PostgreSQL 镜像的 PuppetDB 镜像。其中还包含两个代理镜像,一个基于Ubuntu Xenial Puppet 代理程序包,另一个基于简化版的Alpine 。
Blueshift 演示了异构软件管理问题的解决方案,使用 Puppet 作为新软件栈的统一管理方式。Blueshift 包含 Puppet 社区中关于如何集成 Consul 、 CoreOS 和 Mesos 等技术的信息。Blueshift 还包含 Puppet 的内部工程。
Puppet 还提供了如何在Docker 中使用Puppet 的例子。目前,这些例子展示了如何在VMware Photon OS 、Red Hat CentOS Atomic 上的 Docker 容器中和 CoreOS 上使用 Puppet。还有例子展示了如何使用 Docker Compose 搭建一个 Puppet 基础设施。
在 Blueshift 项目的一个例子中, Gareth Rushgrove 演示了如何使用 Puppet 管理 Docker 容器。Puppet Docker 模块大约是和 Docker 同时发布的,自此以后,社区一直致力于这方面的工作。
第一步是安装 Puppet Docker 示例模块:
# puppet module install garethr-docker
简单来说,Docker 模块允许你使用清单文件中的一行代码安装 Docker:
include 'docker'
你可以在清单文件中声明任意多的镜像。这个例子使用了一个 Ubuntu 镜像:
docker::image { ‘ubuntu':
image => 'trusty',
}
这些简单的 Docker 容器现在可以通过 Puppet 轻松地应用了。Docker version 会显示 Docker 已经安装了,并且会显示详细的版本信息:
# puppet apply /vagrant/docker_example.pp
# docker version
Client version: 1.5.0
Client API version: 1.17
Go version (client): go1.4.1
Git commit (client):a8a31ef
OS/Arch (client): linux/amd64
Server version: 1.5.0
Server API version: 1.17
Go version (server): go1.4.1
Git commit (server): a8a31ef
#
Docker ps 会显示当前没有任何东西在运行:
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
#
Docker images 会显示 Docker 镜像已经创建:
# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Ubuntu trusty d0955f21bf24 3 weeks ago 188.3 MB
Ubuntu trusty-20150320 d0955f21bf24 3 weeks ago 188.3 MB
Ubuntu latest d0955f21bf24 3 weeks ago 188.3 MB
Ubuntu 14.04 d0955f21bf24 3 weeks ago 188.3 MB
Ubuntu 14.04.2 d0955f21bf24 3 weeks ago 188.3 MB #
Docker 模块支持运行和管理各种 Docker 容器。容器可以在主机的 init 系统(如 systemd 或 sysvinit )下运行,也可以使用 Docker 内置的进程管理器。下面的代码增加了两个简单的 Docker run 资源:
docker::run { 'helloworld':
image => 'ubuntu',
command => '/bin/sh -c "while true; do echo hello world; sleep 1; done"',
}
docker::run { 'goodbyecruelworld':
image => 'ubuntu',
command => '/bin/sh -c "while true; do echo goodbye cruel world; sleep 1; done"',
}
使用 puppet apply,我们可以快速将更新应用到这两个将要在 Docker 容器中运行的服务上。现在,Docker ps 会显示,当前有两个简单的服务正在运行:
# Puppet apply /vagrant/docker_example.pp
Notice: Compiled catalog for localhost in environment production in 0.93 seconds
Notice: /Stage[main]/Main/Docker::Run[helloworld]/File[/etc/init.d/docker-helloworld]/ensure: created
Notice: /Stage[main]/Main/Docker::Run[helloworld]/Service[docker-helloworld]/ensure: ensure changed ‘stopped’to ‘running’
Notice: /Stage[main]/Main/Docker::Run[goodbyecruelworld]/File[/etc/init.d/docker-goodbyecruelworld]/ensure: created
Notice: /Stage[main]/Main/Docker::Run[goodbyecruelworld]/Service[docker-goodbyecruelworld]/ensure: ensure changed ‘stopped’ to ‘running’
Notice: Finished catalog run in 1.11 seconds
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
27b9ca786f9b ubuntu:14.04 “/bin/sh -c ‘while t 18 seconds ago Up 17 seconds jolly_wright
4ec0c0225714 ubuntu:14.04 “/bin/sh -c ‘while t 18 seconds ago Up 17 seconds focused_wright
#
使用 docker attach 和容器 ID 连接到其中一个服务上会显示正在 Docker 中执行的服务:
# docker attach 27b9ca786f9b
goodbye cruel world
goodbye cruel world
goodbye cruel world
^C#
#
# docker attach 4ec0c0225714
hello world
hello world
hello world
^C#
#
Docker 模块还支持类似挂载卷、设置环境变量、运行特权容器和暴露端口这样的动作。Puppet 还可以使用 docker exec 特性在运行中的容器环境中执行命令:
docker::exec { 'helloworld-uptime':
detach => true,
container => 'helloworld',
command => 'uptime',
tty => true,
}
查看英文原文: Puppet Releases Docker-Focused Features in Project Blueshift




