构建 Octavia Amphora 镜像

Octavia 是 OpenStack 负载均衡即服务 (LBaaS) 的一个运营商级参考实现。Octavia 中执行负载均衡的组件称为 amphora。Amphora 可能是虚拟机,可能是容器,也可能在裸机上运行。为裸机 amphora 安装创建镜像超出本版本范围,但未来可能会添加此功能。

先决条件

应安装 Python pip 以及 requirements.txt 文件中找到的 Python 模块。

您可以使用以下命令在 Ubuntu 上执行此操作

$ # Install python pip
$ sudo apt install python-pip
$ # Eventually create a virtualenv
$ sudo apt install python-virtualenv
$ virtualenv octavia_disk_image_create
$ source octavia_disk_image_create/bin/activate
$ # Install octavia requirements
$ cd octavia/diskimage-create
$ pip install -r requirements.txt

您的缓存目录应至少有 1GB 可用空间,工作目录需要约 1.5GB,您的镜像目标位置需要约 500MB

该脚本将使用您系统上安装的 diskimage-builder 版本,或者可以通过设置以下环境变量来覆盖它

DIB_REPO_PATH = /<some directory>/diskimage-builder
DIB_ELEMENTS = /<some directory>/diskimage-builder/elements

每个平台都需要以下软件包

Ubuntu

$ sudo apt install qemu-utils git kpartx debootstrap

Fedora、CentOS 和 Red Hat Enterprise Linux

$ sudo dnf install qemu-img git e2fsprogs policycoreutils-python-utils

测试先决条件

tox 镜像测试需要 libguestfs-tools 1.24 或更高版本。Libguestfs 允许在无需 root 权限的情况下测试 Amphora 镜像。在 Ubuntu 系统上,您还需要为运行测试的用户提供对内核的读取访问权限

$ sudo chmod 0644 /boot/vmlinuz*

用法

此脚本和相关元素将构建 Amphora 镜像。当前支持 Ubuntu 和 CentOS Stream 作为基础操作系统以及 HAProxy。该脚本可以使用 RHEL 和 Fedora 作为基础操作系统,但这些最初将不会被测试或支持。随着项目的发展和/或 diskimage-builder 项目添加对其他基础操作系统选项的支持,它们可能会为 Amphora 镜像提供。但这并不意味着它们一定得到支持或测试。

注意

如果您的云为 nova 提供了多个硬件架构,请记住在将镜像加载到 glance 时设置适当的 hw_architecture 属性。例如,在加载为“amd64”构建的 amphora 镜像时,您将添加“–property hw_architecture=’x86_64’”到您的“openstack image create”命令行。

该脚本将使用环境变量来定制超出 Octavia 项目默认设置的构建,例如添加元素。

通过使用 diskimage-create.sh 默认值(不设置命令行参数或环境变量)创建受支持和测试的镜像。随着项目的发展,我们可能会添加其他受支持的配置。

命令语法

$ diskimage-create.sh
        [-a **amd64** | armhf | aarch64 | ppc64le ]
        [-b **haproxy** ]
        [-c **~/.cache/image-create** | <cache directory> ]
        [-d **noble**/**9-stream**/**9** | <other release id> ]
        [-e]
        [-f]
        [-g **repository branch** | stable/train | stable/stein | ... ]
        [-h]
        [-i **ubuntu-minimal** | fedora | centos-minimal | rhel | rocky ]
        [-k <kernel package name> ]
        [-l <log file> ]
        [-m]
        [-n]
        [-o **amphora-x64-haproxy** | <filename> ]
        [-p]
        [-r <root password> ]
        [-s **2** | <size in GB> ]
        [-t **qcow2** | tar ]
        [-v]
        [-w <working directory> ]
        [-x]
        [-y]

    '-a' is the architecture type for the image (default: amd64)
    '-b' is the backend type (default: haproxy)
    '-c' is the path to the cache directory (default: ~/.cache/image-create)
    '-d' distribution release id (default on ubuntu: noble)
    '-e' enable complete mandatory access control systems when available (default: permissive)
    '-f' disable tmpfs for build
    '-g' build the image for a specific OpenStack Git branch (default: current repository branch)
    '-h' display help message
    '-i' is the base OS (default: ubuntu-minimal)
    '-k' is the kernel meta package name, currently only for ubuntu-minimal base OS (default: linux-image-virtual)
    '-l' is output logfile (default: none)
    '-m' enable vCPU pinning optimizations (default: disabled)
    '-n' disable sshd (default: enabled)
    '-o' is the output image file name
    '-p' install amphora-agent from distribution packages (default: disabled)"
    '-r' enable the root account in the generated image (default: disabled)
    '-s' is the image size to produce in gigabytes (default: 2)
    '-t' is the image type (default: qcow2)
    '-v' display the script version
    '-w' working directory for image building (default: .)
    '-x' enable tracing for diskimage-builder
    '-y' enable FIPS 140-2 mode in the amphora image

为备用分支构建镜像

默认情况下,diskimage-create.sh 脚本将使用 Octavia Git 仓库的分支构建 amphora 镜像。如果您需要特定分支的镜像,例如“stable/train”,则需要使用“-g”选项和分支名称指定。例如,对于“stable/train”将是

diskimage-create.sh -g stable/train

高级 Git 分支/引用基于镜像

如果您需要从本地仓库或使用特定的 Git 引用或分支构建镜像,则需要为 diskimage-builder 设置一些环境变量。

注意

这些高级设置将覆盖 diskimage-create.sh 的“-g”设置。

从本地 Octavia 仓库构建

将 DIB_REPOLOCATION_amphora_agent 变量设置为包含 amphora agent 的 Git 仓库的位置

export DIB_REPOLOCATION_amphora_agent=/opt/stack/octavia

使用特定 Git 引用构建

将 DIB_REPOREF_amphora_agent 变量设置为指向 amphora agent 的 Git 分支或引用

export DIB_REPOREF_amphora_agent=refs/changes/40/674140/7

有关更多信息和示例,请参阅下面的环境变量部分。

Amphora Agent 上限约束

您可能还需要指定将用于构建镜像的 OpenStack upper-constraints.txt 文件的版本。例如,要指定“stable/train”上限约束 Git 分支,请设置以下环境变量

export DIB_REPOLOCATION_upper_constraints=https://opendev.org/openstack/requirements/raw/branch/stable/2025.2/upper-constraints.txt

有关更多信息,请参阅OpenStack 项目的依赖管理

环境变量

这些是可以设置为覆盖脚本默认值的可选环境变量。

DIB_REPOLOCATION_amphora_agent
DIB_REPOREF_amphora_agent
  • 镜像中 amphora-agent 代码要检出的 Git 引用。

  • 默认值:当前分支

  • 示例:stable/stein

  • 示例:refs/changes/40/674140/7

DIB_REPOLOCATION_octavia_lib
DIB_REPOREF_octavia_lib
  • 镜像中 octavia-lib 代码要检出的 Git 引用。

  • 默认值:已发布 OpenStack 系列安装的主分支或稳定分支。

  • 示例:stable/ussuri

  • 示例:refs/changes/19/744519/2

DIB_REPOLOCATION_upper_constraints
CLOUD_INIT_DATASOURCES
  • 逗号分隔的 cloud-int 数据源列表

  • 默认值:ConfigDrive

  • 选项:NoCloud、ConfigDrive、OVF、MAAS、Ec2、<其他>

  • 参考:https://launchpad.net/cloud-init

DIB_DISTRIBUTION_MIRROR
  • 用于所选基础操作系统的镜像的 URL

  • 默认值:无

DIB_ELEMENTS
  • 覆盖用于构建镜像的元素

  • 默认值:无

DIB_LOCAL_ELEMENTS
  • 要添加到构建中的元素(需要指定 DIB_LOCAL_ELEMENTS_PATH)

  • 默认值:无

DIB_LOCAL_ELEMENTS_PATH
  • 本地元素目录的路径

  • 默认值:无

DIB_REPO_PATH
OCTAVIA_REPO_PATH
DIB_OCTAVIA_AMP_USE_NFTABLES
  • 一个布尔值,配置 amphora 镜像内的 nftables

  • 需要启用 SR-IOV 的 amphora

  • 默认值:True

使用分发包为 amphora agent

默认情况下,amphora agent 从 Octavia Git 仓库安装。要使用分发包,请使用“-p”选项。

请注意,这需要启用所需仓库的基础系统镜像(例如 CentOS/Fedora 的 RDO 仓库)。这些变量中的一个必须设置

DIB_LOCAL_IMAGE
  • 本地下载的镜像的路径

  • 默认值:无

DIB_CLOUD_IMAGES
  • 从下载镜像的目录基本 URL

  • 默认值:取决于分发版

RHEL 特定变量

构建基于 RHEL 的镜像需要
  • Red Hat Enterprise Linux KVM Guest 镜像,从 Red Hat 客户门户手动下载。将 DIB_LOCAL_IMAGE 变量设置为指向该文件。更多详细信息请参见:<DIB_REPO_PATH>/elements/rhel

  • 如果您想从官方分发包安装 amphora agent,则需要 Red Hat 订阅匹配的 Red Hat OpenStack Platform 仓库(需要在 diskimage-create.sh 中设置 -p 选项)。根据您的配置设置所需的注册参数。更多详细信息请参见:<DIB_REPO_PATH>/elements/rhel-common

这是一个使用 Satellite 注册(激活密钥必须启用 OSP 仓库)的示例

$ export DIB_LOCAL_IMAGE='/tmp/rhel-server-8.0-x86_64-kvm.qcow2'

$ export REG_METHOD='portal' REG_REPOS='rhel-8-server-openstack-15-rpms'

$ export REG_USER='<user>' REG_PASSWORD='<password>' REG_AUTO_ATTACH=true

此示例使用通过 Satellite 注册(激活密钥必须启用 OSP 仓库)

$ export DIB_LOCAL_IMAGE='/tmp/rhel-server-8.1-x86_64-kvm.qcow2'

$ export REG_METHOD='satellite' REG_ACTIVATION_KEY="<activation key>"

$ export REG_SAT_URL="<satellite url>" REG_ORG="<satellite org>"

在带有 tox 的 virtualenv 中构建

要使用 Python 依赖项的 virtualenv,您可以运行 tox。请注意,您可能仍然需要在主机上安装二进制依赖项才能成功构建。

如果您希望自定义构建,请修改 tox.ini 以将相关环境变量或命令行参数传递给 diskimage-create.sh 脚本。

$ tox -e build

容器支持

使用此脚本创建的 tar 文件导入 Docker 的命令行是

$ docker import - image:amphora-x64-haproxy < amphora-x64-haproxy.tar

参考

本文档和脚本利用 OpenStack TripleO 和 Sahara 团队的先前工作。感谢所有为创建 Octavia Amphora 镜像提供出色基础的人们。