共计 4840 个字符,预计需要花费 13 分钟才能阅读完成。

容器是一个日益流行的开发环境。作为一名开发人员,你可以选择多种工具来管理你的容器。本文将向你介绍 Ansible Container,并展示如何在类似生产环境中运行和测试你的应用程序。
入门
这个例子使用了一个简单的 Flask Hello World 程序。这个程序就像在生产环境中一样由 Apache HTTP 服务器提供服务。首先,安装必要的 docker 包:
sudo dnf install docker
Ansible Container 需要通过本地套接字与 Docker 服务进行通信。以下命令将更改套接字所有者,并将你添加到可访问此套接字的 docker 用户组:
sudogroupadd docker &&sudo gpasswd -a $USER dockerMYGRP=$(id-g);newgrp docker ;newgrp $MYGRP
运行 id 命令以确保 docker 组在你的组成员中列出。最后,使用 sudo 启用并启动 docker 服务:
sudosystemctl enable docker.servicesudosystemctl start docker.service
设置 Ansible Container
Ansible Container 使你能够构建容器镜像并使用 Ansible playbook 进行编排。该程序在一个 YAML 文件中描述,而不是使用 Dockerfile,列出组成容器镜像的 Ansible 角色。
不幸的是,Ansible Container 在 Fedora 中没有 RPM 包可用。要安装它,请使用 Python3 虚拟环境模块。
mkdir ansible-container-flask-examplecd ansible-container-flask-examplepython3 -m venv .venvsource .venv/bin/activatepip install ansible-container[docker]
这些命令将安装 Ansible Container 及 Docker 引擎。Ansible Container 提供三种引擎:Docker、Kubernetes 和 Openshift。
设置项目
现在已经安装了 Ansible Container,接着设置这个项目。Ansible Container 提供了一个简单的命令来创建启动所需的所有文件:
ansible-container init
来看看这个命令在当前目录中创建的文件:
ansible.cfgansible-requirements.txtcontainer.ymlmeta.ymlrequirements.yml
该项目仅使用 container.yml 来描述程序服务。有关其他文件的更多信息,请查看 Ansible Container 的入门文档。
定义容器
如下更新 container.yml:
version:"2"settings:conductor:#TheConductor container does the heavy lifting,and provides a portable#Python runtime for building your target containers.It should be derived#from the same distribution as you're building your target containers with.base: fedora:26# roles_path: # Specify a local path containing Ansible roles# volumes: # Provide a list of volumes to mount# environment: # List or mapping of environment variables# Set the name of the project. Defaults to basename of the project directory.# For built services, concatenated with service name to form the built image name.project_name: flask-helloworldservices:# Add your containers here, specifying the base image you want to build from.# To use this example, uncomment it and delete the curly braces after services key.# You may need to run `docker pull Ubuntu:trusty` for this to work.web:from: "fedora:26"roles:- baseports:- "5000:80"command: ["/usr/bin/dumb-init", "httpd", "-DFOREGROUND"]volumes:- $PWD/flask-helloworld:/flaskapp:Z
conductor 部分更新了基本设置以使用 Fedora 26 容器基础镜像。
services 部分添加了 web 服务。这个服务使用 Fedora 26,后面有一个名为 base 的角色。它还设置容器和主机之间的端口映射。Apache HTTP 服务器为容器的端口 80 上的 Flask 程序提供服务,该容器重定向到主机的端口 5000。然后这个文件定义了一个卷,它将 Flask 程序源代码挂载到容器中的 /flaskapp 中。
最后,容器启动时运行 command 配置。这个例子中使用 dumb-init,一个简单的进程管理器并初始化系统启动 Apache HTTP 服务器。
Ansible 角色
现在已经设置完了容器,创建一个 Ansible 角色来安装并配置 Flask 程序所需的依赖关系。首先,创建 base 角色。
mkdir-p roles/base/taskstouch roles/base/tasks/main.yml
现在编辑 main.yml,它看起来像这样:
---- name:Install dependenciesdnf: pkg={{item}} state=presentwith_items:- python3-flask- dumb-init- httpd- python3-mod_wsgi- name: copy the apache configurationcopy:src: flask-helloworld.confdest:/etc/httpd/conf.d/flask-helloworld.confowner: apachegroup: rootmode:655
这个 Ansible 角色是简单的。首先它安装依赖关系。然后,复制 Apache HTTP 服务器配置。如果你对 Ansible 角色不熟悉,请查看角色文档。
Apache HTTP 配置
接下来,通过创建 flask-helloworld.conf 来配置 Apache HTTP 服务器:
$ mkdir-p roles/base/files$ touch roles/base/files/flask-helloworld.conf
最后将以下内容添加到文件中:
<VirtualHost *>ServerName example.comWSGIDaemonProcess hello_world user=apache group=rootWSGIScriptAlias / /flaskapp/flask-helloworld.wsgi<Directory/flaskapp>WSGIProcessGroup hello_worldWSGIApplicationGroup %{GLOBAL}Require all granted</Directory></VirtualHost>
这个文件的重要部分是 WSGIScriptAlias。该指令将脚本 flask-helloworld.wsgi 映射到 /。有关 Apache HTTP 服务器和 mod_wsgi 的更多详细信息,请阅读 Flask 文档。
Flask“hello world”
最后,创建一个简单的 Flask 程序和 flask-helloworld.wsgi 脚本。
mkdir flask-helloworldtouch flask-helloworld/app.pytouch flask-helloworld/flask-helloworld.wsgi
将以下内容添加到 app.py:
from flask importFlaskapp =Flask(__name__)@app.route("/")def hello():return"Hello World!"
然后编辑 flask-helloworld.wsgi,添加这个:
import syssys.path.insert(0,'/flaskapp/')from app import app as application
构建并运行
现在是时候使用 ansible-container build 和 ansible-container run 命令来构建和运行容器。
ansible-container build
这个命令需要一些时间来完成,所以要耐心等待。
ansible-container run
你现在可以通过以下 URL 访问你的 flask 程序:http://localhost:5000/
结论
你现在已经看到如何使用 Ansible Container 来管理、构建和配置在容器中运行的程序。本例的所有配置文件和源代码在 Pagure.io 上。你可以使用此例作为基础来开始在项目中使用 Ansible Container。
下面关于 Ansible 的文章您也可能喜欢,不妨参考下:
使用 Ansible 批量管理远程服务器 http://www.linuxidc.com/Linux/2015-05/118080.htm
在 CentOS 7 中安装并使用自动化工具 Ansible http://www.linuxidc.com/Linux/2015-10/123801.htm
CentOS 7 上搭建 Jenkins+Ansible 服务 http://www.linuxidc.com/Linux/2016-12/138737.htm
Linux 下源码编译安装 Ansible 及排错记录 http://www.linuxidc.com/Linux/2017-03/141427.htm
Ansible 基础—安装与常用模块 http://www.linuxidc.com/Linux/2017-02/140216.htm
Ansible 配置及使用 http://www.linuxidc.com/Linux/2017-03/142121.htm
自动化运维工具之 Ansible 介绍及安装使用 http://www.linuxidc.com/Linux/2016-12/138104.htm
自动化运维之 Ansible 详解 http://www.linuxidc.com/Linux/2017-03/142191.htm
Ansible 入门 notify 和 handlers http://www.linuxidc.com/Linux/2017-02/140871.htm
CentOS 6.5 安装自动化工具 Ansible 和图形化工具 Tower http://www.linuxidc.com/Linux/2017-03/141422.htm
Ansible 的详细介绍:请点这里
Ansible 的下载地址:请点这里
via: https://fedoramagazine.org/build-test-applications-ansible-container/
作者:Clement Verna 译者:geekpi 校对:wxy
本文由 LCTT 原创编译,Linux 中国 荣誉推出
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-11/148799.htm






