如何使用 Ansible 的 podman_image 和 podman_container 模块在类似 Red Hat 的系统(如 Fedora、CentOS 或 RHEL)中自动化部署一个独立运行(无需登录用户)的 Apache httpd Web Server 容器,并将端口 80 映射到 8080。

部署 Apache Web Server 的 Podman 容器

概述

部署 Apache Web Server 的 Podman 容器需要以下步骤:

  1. 确保 Podman 及其依赖项已成功安装。
  2. 创建自定义的 index.html 文件。
  3. 从容器仓库拉取 Apache httpd 的镜像。
  4. 启动 Web Server 容器,设置端口映射和卷挂载。

Ansible 模块

1. ansible.builtin.yum

安装 Podman 及其依赖项。

2. ansible.builtin.copy

创建自定义的 index.html 文件。

3. containers.podman.podman_image

从容器仓库拉取 Apache httpd 的镜像。

4. containers.podman.podman_container

运行 Web Server 容器,设置端口映射和卷挂载。


示例代码

以下是一个完整的 Ansible Playbook,用于在类似 Red Hat 的系统中部署 Apache Web Server 的 Podman 容器:

---
- name: deploy httpd container
  hosts: all
  become: true
  gather_facts: false
  vars:
    webroot: "/webroot"
  tasks:
    - name: podman installed
      ansible.builtin.yum:
        name: podman
        state: latest

    - name: pull image
      containers.podman.podman_image:
        name: httpd
        pull: true
        tag: latest

    - name: webroot present
      ansible.builtin.file:
        path: "{{ webroot }}"
        state: directory
        owner: "root"
        group: "root"
        mode: '0777'
        setype: "container_share_t"

    - name: custom index.html
      ansible.builtin.copy:
        dest: "{{ webroot }}/index.html"
        content: |
          Custom Web Page
        setype: "container_share_t"

    - name: run httpd container
      containers.podman.podman_container:
        name: webserver
        image: httpd
        state: started
        detach: true
        expose:
          - 80
        ports:
          - 8080:80
        volume:
          - "{{ webroot }}:/usr/local/apache2/htdocs/:exec"

执行过程

执行命令

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory container/podman_httpd_redhat2.yml

输出结果

PLAY [deploy httpd on container] ***********************************************
TASK [podman installed] ******************************************************
changed: [demo.example.com]
TASK [pull image] ************************************************************
changed: [demo.example.com]
TASK [webroot present] *******************************************************
changed: [demo.example.com]
TASK [custom index.html] *****************************************************
changed: [demo.example.com]
TASK [run httpd container] ****************************************************
changed: [demo.example.com]
PLAY RECAP *******************************************************************
demo.example.com           : ok=5    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试

再次运行 Playbook 时,不会重复执行已成功完成的任务:

ansible-pilot $ ansible-playbook -i virtualmachines/demo/inventory container/podman_httpd_redhat2.yml

输出结果:

PLAY [deploy httpd on container] ***********************************************
TASK [podman installed] ******************************************************
ok: [demo.example.com]
TASK [pull image] ************************************************************
ok: [demo.example.com]
TASK [webroot present] *******************************************************
changed: [demo.example.com]
TASK [custom index.html] *****************************************************
ok: [demo.example.com]
TASK [run httpd container] ****************************************************
changed: [demo.example.com]
PLAY RECAP *******************************************************************
demo.example.com           : ok=5    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前

在执行 Playbook 之前,目标主机上未安装 Podman,也没有运行的容器:

ansible-pilot $ ssh devops@demo.example.com
Last login: Thu Mar 10 13:26:52 2022 from 192.168.0.59
[devops@demo ~]$ cat /etc/os-release 
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
[devops@demo ~]$ podman images
-bash: podman: command not found
[devops@demo ~]$ podman ps -a
-bash: podman: command not found
[devops@demo ~]$ dnf info podman
Not root, Subscription Management repositories not updated
This system is not registered with an entitlement server. You can use subscription-manager to register.
Extra Packages for Enterprise Linux 8 - x86_64           4.0 MB/s |  11 MB     00:02    
Extra Packages for Enterprise Linux Modular 8 - x86_64   1.1 MB/s | 979 kB     00:00    
Red Hat Enterprise Linux 8 for x86_64 - AppStream (RPMs) 5.2 MB/s |  39 MB     00:07    
Red Hat Enterprise Linux 8 for x86_64 - BaseOS (RPMs)    5.1 MB/s |  43 MB     00:08    
Last metadata expiration check: 0:00:01 ago on Thu 10 Mar 2022 01:49:01 PM UTC.
Available Packages
Name         : podman
Epoch        : 1
Version      : 3.4.2
Release      : 9.module+el8.5.0+13852+150547f7
Architecture : x86_64
Size         : 12 M
Source       : podman-3.4.2-9.module+el8.5.0+13852+150547f7.src.rpm
Repository   : rhel-8-for-x86_64-appstream-rpms
Summary      : Manage Pods, Containers and Container Images
URL          : https://podman.io/
License      : ASL 2.0 and GPLv3+
Description  : podman (Pod Manager) is a fully featured container engine that is a simple daemonless tool. podman provides a Docker-CLI comparable command line that eases the transition from other container engines and allows the management of pods, containers and images. Simply put: alias docker=podman.

执行后

执行 Playbook 后,Apache httpd 的 Podman 容器已成功启动,并且可以通过端口 8080 访问:

bash复制

ansible-pilot $ ssh devops@demo.example.com
Last login: Thu Mar 24 17:39:48 2022 from 192.168.0.59
[devops@demo ~]$ sudo su
[root@demo devops]# cat /etc/os-release 
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.5
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.5"
[root@demo devops]# podman images
REPOSITORY               TAG         IMAGE ID      CREATED     SIZE
docker.io/library/httpd  latest      b9bd7e513e0f  7 days ago  148 MB
[root@demo devops]# podman ps -a
CONTAINER ID  IMAGE                           COMMAND        CREATED         STATUS             PORTS          NAMES
804debe95694  docker.io/library/httpd:latest  httpd-foreground  58 seconds ago  Up 58 seconds ago  0.0.0.0:8080->80/tcp  webserver
[root@demo devops]# dnf info podman
Updating Subscription Management repositories.
Last metadata expiration check: 0:12:45 ago on Thu 24 Mar 2022 05:28:22 PM UTC.
Installed Packages
Name         : podman
Epoch        : 1
Version      : 3.4.2
Release      : 9.module+el8.5.0+13852+150547f7
Architecture : x86_64
Size         : 48 M
Source       : podman-3.4.2-9.module+el8.5.0+13852+150547f7.src.rpm
Repository   : @System
From repo    : rhel-8-for-x86_64-appstream-rpms
Summary      : Manage Pods, Containers and Container Images
URL          : https://podman.io/
License      : ASL 2.0 and GPLv3+
Description  : podman (Pod Manager) is a fully featured container engine that is a simple daemonless tool. podman provides a Docker-CLI comparable command line that eases the transition from other container engines and allows the management of pods, containers and images. Simply put: alias docker=podman.

通过以上步骤,你可以使用 Ansible 在类似 Red Hat 的系统中自动化部署 Apache Web Server 的 Podman 容器,并确保其正常运行。