本文与 Maxim Svistunov 共同撰写。
与包含整个操作系统的虚拟机不同,容器仅用于容纳运行应用程序所需的软件。因此,为了高效且安全地运行容器,您需要一个操作系统,该操作系统提供安全的容器服务并充当运行容器的基础。为该任务开发的一种操作系统是 Atomic Host。
将 Atomic Host 视为 Fedora、CentOS 或 Red Hat Enterprise Linux (RHEL) 的安全、专用版本。其最佳用途是为运行容器提供可靠且易于升级的操作系统。Atomic Host 的不同格式可用于在从裸机到各种云环境的任何平台上运行。安装 Atomic Host 系统后,您可以像在其他启用容器的系统上一样使用 docker
命令。但是,Atomic Host 还附带一个名为 atomic
的附加命令,该命令扩展了您可以对容器执行的操作。
atomic
命令是用于处理 Atomic Host 系统以及这些系统上的镜像和容器的特殊工具。与 docker
命令一样,atomic
可用于列出、运行、标记、推送、拉取和删除容器和镜像。但是 atomic
还具有其他功能,可让您
- 处理 Atomic 主机: 验证、升级、回滚和解锁 Atomic Host 系统
- 管理镜像: 安装(以预定义的方式运行)、更新、验证和扫描容器镜像
- 操作容器: 列出、更新和回滚
此外,使 atomic
与 docker
命令不同的一个关键特性是,它可以处理与 Docker 守护程序无关的容器。使用 atomic
安装所谓的系统容器,您可以设置这些容器在 Docker 守护程序启动之前或在根本没有 Docker 守护程序的系统上运行。随着容器运行时变得可以替代 Docker(例如 CRI-O),atomic
命令还将允许您在这些运行时上处理容器。
如果您以前从未使用过 Atomic Host,那么完成以下 10 个任务应该会让您对可以使用它做什么有一个很好的感觉。首先获取并启动以下操作系统之一的 Atomic Host 镜像
任务 1:升级和回滚 Atomic Host
常用的 Linux 发行版使用软件包管理器,因此更新是逐个软件包进行的。Atomic 不同:更新一次性发生在整个文件系统树上。文件系统树称为“ostree”。
Ostree 更新是原子的——如果系统关闭,则更新无法完成一半。您要么拥有先前使用的 ostree(“旧”)ostree,要么拥有您要切换到的 ostree(“新”)。从一个切换到另一个是瞬间完成的。这之所以可能,是因为“新”ostree 是与“旧”ostree并行创建的,而不是修改“旧”ostree。在下次重新启动时,将使用新的 ostree。
要获取有关当前 ostree 的信息,请运行
[root@localhost ~]# atomic host status
...
Deployments:
● fedora-atomic:fedora/26/x86_64/atomic-host
Version: 26.157 (2017-10-29 14:42:37)
Commit: c099633883cd8d06895e32a14c63f6672072430c151de882223e4abe20efa7ca
...
ostree 技术(管理 ostree)类似于 Git。当前正在使用的 ostree 就像已检出到工作目录中的 Git 提交。此外,历史记录中的每个可用 ostree 都像您可以检出和使用的 Git 提交。
要升级 Atomic,请运行
[root@localhost ~]# atomic host upgrade
...
Upgraded:
GeoIP-GeoLite-data 2017.10-1.fc26 -> 2018.01-1.fc26
NetworkManager 1:1.8.2-1.fc26 -> 1:1.8.2-4.fc26
...
Removed:
...
Added:
...
此命令类似于 git pull
——它获取并部署最新的可用 ostree。在输出中,您会看到新的、升级的和删除的软件包及其版本。在重新启动之前,旧的 ostree 正在使用中。要开始使用新的 ostree,请重新启动
[root@localhost ~]# reboot
当您切换到另一个 ostree 时,配置、容器镜像和用户文件等内容会被保留,只有可执行文件和库等文件会发生更改。这样,切换到不同的 ostree 类似于在普通 Linux 发行版上升级软件包。
现在登录并再次检查状态
[root@localhost ~]# atomic host status
...
Deployments:
● ostree://fedora-atomic:fedora/26/x86_64/atomic-host
Version: 26.247 (2018-02-14 16:51:26)
Commit: 5d0283ee...
...
ostree://fedora-atomic:fedora/26/x86_64/atomic-host
Version: 26.157 (2017-10-29 14:42:37)
Commit: c0996338...
...
●
符号表示当前 ostree 是版本 26.247,对应于提交 5d0283ee
。您还会看到“旧”ostree。Atomic 始终保留两个 ostree:当前的 ostree 和旧的 ostree。
回滚到旧的 ostree 很简单
[root@localhost ~]# atomic host rollback
Moving 'c0996338...' to be first deployment
...
Downgraded:
GeoIP-GeoLite-data 2018.01-1.fc26 -> 2017.10-1.fc26
NetworkManager 1:1.8.2-4.fc26 -> 1:1.8.2-1.fc26
...
Removed:
...
Added:
...
[root@localhost ~]# reboot
系统现在再次使用旧的 ostree。
您可以比较 ostree A 和 ostree B,而无需从 A 升级到 B。您甚至不需要获取 A 或 B。只需运行
[root@localhost ~]# rpm-ostree db diff c099633 5d0283e
ostree diff commit old: c099633
ostree diff commit new: 5d0283e
Upgraded:
GeoIP-GeoLite-data 2017.10-1.fc26.noarch -> 2018.01-1.fc26.noarch
NetworkManager 1:1.8.2-1.fc26.x86_64 -> 1:1.8.2-4.fc26.x86_64
...
Removed:
...
Added:
...
您将获得与升级期间相同的信息。
任务 2:部署较旧的 Atomic Host
升级到最新的 ostree 和回滚是更通用的“部署”操作的特定情况。与 Git 中您可以检出任何提交一样,您可以部署任何版本的 ostree。
要列出可用的 ostree 版本,请拉取 ostree 元数据
[root@localhost ~]# ostree pull --commit-metadata-only --depth=-1 \
fedora-atomic:fedora/26/x86_64/atomic-host
…
上述命令中的最后一个参数指定 ostree 的远程源。它们存储在 /sysroot/ostree/deploy/fedora-atomic/deploy/*.origin
文件中。要找出系统中的此参数是什么,请查看此 .origin
文件内部
# cat /sysroot/ostree/deploy/fedora-atomic/deploy/*.origin
[origin]
refspec=fedora-atomic:fedora/26/x86_64/atomic-host
比较 ostree 版本之间的 RPM 软件包很有用,因此也拉取有关 RPM 的元数据
[root@localhost ~]# ostree pull --subpath /usr/share/rpm --depth=-1 \
fedora-atomic:fedora/26/x86_64/atomic-host
要列出可用的 ostree,请运行
[root@localhost ~]# ostree log fedora-atomic:fedora/26/x86_64/atomic-host
commit 5d0283ee9dda7d3e1d49b8b34376bd38d36c5e4774c06bb2155168cef7f08cbe
Date: 2018-02-14 16:51:26 +0000
Version: 26.247
(no subject)
commit 26957d49f289bdfe3c24ede92112a8c91a22a19f5cec860cd77190825cef3419
Date: 2018-02-13 17:27:26 +0000
Version: 26.246
(no subject)
...
假设您要部署版本 26.230。首先查看当前 ostree 版本中的软件包与 26.230 中的软件包相比如何,这很有帮助。您可以通过运行部署操作的预览来执行此操作
# atomic host deploy --preview 26.230
Resolving version '26.230'
!GeoIP-GeoLite-data-2017.10-1.fc26-noarch
=GeoIP-GeoLite-data-2018.01-1.fc26-noarch
...
要部署 26.230,请删除 --preview
选项
# atomic host deploy 26.230
与升级一样,您必须先重新启动才能运行新的 ostree。
此外,我们用于比较当前 ostree 与最新 ostree 的 rpm-ostree db diff
命令可用于任何两个 ostree 版本。
任务 3:部署其他 Atomic Host
在任务 1 和 2 中,部署了 Fedora Atomic Host ostree。但是您也可以部署另一个 Atomic Host 变体的 ostree,例如 CentOS Atomic Host 或 RHEL Atomic Host。这意味着切换到不同的操作系统,例如在最前沿的 Fedora 与稳定的 CentOS Atomic Host 或 RHEL Atomic Host 之间切换。
要切换,请添加另一个 Atomic Host 变体的存储库,然后使用以下命令切换到 CentOS
# ostree remote add --no-gpg-verify centos-atomic \
http://mirror.centos.org/centos/7/atomic/x86_64/repo
切换到 CentOS 的操作称为 rebase
# atomic host rebase \
centos-atomic:centos-atomic-host/7/x86_64/standard
# reboot
这将获取最新的 CentOS ostree 并部署它,与 Fedora ostree 的方式相同。重新启动后,您将运行 CentOS ostree
# atomic host status
State: idle; auto updates disabled
Deployments:
● ostree://centos-atomic-host:centos-atomic-host/7/x86_64/standard
Version: 7.1803 (2018-04-03 12:35:38)
Commit: cbb9dbf9c8697e9254f481fff8f399d6808cecbed0fa6cc24e659d2f50e05a3e
ostree://fedora-atomic:fedora/27/x86_64/atomic-host
Version: 27.122 (2018-04-18 23:34:24)
Commit: 931ebb3941fc49af706ac5a90ad3b5a493be4ae35e85721dabbfd966b1ecbf99
GPGSignature: Valid signature by 860E19B0AFA800A1751881A6F55E7430F5282EE4
现在您将跟踪 CentOS ostree,因此如果您运行 atomic host upgrade
,您将获得升级的 CentOS ostree,而不是 Fedora ostree。
要恢复到 Fedora Atomic Host,请执行 atomic host rollback
。
任务 4:安装软件包
在 Atomic 上,您可以使用 rpm-ostree
而不是使用 yum
和 dnf
等软件包管理命令来安装软件。在此任务中,您将在 Atomic Host 上安装和删除 RPM 软件包。
注意:在 RHEL 系统上,在安装软件之前注册并订阅系统。
安装软件包
要安装名为 nmap
的软件包,请键入以下内容
# rpm-ostree install nmap
Checking out tree da0bd96... done
…
Added:
nmap-2:7.60-7.fc27.x86_64
Run "systemctl reboot" to start a reboot
# systemctl reboot
重新启动后,再次登录。然后检查 RPM 是否已安装且可用
# rpm -q nmap
nmap-7.60-7.fc27.x86_64
删除软件包
要删除软件包,请使用 rpm-ostree uninstall
命令
# rpm-ostree uninstall nmap
Copying /etc changes: 17 modified, 0 removed, 36 added
Transaction complete; bootconfig swap: no deployment count change: 0
Removed:
nmap-2:7.60-7.fc27.x86_64
Run "systemctl reboot" to start a reboot
# systemctl reboot
重新启动后,再次登录。然后检查 RPM 是否不可用
# rpm -q nmap
package nmap is not installed
任务 5:管理镜像和容器
atomic
命令简化了容器镜像和容器的管理。容器镜像和容器之间有什么区别?与 RPM 中的命令和您使用这些命令启动的进程之间的区别相同。例如,yum install vim
安装具有 vim
命令的 vim
命令(容器镜像),而 vim file.txt
启动使用 vim
命令的实例(容器)。
以下命令显示如何在 Atomic 上管理容器镜像。第一个命令下载镜像,第二个命令列出镜像,最后一个命令删除镜像
# atomic pull docker.io/fedora/apache
# atomic images list
# atomic images delete docker.io/fedora/apache
以下命令显示如何在 Atomic 上管理容器镜像。第一个命令从镜像运行容器,第二个命令列出正在运行的容器,第三个命令停止 ID 为 ecc736143f0c
的正在运行的容器,最后一个命令删除 ID 为 ecc736143f0c
的容器
# atomic run docker.io/fedora/apache
# atomic containers list
# atomic stop ecc736143f0c
# atomic containers delete ecc736143f0c
任务 6:更新和验证镜像
使用 atomic images verify
,您可以查看容器镜像的本地和远程版本信息。如果注册表中提供了比本地可用的镜像更新的镜像,则可以更新该镜像。操作方法如下
# docker pull registry.access.redhat.com/rhel7/rhel
# atomic images verify -v registry.access.redhat.com/rhel7/rhel
registry.access.redhat.com/rhel7/rhel contains the following images:
NAME LOCAL VERSION REMOTE VERSION DIFFERS
rhel7 7.3-97 7.4-129 YES
# atomic images update registry.access.redhat.com/rhel7/rhel
Pulling registry.access.redhat.com/rhel7/rhel:latest ...
Copying blob sha256:9c …
…
# atomic images verify -v registry.access.redhat.com/rhel7/rhel
registry.access.redhat.com/rhel7/rhel contains the following images:
NAME LOCAL VERSION REMOTE VERSION DIFFERS
rhel7 7.4-129 7.4-129 NO
在此示例中,本地 rhel7 镜像的版本为 7.3-97,而注册表中提供了不同的版本 (7.4-129)。更新镜像后,您可以验证本地版本是否与远程版本匹配。
任务 7:运行特权容器
support-tools
镜像是一个容器示例,该容器打开主机的特权,并允许您从该容器运行 Atomic 中未包含的工具(例如 sosreport
和 strace
)。此容器完全打开对主机功能(IPC、网络接口和进程表)和文件系统内容(/run
、/var/log
等)的访问,以供您运行的命令(在此示例中为 sosreport
)使用。
# atomic run rhel7/support-tools sosreport
docker run -it --name support-tools --privileged --ipc=host --net=host --pid=host -e HOST=/host -e NAME=support-tools -e IMAGE=rhel7/support-tools -v /run:/run -v /var/log:/var/log -v /etc/machine-id:/etc/machine-id -v /etc/localtime:/etc/localtime -v /:/host rhel7/support-tools sosreport
This container uses privileged security switches:
...
Your sosreport has been generated and saved in:
/host/var/tmp/sosreport-c.12345-20180228004916.tar.xz
The checksum is: e1eb00750a5d44d7e8210c5a9a7ceae7
Please send this file to your support representative.
某些容器(包括 support-tools
)经过专门构建,包含 run
变量。当您使用 atomic run
启动它时,该变量设置要启动的确切 docker
命令。您可以使用 docker inspect
查看该变量
# docker inspect registry.access.redhat.com/rhel7/support-tools \
| grep run
"run": "docker run -it --name NAME --privileged --ipc=host --net=host --pid=host -e HOST=/host -e NAME=NAME -e IMAGE=IMAGE -v /run:/run -v /var/log:/var/log -v /etc/machine-id:/etc/machine-id -v /etc/localtime:/etc/localtime -v /:/host IMAGE",
任务 8:安装超级特权容器和系统容器
某些为 atomic
构建的特殊容器可以“安装”在 Atomic Host 上,以按照容器内部的信息定义的方式运行。这些包括
-
超级特权容器:具有额外特权(可以访问或修改主机系统)并包含安装步骤以准备系统运行容器的容器
-
系统容器:在安装步骤中设置为由 systemd 管理的容器,因此它们可以在系统启动时自动启动并在不需要 Docker 服务的情况下持续运行
安装和运行超级特权容器
atomic install
命令设置一个超级特权容器来运行,方法是将镜像拉取到本地系统并运行脚本以设置主机系统以运行容器。对于某些容器,这包括创建配置文件。然后,atomic run
命令使用容器内部的预设选项来运行容器,以打开额外的主机特权。
例如,要配置 rsyslog 容器,请运行
# atomic install registry.access.redhat.com/rhel7/rsyslog
Pulling registry.access.redhat.com/rhel7/rsyslog:latest ...
…
Creating directory at /host//etc/pki/rsyslog
Installing file at /host//etc/rsyslog.conf
Installing file at /host//etc/sysconfig/rsyslog
Installing file at /host//etc/logrotate.d/syslog
atomic run
命令使用容器内部的预设选项运行容器
# atomic run registry.access.redhat.com/rhel7/rsyslog
docker run -d --privileged --name rsyslog --net=host --pid=host -v
/etc/pki/rsyslog:/etc/pki/rsyslog ...
对于超级特权容器(例如 rsyslog),该容器在主机上具有额外的特权。结果是 rsyslogd 守护程序正在运行,并且可以访问主机上的配置文件和日志文件。请参阅 atomic run
命令后的完整 docker
命令输出。有关超级特权容器和特权选项描述的更多信息,请参阅 运行超级特权容器。
安装和运行系统容器
某些容器需要在 Docker 服务启动之前启动,或者在没有 Docker 服务的系统上启动。使用 atomic install
设置系统容器,该容器完全绕过 docker
服务,并使用 systemd
服务启动容器。
这是一个 etcd
系统容器的示例。
# atomic pull --storage=ostree registry.access.redhat.com/rhel7/etcd
Pulling layer 9a32f102e6778e4b3c677f1f93fa121808006d3c9e002237677248de9acb9589
# atomic install --system registry.access.redhat.com/rhel7/etcd
Extracting to /var/lib/containers/atomic/etcd.0
systemctl daemon-reload
systemd-tmpfiles --create /etc/tmpfiles.d/etcd.conf
systemctl enable etcd
# systemctl start etcd
结果是 etcd
服务启动并持续运行,然后在每次系统重新启动时重新启动。有关系统容器的更多信息,请参阅 运行系统容器。
任务 9:更新和回滚容器
如果您已设置容器在系统上运行,并且该容器的新版本可用,则可以更新它,而无需重复拉取和安装步骤。这是一个示例
# atomic pull --storage=ostree \
registry.access.redhat.com/rhel7/flannel
# atomic install --system \
registry.access.redhat.com/rhel7/flannel
# systemctl start flannel
# atomic containers list
CONTAINER ID IMAGE COMMAND
CREATED STATE BACKEND RUNTIME
flannel registry.access.redh flannel /usr/bin/f
2018-02-28 16:48 running ostree /usr/bin/r
# atomic containers update flannel
# atomic containers rollback flannel
在此示例中,在拉取、安装和启动 flannel
容器后,列出容器显示 flannel
容器正在运行,并且是 ostree 类型。当新的 flannel
容器镜像可用时,您可以使用 update
选项来获取新容器。如果要返回旧容器,请使用 rollback
选项。
任务 10:扫描镜像
atomic scan
命令对容器镜像和容器执行安全扫描。该命令可以在后端使用不同的扫描器。此示例使用 openscap
扫描器。请注意,openscap
仅适用于基于 rhel
容器镜像的容器镜像和容器。
首先,安装 openscap
容器镜像
# atomic install registry.access.redhat.com/rhel7/openscap
...
docker run --rm --privileged -v /:/host/ registry.access.redhat.com/rhel7/openscap sh /root/install.sh registry.access.redhat.com/rhel7/openscap
...
其次,下载但不安装您要扫描的镜像。在此示例中,将扫描 Apache Web 服务器容器镜像
# atomic pull registry.access.redhat.com/rhscl/httpd-24-rhel7
最后,使用 openscap
扫描器扫描 Apache 镜像
# atomic scan --scanner openscap registry.access.redhat.com/rhscl/httpd-24-rhel7
...
registry.access.redhat.com/rhscl/httpd-24-rhel7 passed the scan
...
与此扫描关联的文件位于 /var/lib/atomic/openscap/2018-04-29-22-38-58-453732
中。
上面的命令扫描了容器镜像中的常见漏洞和暴露 (CVE)。它是默认扫描类型。还有两种其他扫描类型:扫描配置合规性和修复配置合规性。
要扫描配置合规性
# atomic scan --scanner openscap \
--scan_type configuration_compliance \
registry.access.redhat.com/rhscl/httpd-24-rhel7
...
发现以下问题
...
Ensure Software Patches Installed
Severity: Important
XCCDF result: notchecked
...
与此扫描关联的文件位于 /var/lib/atomic/openscap/2018-04-29-22-39-21-584104
中。
要修复配置合规性
# atomic scan --scanner openscap \
--scan_type configuration_compliance --remediate \
registry.access.redhat.com/rhscl/httpd-24-rhel7
...
Remediating target df5ff2793065d8bf2dd8d9ab9678fc17f7c8dadfed97197795dd4ffb08cafcda.
...
有关更多信息,请参阅 Atomic CLI 参考指南 和 将 OpenSCAP 与 atomic scan 命令结合使用。
评论已关闭。