mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translated
This commit is contained in:
parent
d17b6c0018
commit
3887acca01
@ -1,237 +0,0 @@
|
||||
translating---geekpi
|
||||
|
||||
# [Build and test applications with Ansible Container][1]
|
||||
|
||||
![](https://fedoramagazine.org/wp-content/uploads/2017/10/ansible-container-945x400.jpg)
|
||||
|
||||
Containers are an increasingly popular development environment. As a developer you have a large choice of tools to manage your containers. This article introduces you to Ansible Container and shows how you can run and test your application in a production-like environment.
|
||||
|
||||
### Getting started
|
||||
|
||||
This example uses a simple Flask Hello World application. This application is served by an Apache HTTP server just like in production. First, install the requisite _docker_ package:
|
||||
|
||||
```
|
||||
sudo dnf install docker
|
||||
```
|
||||
|
||||
Ansible Container needs to communicate with the docker service through its local socket. The following commands change the socket ownership, and add you to a _docker_ group that can access the socket:
|
||||
|
||||
```
|
||||
sudo groupadd docker && sudo gpasswd -a $USER docker
|
||||
MYGRP=$(id -g) ; newgrp docker ; newgrp $MYGRP
|
||||
```
|
||||
|
||||
Run the _id_ command to ensure the _docker_ group is listed in your group memberships. Finally, enable and start the docker service [using sudo][2]:
|
||||
|
||||
```
|
||||
sudo systemctl enable docker.service
|
||||
sudo systemctl start docker.service
|
||||
```
|
||||
|
||||
### Setting up Ansible Container
|
||||
|
||||
Ansible Container enables you to build container images and orchestrate them using only Ansible playbooks. The application is described in a single YAML file, and instead of using a Dockerfile, lists Ansible roles that make up the container images.
|
||||
|
||||
Unfortunately Ansible Container is not yet available as an RPM package in Fedora. To install it, use the python3 virtual environment module.
|
||||
|
||||
```
|
||||
mkdir ansible-container-flask-example
|
||||
cd ansible-container-flask-example
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install ansible-container[docker]
|
||||
```
|
||||
|
||||
These commands install Ansible Container with the Docker engine. Ansible Container provides three engines: Docker, Kubernetes and Openshift.
|
||||
|
||||
### Setting up the project
|
||||
|
||||
Now that Ansible Container is installed, set up the project. Ansible Container provides a simple command to create all files needed to get started:
|
||||
|
||||
```
|
||||
ansible-container init
|
||||
```
|
||||
|
||||
Now look at the files this command created in the current directory:
|
||||
|
||||
* _ansible.cfg_
|
||||
|
||||
* _ansible-requirements.txt_
|
||||
|
||||
* _container.yml_
|
||||
|
||||
* _meta.yml_
|
||||
|
||||
* _requirements.yml_
|
||||
|
||||
This project uses only the _container.yml_ file to describe the application services. For more information about the other files, check out the [Getting Started][3] documentation of Ansible Container.
|
||||
|
||||
### Defining the container
|
||||
|
||||
Update _container.yml_ as follows:
|
||||
|
||||
```
|
||||
version: "2"
|
||||
settings:
|
||||
conductor:
|
||||
# The Conductor 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-helloworld
|
||||
|
||||
services:
|
||||
# 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:
|
||||
- base
|
||||
ports:
|
||||
- "5000:80"
|
||||
command: ["/usr/bin/dumb-init", "httpd", "-DFOREGROUND"]
|
||||
volumes:
|
||||
- $PWD/flask-helloworld:/flaskapp:Z
|
||||
```
|
||||
|
||||
The _conductor_ section updates the base setting to use a Fedora 26 container base image.
|
||||
|
||||
The _services_ section adds the _web_ service. This service uses Fedora 26 and has a _role_ called _base_ to be defined later. It also sets up the port mapping between the container and host. The Apache HTTP server serves the Flask application on port 80 of the container, which redirects to port 5000 of the host. Then this file defines a _volume_ that mounts the Flask application source code to _/flaskapp_ in the container.
|
||||
|
||||
Finally the _command_ configuration runs when the container starts. This example uses [dumb-init][4], a simple process supervisor and init system to start the Apache HTTP server.
|
||||
|
||||
### Ansible role
|
||||
|
||||
Now that the container is setup, create an Ansible role to install and configure the dependencies needed by the Flask application. First, create the _base_ role.
|
||||
|
||||
```
|
||||
mkdir -p roles/base/tasks
|
||||
touch roles/base/tasks/main.yml
|
||||
```
|
||||
|
||||
Now edit the _main.yml_ file so that it looks like this:
|
||||
|
||||
```
|
||||
---
|
||||
- name: Install dependencies
|
||||
dnf: pkg={{item}} state=present
|
||||
with_items:
|
||||
- python3-flask
|
||||
- dumb-init
|
||||
- httpd
|
||||
- python3-mod_wsgi
|
||||
|
||||
- name: copy the apache configuration
|
||||
copy:
|
||||
src: flask-helloworld.conf
|
||||
dest: /etc/httpd/conf.d/flask-helloworld.conf
|
||||
owner: apache
|
||||
group: root
|
||||
mode: 655
|
||||
```
|
||||
|
||||
This Ansible role is a simple one. First it installs dependencies. Then, it copies the Apache HTTP server configuration. If you’re not familiar with Ansible roles, check out the [Roles documentation][5].
|
||||
|
||||
### Apache HTTP configuration
|
||||
|
||||
Next, configure the Apache HTTP server by creating the _flask-helloworld.conf_ file:
|
||||
|
||||
```
|
||||
$ mkdir -p roles/base/files
|
||||
$ touch roles/base/files/flask-helloworld.conf
|
||||
```
|
||||
|
||||
And finally add the following to the file:
|
||||
|
||||
```
|
||||
<VirtualHost *>
|
||||
ServerName example.com
|
||||
|
||||
WSGIDaemonProcess hello_world user=apache group=root
|
||||
WSGIScriptAlias / /flaskapp/flask-helloworld.wsgi
|
||||
|
||||
<Directory /flaskapp>
|
||||
WSGIProcessGroup hello_world
|
||||
WSGIApplicationGroup %{GLOBAL}
|
||||
Require all granted
|
||||
</Directory>
|
||||
</VirtualHost>
|
||||
```
|
||||
|
||||
The important part of this file is the _WSGIScriptAlias._ This instruction maps the script _flask-helloworld.wsgi_ to the “/” URL. For more details on Apache HTTP server and mod_wsgi, read [the Flask documentation][6].
|
||||
|
||||
### Flask “hello world”
|
||||
|
||||
Finally, create a simple Flask application and the _ flask-helloworld.wsgi_ script.
|
||||
|
||||
```
|
||||
mkdir flask-helloworld
|
||||
touch flask-helloworld/app.py
|
||||
touch flask-helloworld/flask-helloworld.wsgi
|
||||
```
|
||||
|
||||
Add the following to _app.py_ **:**
|
||||
|
||||
```
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return "Hello World!"
|
||||
```
|
||||
|
||||
Then edit _flask-helloworld.wsgi_ to add this:
|
||||
|
||||
```
|
||||
import sys
|
||||
sys.path.insert(0, '/flaskapp/')
|
||||
|
||||
from app import app as application
|
||||
```
|
||||
|
||||
### Build and run
|
||||
|
||||
Now it’s time to build and run the container with the _ansible-container build_ and _ansible-container run_ commands.
|
||||
|
||||
```
|
||||
ansible-container build
|
||||
```
|
||||
|
||||
This command takes a bit of time to complete, so be patient.
|
||||
|
||||
```
|
||||
ansible-container run
|
||||
```
|
||||
|
||||
You can now access your flask application at this URL: _http://localhost:5000/_
|
||||
|
||||
### Conclusion
|
||||
|
||||
You’ve now seen how to use Ansible Container to manage, build and configure your applications running inside a container. All the configuration files and the source code of this example are hosted on [Pagure.io][7]. You can use this example as the base to start using Ansible Container on your projects.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/build-test-applications-ansible-container/
|
||||
|
||||
作者:[Clement Verna ][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://fedoramagazine.org/author/cverna/
|
||||
[1]:https://fedoramagazine.org/build-test-applications-ansible-container/
|
||||
[2]:https://fedoramagazine.org/howto-use-sudo/
|
||||
[3]:https://docs.ansible.com/ansible-container/getting_started.html
|
||||
[4]:https://github.com/Yelp/dumb-init
|
||||
[5]:http://docs.ansible.com/ansible/latest/playbooks_reuse_roles.html
|
||||
[6]:http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/
|
||||
[7]:https://pagure.io/ansible-container-flask-example
|
@ -0,0 +1,235 @@
|
||||
# [使用 Ansible Container 构建和测试应用程序][1]
|
||||
|
||||
![](https://fedoramagazine.org/wp-content/uploads/2017/10/ansible-container-945x400.jpg)
|
||||
|
||||
容器是一个日益流行的开发环境。作为一名开发人员,你可以选择多种工具来管理你的容器。本文将向你介绍 Ansible Container,并展示如何在类似生产环境中运行和测试你的应用程序。
|
||||
|
||||
### 入门
|
||||
|
||||
这个例子使用了一个简单的 Flask Hello World 程序。这个程序就像在生产中一样由 Apache HTTP 服务器提供服务。首先,安装必要的 _docker_ 包:
|
||||
|
||||
```
|
||||
sudo dnf install docker
|
||||
```
|
||||
|
||||
Ansible Container 需要通过本地套接字与 Docker 服务进行通信。以下命令将更改套接字所有者,并将你添加到可访问此套接字的 _docker_ 组:
|
||||
|
||||
```
|
||||
sudo groupadd docker && sudo gpasswd -a $USER docker
|
||||
MYGRP=$(id -g) ; newgrp docker ; newgrp $MYGRP
|
||||
```
|
||||
|
||||
运行 _id_ 命令以确保 _docker_ 组在你的组成员中列出。最后,[使用 sudo ][2]启用并启动 docker 服务:
|
||||
|
||||
```
|
||||
sudo systemctl enable docker.service
|
||||
sudo systemctl start docker.service
|
||||
```
|
||||
|
||||
### 设置 Ansible Container
|
||||
|
||||
Ansible Container 使你能够构建容器镜像并使用 Ansible playbook 进行编排。该程序在一个 YAML 文件中描述,而不是使用 Dockerfile,列出组成容器镜像的 Ansible 角色。
|
||||
|
||||
不幸的是,Ansible Container 在 Fedora 中没有 RPM 包可用。要安装它,请使用 python3 虚拟环境模块。
|
||||
|
||||
```
|
||||
mkdir ansible-container-flask-example
|
||||
cd ansible-container-flask-example
|
||||
python3 -m venv .venv
|
||||
source .venv/bin/activate
|
||||
pip install ansible-container[docker]
|
||||
```
|
||||
|
||||
这些命令用 Docker 引擎安装 Ansible Container。 Ansible Container 提供三种引擎:Docker、Kubernetes 和 Openshift。
|
||||
|
||||
### 设置项目
|
||||
|
||||
现在已经安装了 Ansible Container,接着设置这个项目。Ansible Container 提供了一个简单的命令来创建启动所需的所有文件:
|
||||
|
||||
```
|
||||
ansible-container init
|
||||
```
|
||||
|
||||
来看看这个命令在当前目录中创建的文件:
|
||||
|
||||
* _ansible.cfg_
|
||||
|
||||
* _ansible-requirements.txt_
|
||||
|
||||
* _container.yml_
|
||||
|
||||
* _meta.yml_
|
||||
|
||||
* _requirements.yml_
|
||||
|
||||
该项目仅使用 _container.yml_ 来描述程序服务。有关其他文件的更多信息,请查看 Ansible Container 的[入门][3]文档。
|
||||
|
||||
### 定义容器
|
||||
|
||||
如下更新 _container.yml_:
|
||||
|
||||
```
|
||||
version: "2"
|
||||
settings:
|
||||
conductor:
|
||||
# The Conductor 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-helloworld
|
||||
|
||||
services:
|
||||
# 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:
|
||||
- base
|
||||
ports:
|
||||
- "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。然后这个文件定义了一个 _volume_,它将 Flask 程序源代码挂载到容器中的 _/flaskapp_ 中。
|
||||
|
||||
最后,容器启动时运行 _command_ 配置。这个例子中使用 [dumb-init][4],一个简单的进程管理器并初始化系统启动 Apache HTTP 服务器。
|
||||
|
||||
### Ansible 角色
|
||||
|
||||
现在已经设置完了容器,创建一个 Ansible 角色来安装并配置 Flask 程序所需的依赖关系。首先,创建 _base_ 角色。
|
||||
|
||||
```
|
||||
mkdir -p roles/base/tasks
|
||||
touch roles/base/tasks/main.yml
|
||||
```
|
||||
|
||||
现在编辑 _main.yml_ ,它看起来像这样:
|
||||
|
||||
```
|
||||
---
|
||||
- name: Install dependencies
|
||||
dnf: pkg={{item}} state=present
|
||||
with_items:
|
||||
- python3-flask
|
||||
- dumb-init
|
||||
- httpd
|
||||
- python3-mod_wsgi
|
||||
|
||||
- name: copy the apache configuration
|
||||
copy:
|
||||
src: flask-helloworld.conf
|
||||
dest: /etc/httpd/conf.d/flask-helloworld.conf
|
||||
owner: apache
|
||||
group: root
|
||||
mode: 655
|
||||
```
|
||||
|
||||
这个 Ansible 角色是简单的。首先它安装依赖关系。然后,复制 Apache HTTP 服务器配置。如果你对 Ansible 角色不熟悉,请查看[角色文档][5]。
|
||||
|
||||
### Apache HTTP 配置
|
||||
|
||||
接下来,通过创建 _flask-helloworld.conf_ 来配置 Apache HTTP 服务器:
|
||||
|
||||
```
|
||||
$ mkdir -p roles/base/files
|
||||
$ touch roles/base/files/flask-helloworld.conf
|
||||
```
|
||||
|
||||
最后将以下内容添加到文件中:
|
||||
|
||||
```
|
||||
<VirtualHost *>
|
||||
ServerName example.com
|
||||
|
||||
WSGIDaemonProcess hello_world user=apache group=root
|
||||
WSGIScriptAlias / /flaskapp/flask-helloworld.wsgi
|
||||
|
||||
<Directory /flaskapp>
|
||||
WSGIProcessGroup hello_world
|
||||
WSGIApplicationGroup %{GLOBAL}
|
||||
Require all granted
|
||||
</Directory>
|
||||
</VirtualHost>
|
||||
```
|
||||
|
||||
这个文件的重要部分是 _WSGIScriptAlias_。该指令将脚本 _flask-helloworld.wsgi_ 映射到 “/”。有关 Apache HTTP 服务器和 mod_wsgi 的更多详细信息,请阅读[ Flask 文档][6]。
|
||||
|
||||
### Flask “hello world”
|
||||
|
||||
最后,创建一个简单的 Flask 程序和 _ flask-helloworld.wsgi_ 脚本。
|
||||
|
||||
```
|
||||
mkdir flask-helloworld
|
||||
touch flask-helloworld/app.py
|
||||
touch flask-helloworld/flask-helloworld.wsgi
|
||||
```
|
||||
|
||||
将以下内容添加到 _app.py_ **:**
|
||||
|
||||
```
|
||||
from flask import Flask
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/")
|
||||
def hello():
|
||||
return "Hello World!"
|
||||
```
|
||||
|
||||
然后编辑 _flask-helloworld.wsgi_ 添加这个:
|
||||
|
||||
```
|
||||
import sys
|
||||
sys.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][7] 上。你可以使用此例作为基础来开始在项目中使用 Ansible Container。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://fedoramagazine.org/build-test-applications-ansible-container/
|
||||
|
||||
作者:[Clement Verna ][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://fedoramagazine.org/author/cverna/
|
||||
[1]:https://fedoramagazine.org/build-test-applications-ansible-container/
|
||||
[2]:https://fedoramagazine.org/howto-use-sudo/
|
||||
[3]:https://docs.ansible.com/ansible-container/getting_started.html
|
||||
[4]:https://github.com/Yelp/dumb-init
|
||||
[5]:http://docs.ansible.com/ansible/latest/playbooks_reuse_roles.html
|
||||
[6]:http://flask.pocoo.org/docs/0.12/deploying/mod_wsgi/
|
||||
[7]:https://pagure.io/ansible-container-flask-example
|
Loading…
Reference in New Issue
Block a user