TranslateProject/sources/tech/20171108 Build and test applications with Ansible Container.md
2017-11-13 22:38:02 +08:00

8.0 KiB
Raw Blame History

Build and test applications with Ansible Container

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:

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 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, 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 youre not familiar with Ansible roles, check out the Roles documentation.

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.

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 its 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

Youve 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. 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 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出