编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

红帽可引导容器BOOTC技术(四)-升级和回滚

wxchong 2024-10-19 15:52:32 开源技术 10 ℃ 0 评论

红帽提供了一个很简单的工具bootc,可以做很多BootC维护相关的工作。其中之一是事务性地从镜像仓库中获取新的操作系统更新,并支持回滚。bootc命令会去镜像仓库查询本服务器所使用的bootc镜像有无更新,如有,则下载新的bootc镜像并更新。请注意,升级不会对现有的运行环境产生任何影响,重启之后才会生效。

这是由于BootC采用了A/B升级机制来升级系统,如果你曾经使用过Fedora CoreOS或者Fedora Silverblue,那对A/B升级机制就不会陌生。类似于传统的 A/B 更新机制,BootC使用两个根文件系统分区,称为 "A" 和 "B"。这两个分区分别存储不同版本的操作系统。

BootC将整个根文件系统作为一个容器。这意味着操作系统的根文件系统是不可变的,并且可以像容器镜像一样管理和更新。假设当前系统运行在分区 A 上,并使用当前版本的容器化根文件系统。当新的操作系统版本发布时,bootc会将新版本下载到未使用的分区 B 上,创建一个新的容器化根文件系统。安装完成后,bootc会配置引导程序在下次启动时使用新的分区B和其上的新版本容器化根文件系统。

如果在新版本(分区 B)运行过程中遇到任何问题,系统可以轻松切换回旧版本(分区 A),确保服务的连续性和稳定性。Bootc 的回滚机制使得恢复到之前的稳定状态非常迅速和简单。

由于Bootc将根文件系统容器化,这增加了系统的安全性和稳定性。应用程序和用户数据可以通过独立的层进行管理,这减少了对基础系统的直接修改。

Bootc支持自动和手动控制更新过程。用户可以配置自动更新,以便在新的稳定版本发布后自动进行升级。手动更新可以通过命令行工具进行控制,允许用户在需要时执行更新和回滚。

Bootc 的 A/B 更新机制通过容器化技术提供了一个更加灵活和可靠的更新方法。它不仅保留了传统 A/B 更新的优点,如减少宕机时间和提供快速回滚,还增加了不可变性和更好的隔离特性,使其更适合现代操作系统环境。

1

准备升级环境

接下来看看具体的操作演示,在下面的演示中,我们依然使用前文中创建的bootc镜像和相关配置文件。在开始下面的操作之前,建议先清除之前在本地创建的所有镜像和容器。确信/home/liefchen/bootc/stream9/Containerfile中内容如下:

FROM quay.io/centos-bootc/centos-bootc:stream9

COPY httpd-24.container /usr/share/containers/systemd

EXPOSE 80

EXPOSE 443

# Enable the simple "automatic update containers" timer, in the same way

# that there is a simplistic bootc upgrade timer. However, you can

# obviously also customize this as you like; for example, using

# other tooling like Watchtower or explicit out-of-band control over container

# updates via e.g. Ansible or other custom logic.

RUN systemctl enable podman-auto-update.timer

RUN echo "root:redhat" | chpasswd

确信/home/liefchen/bootc/stream9/httpd-24.containerner文件内容如下:

[Unit]

Description=Run a Apache demo webserver

[Container]

# This image happens to be multiarch and somewhat maintained

Image=registry.access.redhat.com/ubi9/httpd-24

PublishPort=443:8443

PublishPort=80:8080

AutoUpdate=registry

[Service]

Restart=on-failure

RestartSec=60s

[Install]

WantedBy=default.target

创建名字为stream9-httpd-24-bootc:v1的bootc容器:

$ cd /home/liefchen/bootc/stream9/

$ sudo podman build -t quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v1 .

$ sudo podman tag quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v1 quay.io/lief_chen/bootc/stream9-httpd-24-bootc:latest

将生成的镜像push到quay.io,如果镜像仓库中有上传过,建议提前删除。上传成功后请在quay.io里把对应的仓库设置成public,否则无法被访问。

$ sudo podman push quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v1

$ sudo podman push quay.io/lief_chen/bootc/stream9-httpd-24-bootc:latest

确信/home/liefchen/bootc/config.toml中的内容如下,请根据你的实际情况修改用户名、密码以及ssh key中的内容:

[[customizations.user]]

name = "liefchen"

password = "redhat"

key = "ssh-rsa AAAAB3N......HKXkGy4H3W2s= liefchen@fedora.example.com"

groups = ["wheel"]

基于stream9-httpd-24-bootc:latest镜像转换kvm虚拟机可用的qcow2格式的镜像:

$ cd /home/liefchen/bootc/

$ sudo podman run --rm --privileged --security-opt label=type:unconfined_t -v ./config.toml:/config.toml -v ./output:/output -v /var/lib/containers/storage:/var/lib/containers/storage quay.io/centos-bootc/bootc-image-builder:latest --type qcow2 quay.io/lief_chen/bootc/stream9-httpd-24-bootc:latest

转换成功的文件位于output/qcow2/disk.qcow2,基于该文件创建虚拟机并启动,具体操作步骤参考前面的文章,这里不再赘述。接下来,我们更新stream9-httpd-24-bootc容器中的内容,并生成v2的版本,这里修改Containerfile文件,加上dnf update -y来升级系统,完整的Containerfile如下:

FROM quay.io/centos-bootc/centos-bootc:stream9

COPY httpd-24.container /usr/share/containers/systemd

EXPOSE 80

EXPOSE 443

# Enable the simple "automatic update containers" timer, in the same way

# that there is a simplistic bootc upgrade timer. However, you can

# obviously also customize this as you like; for example, using

# other tooling like Watchtower or explicit out-of-band control over container

# updates via e.g. Ansible or other custom logic.

RUN systemctl enable podman-auto-update.timer

RUN dnf update -y && dnf clean all

RUN echo "root:redhat" | chpasswd

其余的配置保持不变,之后重新编译v2的版本:

$ sudo podman build -t quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v2 .

$ sudo podman tag quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v2 quay.io/lief_chen/bootc/stream9-httpd-24-bootc:latest

按照上面的步骤推送到镜像仓库:

$ sudo podman push quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v2

$ sudo podman push quay.io/lief_chen/bootc/stream9-httpd-24-bootc:latest

这时查询镜像仓库中stream9-httpd-24-bootc的版本可正常显示v1和v2:

$ podman search --list-tags quay.io/lief_chen/bootc/stream9-httpd-24-bootc

NAME TAG

quay.io/lief_chen/bootc/stream9-httpd-24-bootc latest

quay.io/lief_chen/bootc/stream9-httpd-24-bootc v1

quay.io/lief_chen/bootc/stream9-httpd-24-bootc v2

BootC支持以下几种升级方式:

  • 通过bootc CLI命令手动升级
  • 提供了systemd的脚本可以实现定时自动检测更新并升级或者开机自动升级
  • 通过systemd的脚本实现基于containerized的BootC系统自动更新容器

前两种是系统层面的更新,最后一个是对容器化的应用的更新

2

使用命令进行更新

先来看看如果通过命令对BootC系统进行更新、

检查是否存在更新:

# bootc upgrade --check

Update available for: docker://quay.io/lief_chen/bootc/stream9-httpd-24-bootc:latest

Version: stream9.20240703.0

Digest: sha256:4f6c971d357349e7c285fc5dd1640707cb6d8932dde57c3021d98cbc650722ac

Total new layers: 70 Size: 1.1 GB

Removed layers: 1 Size: 523 bytes

Added layers: 2 Size: 224.8 MB

从输出可以看到bootc容器镜像有更新,增加了2个层,更新的大小为224.8MB。

以下命令都可以执行更新操作:

更新系统,显示更新进度,将更新部署到B分区,需要手工重启生效

# bootc upgrade

更新系统,显示更新进度,将更新部署到B分区,更新结束后自动重启并从新版本启动

# bootc upgrade --apply

此外,你可以加上--quiet参数从而不显示更新进度,例如

# bootc upgrade --quiet

# bootc upgrade --apply --quiet

显示状态:

# bootc status

此外,也可以用下文的systemd服务来进行更新

# systemctl start bootc-fetch-apply-updates.service

3

自动更新

bootc提供了两个systemd脚本用于实现自动更新:

  • systemd定时器:bootc-fetch-apply-updates.timer
  • systemd服务:bootc-fetch-apply-updates.service

工作原理是bootc-fetch-apply-updates.timer定时器类似于crontab,会定时检查镜像仓库中有无更新,如果检查到有更新,则触发bootc-fetch-apply-updates.service进行更新。默认情况下,bootc-fetch-apply-updates.timer的检查周期是OnBootSec+RandomizedDelaySec,在我的环境中OnBootSec=1h而RandomizedDelaySec=2h。

可以用systemctl list-timers --no-pager命令查看timer的状态:

bootc-fetch-apply-updates.timer是默认启动的,如果无需此功能请通过下面的命令禁止:

$ sudo systemctl disable --now bootc-fetch-apply-updates.timer

4

回滚

升级以后,如果由于各种原因,可以用bootc命令回退到以前的版本:

# bootc rollback

重启即可

5

切换

bootc可以很方便的在不同的版本之间进行切换,例如,我们上面基于CentOS Stream9的stream9-httpd-24-bootc:latest镜像的操作系统,我们通过修改Containerfile添加任意其他的内容,再生成stream9-httpd-24-bootc:v3的版本,然后push到quay以后,就通过bootc switch命令可以切换到stream9-httpd-24-bootc:v3的版本:

# bootc switch quay.io/lief_chen/bootc/stream9-httpd-24-bootc:v3

可以用bootc status或者rpm-ostree status查看状态:

同样的重启之后生效,如果切换后系统或者服务有异常,亦可通过bootc rollback进行回滚。

6

更多资源

以下是bootc相关的资料,给想了解更多关于bootc的信息的朋友:

  • Fedora/CentOS bootc文档:https://docs.fedoraproject.org/en-US/bootc/
  • Github上bootc的手册:https://containers.github.io/bootc/intro.html
  • 图形化工具podman desktop: https://github.com/containers/podman-desktop-extension-bootc?tab=readme-ov-file
  • bootc-image-builder:https://github.com/osbuild/bootc-image-builder
  • podman-bootc:https://github.com/containers/podman-bootc

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表