在 IBM Bluemix 云平台上开发并部署您的下一个应用。 Docker 之所以如此流行,是因为它创造了一种采用方便的命令行接口 (CLI) 和 HTTP API 工具来封装、运行和维护容器的独特方式。这种简化降低了此技术的入门门槛,使得将应用程序及其运行时环境封装到一个简单 Dockerfile 中的独立镜像中变得可行。Docker 使您能够开发更复杂的项目,但您仍需要配置这些容器。在本文中,我将展示 Ansible 如何通过更清晰的语法带来配置管理器的特性。您将学习如何仅使用已安装的 Python 和 Docker 构建任何堆栈。 在介绍 Ansible 的细节之前,我首先将介绍 Ansible 的 分析 中提到的一些要点:
更确切地讲,借助 Docker,您可以在几秒内实现完全隔离的堆栈环境,或者在服务器之间复制准确的设置。但是,Docker 不包含提供端到端体验的可靠工具,无论是针对开发还是生产。Docker 团队通过新的集群工具解决了这些不断演变的挑战,尝试将 Docker 转变为一个大规模运行容器的可靠解决方案。然而,Docker 仍然需要您手动硬编码任务和重复常见的设置。所以,针对容器的编排和配置管理的关键 Docker 流程仍有待解决。在本文中,您将学习如何结合使用 Ansible 和 Docker 来帮助解决这些问题。 DevOps 的兴起在部署到生产中之前,现代应用程序通常涉及到一个复杂的部署管道。最佳实践建议在每次小型迭代后尽早地、频繁地发布代码。任务的手动执行无法扩展,组织已开始完善介于开发人员与系统管理员之间的流程,所以 DevOps 就诞生了。从那时起,敏捷团队就开始尝试强化和自动化测试代码,以及将其交付给用户的方式。 通过实现最新的技术和方法,公司对其服务器上的代码建立了信心。然而,随着应用程序在规模和复杂性上不断增长,开发人员和系统管理员继续面临着无数挑战。现在比以往更需要为产品提供受支持的社区驱动工具。 Ansible 的可扩展设计在此环境中,Ansible 提供了一个有趣的框架来管理基础架构。您可以获得服务器定义的控制权,比如要安装的包或要复制的文件,并将该配置扩展到数千个服务器。Ansible playbook 构成了集群的目标状态的一种安全表示。它的 YAML 语法和庞大的模块列表生成了任何开发人员都能快速理解的易读性配置文件。不同于 Chef 或 Puppet,Ansible 是无代理的,这意味着您要在远程主机上运行命令,只需一个 SSH 连接即可。可以看到,Ansible 可轻松地处理 DevOps 复杂性。 但是,Ansible 是在容器快速兴起和它们在云开发环境中带来革命之前设计的。那么 Ansible 是否仍然有用?微型服务的范式和复杂开发环境引入了新的需求:
在此上下文中,Ansible 的可扩展架构解决了这些问题。一个 Docker 模块在较高层面上管理主机和容器。尽管您可能会争论哪个编排工具(来自 Google 的 Kubernetes,还是来自 New Relic 的 Centurion)最适合此环境,但 Docker 模块执行效率很高,这正是我在本文中使用它的原因。但是,您还可以构建从其官方 Ansible 镜像启动的容器,然后在本地模式下从内部运行 playbook。尽管此方法非常适合 Packer,而且肯定也适合许多使用情况,但它的缺点通常极为关键。
此方法将容器视为小型 VM,您可以在其中使用一个特定的解决方案。幸运的是,Ansible 拥有模块化设计。模块分散在不同的存储库中,而且 Ansible 的大部分功能都可以通过插件进行扩展。 在下一节中,您将设置一个有效的环境,针对您的需求来调整 Ansible。 设置一个 Ansible 环境假设您想要一个很容易部署的工具,它在轻量型容器中配置应用程序环境。由于与这些容器分离,您需要一个安装了 Ansible 的客户端,您将使用它来向 Docker 守护进程发送命令。此设置如 图 1 中所示。 图 1. 使用 Ansible 配备容器所需的组件您在此配置中必须管理的依赖项,已通过从容器中运行 Ansible 来最小化。此架构将主机限制为容器与命令之间的一个通信桥梁。 可通过许多选项在您服务器上安装 Docker:
无论选择何种解决方案,请确保部署了 Docker 1.3 版或更高版本(1.3 版引入了进程注入)。您还需要运行一个 SSH 服务器来安全地处理 Ansible 命令。 清单 1 中的命令使用公钥设置了一种方便可靠的身份验证方法。 清单 1. 使用公钥设置身份验证的命令# install dependencies sudo apt-get install -y openssh-server libssl-dev # generate private and public keys ssh-keygen -t rsa -f ansible_id_rsa # allow future client with this public key to connect to this server cat ansible_id_rsa.pub >> ~/.ssh/authorized_keys # setup proper permissions chmod 0700 ~/.ssh/ chmod 0600 ~/.ssh/authorized_keys # make sure the daemon is running sudo service ssh restart 配置 SSH 和安全性问题不属于本文的讨论范围。细心的读者可查阅 下一步是将公钥加载到运行 Ansible 的客户端容器上并配备构建器容器。使用一个 Dockerfile 来配备构建器。参见 清单 2。 清单 2. 配备构建器的 DockerfileFROM python:2.7 # Install Ansible from source (master) RUN apt-get -y update && apt-get install -y python-httplib2 python-keyczar python-setuptools python-pkg-resources git python-pip && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN pip install paramiko jinja2 PyYAML setuptools pycrypto>=2.6 six requests docker-py # docker inventory plugin RUN git clone http://github.com/ansible/ansible.git /opt/ansible && cd /opt/ansible && git reset --hard fbec8bfb90df1d2e8a0a4df7ac1d9879ca8f4dde && git submodule update --init ENV PATH /opt/ansible/bin:$PATH ENV PYTHONPATH $PYTHONPATH:/opt/ansible/lib ENV ANSIBLE_LIBRARY /opt/ansible/library # setup ssh RUN mkdir /root/.ssh ADD ansible_id_rsa /root/.ssh/id_rsa ADD ansible_id_rsa.pub /root/.ssh/id_rsa.pub # extend Ansible # use an inventory directory for multiple inventories support RUN mkdir -p /etc/ansible/inventory && cp /opt/ansible/plugins/inventory/docker.py /etc/ansible/inventory/ ADD ansible.cfg /etc/ansible/ansible.cfg ADD hosts /etc/ansible/inventory/hosts 这些指令改编自官方构建版本,自动化了一次来自 Ansible 主分支上的提交 fbec8bfb90df1d2e8a0a4df7ac1d9879ca8f4dde 有效安装。 主机和 ansible.cfg 配置文件(参见 清单 3 和 清单 4)已封装。通过使用容器,可以确保您将共享同一个环境。在本示例中,Dockerfile 安装了 Python 2.7.10 版和 Ansible 2.0.0。 清单 3. 主机配置文件# hosts # this file is an inventory that Ansible is using to address remote servers. Make sure to replace the information with your specific setup and variables that you don't want to provide for every command. [docker] # host properties where docker daemon is running 192.168.0.12 ansible_ssh_user=xavier 清单 4. Ansible 配置文件# ansible.cfg [defaults] # use the path created from the Dockerfile inventory = /etc/ansible/inventory # not really secure but convenient in non-interactive environment host_key_checking = False # free you from typing `--private-key` parameter priva_key_file = ~/.sh/id_rsa # tell Ansible where are the plugins to load callback_plugins = /opt/ansible-plugins/callbacks connection_plugins = /opt/ansible-plugins/connections 在构建 Ansible 容器之前,您必须导出 DOCKER_HOST 环境变量,因为 Ansible 将使用它连接到远程 Docker
守护进程。在使用 HTTP 端点时,需要修改 清单 5. 修改 /etc/default/docker# make docker to listen on HTTP and default socket DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock" 输入命令 以下命令将会构建并验证您用来输入命令的 Ansible 容器(参见 清单 6)。 清单 6. 构建和验证 Ansible 容器的命令# you need DOCKER_HOST variable to point to a reachable docker daemon # pick the method that suits your installation # for boot2docker users eval "$(boot2docker shellinit)" # for docker-machine users, provisioning the running VM was named "dev" eval "$(docker-machine env dev)" # for users running daemon locally export DOCKER_HOST=tcp://$(hostname -I | cut -d" " -f1):2375 # finally users relying on a remote daemon should provide the server's public ip export DOCKER_HOST=tcp://1.2.3.4:2375 # build the container from Dockerfile docker build -t article/ansible . # provide server API version, as returned by `docker version | grep -i "server api"` # it should be at least greater or equal than 1.8 export DOCKER_API_VERSION=1.19 # create and enter the workspace docker run -it --name builder # make docker client available inside -v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock # detect local ip -e DOCKER_HOST=$DOCKER_HOST -e DEFAULT_DOCKER_API_VERSION=DOCKER_API_VERSION -v $PWD:/app -w /app \ # mount the working space article/ansible bash # challenge the setup $ container > ansible docker -m ping 192.168.0.12 | SUCCESS => { "invocation": { "module_name": "ping", "module_args": {} }, "changed": false, "ping": "pong" } 目前为止,一切顺利。您能够从容器输入命令。在下一节中,将对 Ansible 使用特定于 Docker 的扩展。 使用 playbook 和插件扩展 Ansible 环境实质上,Ansible 通过 playbook 自动化了它的执行,这些 playbook 是指定要执行的每个任务和它们的属性的 YAML 文件(参见清单 7)。 Ansible 还使用了清单 (inventory) 来将用户提供的主机映射到基础架构中的具体端点。不同于上一节中使用的静态
清单 7. 一个 Ansible playbook# provision.yml - name: debug docker host hosts: docker tasks: - name: debug infrastructure # access container data : print the state debug: var=hostvars["builder"]["docker_state"] # you can target individual containers by name - name: configure the container hosts: builder tasks: - name: run dummy command command: /bin/echo hello world 清单 8 中的命令查询 Docker 主机,导入事实,打印一些事实,并使用它们对构建器容器执行第二个任务(如 清单 7 中所示)。 清单 8. 查询 Docker 主机的命令ansible-playbook provision.yml -i /etc/ansible/inventory # ... TASK [setup] ******************************************************************** fatal: [builder]: FAILED! => {"msg": "ERROR! SSH encountered an unknown error during the connection. Re-run the command using -vvvv, which enables SSH debugging output to help diagnose the issue", "failed": true} # ... Ansible 不能连接容器,因为它没有运行 SSH 服务器。SSH 服务器是一个要管理的额外进程,它与实际应用程序完全无关。在下一节中,我们将使用一个连接插件来排除此困难。 连接插件是实现传输命令(比如 SSH 或本地执行)的类。Docker 1.3 随带了
像其他插件类型一样,连接挂钩(参见 清单 9)继承一个抽象类,会在您将其放到预期的目录(您在配置文件 清单 9. 连接插件# saved as ./connection_plugins/docker.py import subprocess from ansible.plugins.connections import ConnectionBase class Connection(ConnectionBase): @property def transport(self): """ Distinguish connection plugin. """ return 'docker' def _connect(self): """ Connect to the container. Nothing to do """ return self def exec_command(self, cmd, tmp_path, sudo_user=None, sudoable=False, executable='/bin/sh', in_data=None, su=None, su_user=None): """ Run a command within container namespace. """ if executable: local_cmd = ["docker", "exec", self._connection_info.remote_addr, executable, '-c', cmd] else: local_cmd = '%s exec "%s" %s' % ("docker", self._connection_info.remote_addr, cmd) self._display.vvv("EXEC %s" % (local_cmd), host=self._connection_info.remote_addr) p = subprocess.Popen(local_cmd, shell=isinstance(local_cmd, basestring), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() return (p.returncode, '', stdout, stderr) def put_file(self, in_path, out_path): """ Transfer a file from local to container """ pass def fetch_file(self, in_path, out_path): """ Fetch a file from container to local. """ pass def close(self): """ Terminate the connection. Nothing to do for Docker""" pass 此代码挂钩到 Ansible 模块中,以便通过一个本地 清单 10. |
|