5.4. 使用带有镜像 registry 的 Windows 容器
Windows Machine Config Operator (WMCO)可以使用 ImageDigestMirrorSet
(IDMS) 或 ImageTagMirrorSet
(ITMS) 对象从 registry 镜像拉取镜像,而不是从公共 registry 中拉取镜像。
镜像 registry 具有以下优点:
- 避免公共 registry 中断
- 加速节点和 pod 创建
- 从机构的防火墙后拉取镜像
镜像 registry 也可以在断开连接的环境中的 OpenShift Container Platform 集群中使用,或 air-gapped 网络。断开连接的网络是一个受限网络,没有直接互联网连接。由于集群无法访问互联网,所以无法引用任何外部容器镜像。
使用镜像 registry 需要以下常规步骤:
- 使用 Red Hat Quay 等工具创建镜像 registry。
- 创建容器镜像 registry 凭据文件。
- 将镜像从在线镜像存储库复制到您的镜像 registry。
有关这些步骤的详情,请参考"关于断开连接的安装镜像"。
在创建镜像 registry 并镜像镜像后,您可以使用 ImageDigestMirrorSet
(IDMS)或 ImageTagMirrorSet
(ITMS)对象将集群配置为从镜像 registry 拉取镜像,而无需更新每个 pod 规格。IDMS 和 ITMS 对象重定向从源镜像 registry 上的存储库拉取镜像并通过镜像存储库解析的请求。
如果对 IDMS 或 ITMS 对象进行了更改,WMCO 将使用新信息自动更新 Windows 节点上的相应 hosts.toml
文件。请注意,WMCO 在镜像设置更改时按顺序更新每个 Windows 节点。因此,随着集群中的 Windows 节点数量增加这些更新所需的时间。
另外,因为 WMCO 配置的 Windows 节点依赖于容器运行时,WMCO 可确保容器的配置文件与 registry 设置保持同步。对于新节点,这些文件在创建时复制到实例。对于现有节点,在激活镜像 registry 后,registry 控制器使用 SSH 访问每个节点并复制生成的配置文件,替换任何现有文件。
您可以使用带有机器集或 Bring-Your-Own-Host (BYOH) Windows 节点的 mirror registry。
其他资源
5.4.1. 了解镜像 registry 仓库镜像
通过设置容器 registry 存储库镜像,您可以执行以下任务:
- 配置 OpenShift Container Platform 集群,以便重定向从源镜像 registry 上的存储库拉取(pull)镜像的请求,并通过已镜像 (mirror) 的镜像 registry 上的存储库来解决该请求。
- 为每个目标存储库识别多个已镜像 (mirror)的存储库,以确保如果一个镜像停止运作,仍可使用其他镜像。
OpenShift Container Platform 中的存储库镜像包括以下属性:
- 镜像拉取(pull)可应对 registry 停机的问题。
- 在断开连接的环境中的集群可以从关键位置(如 quay.io)拉取镜像,并让公司防火墙后面的 registry 提供请求的镜像。
- 发出镜像拉取(pull)请求时尝试特定 registry 顺序,通常最后才会尝试持久性 registry。
-
您输入的镜像信息会添加到 OpenShift Container Platform 集群中每个 Windows 节点上的适当的
hosts.toml
容器配置文件中。 - 当节点从源存储库中请求镜像时,它会依次尝试每个已镜像的存储库,直到找到所请求的内容。如果所有镜像均失败,集群则会尝试源存储库。如果成功,则镜像拉取至节点中。
可通过以下方式设置存储库镜像:
在 OpenShift Container Platform 安装中:
通过拉取(pull) OpenShift Container Platform 所需的容器镜像,然后将这些镜像放至公司防火墙后,即可将 OpenShift Container Platform 安装到受限网络中的数据中心。
安装 OpenShift Container Platform 后:
如果您没有在 OpenShift Container Platform 安装过程中配置镜像,您可以在安装后使用以下自定义资源 (CR) 对象之一进行配置:
-
ImageDigestMirrorSet
(IDMS).此对象允许您使用摘要规格从镜像 registry 中拉取镜像。IDMS CR 可让您设置回退策略,在镜像拉取失败时继续尝试从源 registry 中拉取。 -
ImageTagMirrorSet
(ITMS)。此对象允许您使用镜像标签从已镜像的 registry 中拉取镜像。ITMS CR 可让您设置回退策略,在镜像拉取失败时继续尝试从源 registry 中拉取。
-
每个自定义资源对象都标识以下信息:
- 您希望镜像 (mirror) 的容器镜像存储库的源。
- 您希望为其提供从源存储库请求的内容的每个镜像存储库的单独条目。
Windows Machine Config Operator (WMCO)会监视 IDMS 和 ITMS 资源的更改,并生成一组 host.toml
容器配置文件,每个源 registry 有一个文件,以及这些更改。然后,WMCO 会更新任何现有 Windows 节点以使用新的 registry 配置。
在使用镜像 registry 添加 Windows 节点前,必须创建 IDMS 和 ITMS 对象。
5.4.2. 配置镜像 registry 存储库镜像
您可以创建安装后镜像配置自定义资源 (CR),将源镜像 registry 中的镜像拉取请求重定向到镜像 registry。
通过 ImageDigestMirrorSet
和 ImageTagMirrorSet
对象镜像的 Windows 镜像具有特定的命名要求。命名空间和镜像镜像的镜像名称的最终部分必须与正在镜像的镜像匹配。例如,当镜像 mcr.microsoft.com/oss/kubernetes/pause:3.9
镜像时,镜像镜像必须具有 <mirror_registry>/<optional_namespaces>/oss/kubernetes/pause:3.9
格式。optional_namespaces
可以是任意数量的前导存储库命名空间。
先决条件
-
使用具有
cluster-admin
角色的用户访问集群。
流程
通过以下方法配置已镜像的存储库:
- 按照 Red Hat Quay 存储库镜像中所述,使用 Red Hat Quay 来设置已镜像的存储库。使用 Red Hat Quay 有助于您将镜像从一个存储库复制到另一存储库,并可随着时间的推移重复自动同步这些存储库。
使用
skopeo
等工具手动将镜像从源存储库复制到已镜像的存储库。例如:在 Red Hat Enterprise Linux(RHEL 7 或 RHEL 8)系统上安装 skopeo RPM 软件包后,使用
skopeo
命令,如下例所示:Copy to Clipboard Copied! Toggle word wrap Toggle overflow skopeo copy --all \ docker://registry.access.redhat.com/ubi9/ubi-minimal:latest@sha256:5cf... \ docker://example.io/example/ubi-minimal
$ skopeo copy --all \ docker://registry.access.redhat.com/ubi9/ubi-minimal:latest@sha256:5cf... \ docker://example.io/example/ubi-minimal
在本例中,您有一个名为
example.io
的容器镜像 registry,其中包含一个名为example
的镜像存储库,您要将ubi9/ubi-minimal
镜像从registry.access.redhat.com
复制到其中。创建已镜像的 registry 后,您可以将 OpenShift Container Platform 集群配置为将源存储库的请求重定向到已镜像的存储库。
重要您必须镜像
mcr.microsoft.com/oss/kubernetes/pause:3.9
镜像。例如,您可以使用以下skopeo
命令镜像镜像:Copy to Clipboard Copied! Toggle word wrap Toggle overflow skopeo copy \ docker://mcr.microsoft.com/oss/kubernetes/pause:3.9\ docker://example.io/oss/kubernetes/pause:3.9
$ skopeo copy \ docker://mcr.microsoft.com/oss/kubernetes/pause:3.9\ docker://example.io/oss/kubernetes/pause:3.9
- 登录您的 OpenShift Container Platform 集群。
根据需要,创建一个
ImageDigestMirrorSet
或ImageTagMirrorSet
CR,将源和镜像(mirror)替换为您自己的 registry、存储库对和镜像:Copy to Clipboard Copied! Toggle word wrap Toggle overflow apiVersion: config.openshift.io/v1 kind: ImageDigestMirrorSet metadata: name: ubi9repo spec: imageDigestMirrors: - mirrors: - example.io/example/ubi-minimal - example.com/example2/ubi-minimal source: registry.access.redhat.com/ubi9/ubi-minimal mirrorSourcePolicy: AllowContactingSource - mirrors: - mirror.example.com source: registry.redhat.io mirrorSourcePolicy: NeverContactSource - mirrors: - docker.io source: docker-mirror.internal mirrorSourcePolicy: AllowContactingSource
apiVersion: config.openshift.io/v1
1 kind: ImageDigestMirrorSet
2 metadata: name: ubi9repo spec: imageDigestMirrors:
3 - mirrors: - example.io/example/ubi-minimal
4 - example.com/example2/ubi-minimal
5 source: registry.access.redhat.com/ubi9/ubi-minimal
6 mirrorSourcePolicy: AllowContactingSource
7 - mirrors: - mirror.example.com source: registry.redhat.io mirrorSourcePolicy: NeverContactSource - mirrors: - docker.io source: docker-mirror.internal mirrorSourcePolicy: AllowContactingSource
- 1
- 指明此 CR 要使用的 API。这必须是
config.openshift.io/v1
。 - 2
- 根据 pull 类型指示对象类型:
-
ImageDigestMirrorSet
:提取摘要引用镜像。 -
ImageTagMirrorSet
:提取标签引用镜像。
-
- 3
- 表示镜像拉取方法的类型,请执行以下任一方法:
-
imageDigestMirrors
:用于ImageDigestMirrorSet
CR。 -
imageTagMirrors
:用于ImageTagMirrorSet
CR。
-
- 4
- 指明镜像 registry 和存储库的名称。
- 5
- 可选:指定每个目标仓库的二级镜像存储库。如果一个镜像停机,则目标仓库可以使用另一个镜像。
- 6
- 指明 registry 和存储库源,这是在镜像拉取规格中引用的存储库。
- 7
- 可选:如果镜像拉取失败,则指示回退策略:
-
AllowContactingSource
:允许继续尝试从源存储库拉取镜像。这是默认值。 -
NeverContactSource
: 防止继续尝试从源存储库拉取镜像。
-
创建新对象:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc create -f registryrepomirror.yaml
$ oc create -f registryrepomirror.yaml
要检查是否应用了镜像的配置设置,请在其中一个节点上执行以下操作。
列出您的节点:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc get node
$ oc get node
输出示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NAME STATUS ROLES AGE VERSION ip-10-0-137-44.ec2.internal Ready worker 7m v1.29.4 ip-10-0-138-148.ec2.internal Ready master 11m v1.29.4 ip-10-0-139-122.ec2.internal Ready master 11m v1.29.4 ip-10-0-147-35.ec2.internal Ready worker 7m v1.29.4 ip-10-0-153-12.ec2.internal Ready worker 7m v1.29.4 ip-10-0-154-10.ec2.internal Ready master 11m v1.29.4
NAME STATUS ROLES AGE VERSION ip-10-0-137-44.ec2.internal Ready worker 7m v1.29.4 ip-10-0-138-148.ec2.internal Ready master 11m v1.29.4 ip-10-0-139-122.ec2.internal Ready master 11m v1.29.4 ip-10-0-147-35.ec2.internal Ready worker 7m v1.29.4 ip-10-0-153-12.ec2.internal Ready worker 7m v1.29.4 ip-10-0-154-10.ec2.internal Ready master 11m v1.29.4
启动调试过程以访问节点:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow oc debug node/ip-10-0-147-35.ec2.internal
$ oc debug node/ip-10-0-147-35.ec2.internal
输出示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Starting pod/ip-10-0-147-35ec2internal-debug ... To use host binaries, run `chroot /host`
Starting pod/ip-10-0-147-35ec2internal-debug ... To use host binaries, run `chroot /host`
将您的根目录改为
/host
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow chroot /host
sh-4.2# chroot /host
检查 WMCO 是否为每个 Windows 实例上的每个 registry 生成
hosts.toml
文件。对于上例 IDMS 对象,文件结构中应该有三个文件:Copy to Clipboard Copied! Toggle word wrap Toggle overflow tree $config_path
$ tree $config_path
输出示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow C:/k/containerd/registries/ |── registry.access.redhat.com | └── hosts.toml |── mirror.example.com | └── hosts.toml └── docker.io └── hosts.toml:
C:/k/containerd/registries/ |── registry.access.redhat.com | └── hosts.toml |── mirror.example.com | └── hosts.toml └── docker.io └── hosts.toml:
以下输出代表了应用了前面的 IDMS 对象的
hosts.toml
容器配置文件。host.toml 文件示例
Copy to Clipboard Copied! Toggle word wrap Toggle overflow cat "$config_path"/registry.access.redhat.com/host.toml server = "https://registry.access.redhat.com" # default fallback server since "AllowContactingSource" mirrorSourcePolicy is set [host."https://example.io/example/ubi-minimal"] capabilities = ["pull"] [host."https://example.com/example2/ubi-minimal"] # secondary mirror capabilities = ["pull"] cat "$config_path"/registry.redhat.io/host.toml "server" omitted since "NeverContactSource" mirrorSourcePolicy is set [host."https://mirror.example.com"] capabilities = ["pull"] cat "$config_path"/docker.io/host.toml server = "https://docker.io" [host."https://docker-mirror.internal"] capabilities = ["pull", "resolve"] # resolve tags
$ cat "$config_path"/registry.access.redhat.com/host.toml server = "https://registry.access.redhat.com" # default fallback server since "AllowContactingSource" mirrorSourcePolicy is set [host."https://example.io/example/ubi-minimal"] capabilities = ["pull"] [host."https://example.com/example2/ubi-minimal"] # secondary mirror capabilities = ["pull"] $ cat "$config_path"/registry.redhat.io/host.toml # "server" omitted since "NeverContactSource" mirrorSourcePolicy is set [host."https://mirror.example.com"] capabilities = ["pull"] $ cat "$config_path"/docker.io/host.toml server = "https://docker.io" [host."https://docker-mirror.internal"] capabilities = ["pull", "resolve"] # resolve tags
从源拉取镜像到节点,并检查是否通过 mirror 解析。
Copy to Clipboard Copied! Toggle word wrap Toggle overflow podman pull --log-level=debug registry.access.redhat.com/ubi9/ubi-minimal@sha256:5cf...
sh-4.2# podman pull --log-level=debug registry.access.redhat.com/ubi9/ubi-minimal@sha256:5cf...
存储库镜像故障排除
如果存储库镜像流程未按规定工作,请使用以下有关存储库镜像如何工作的信息协助排查问题。
- 首个工作镜像用于提供拉取(pull)的镜像。
- 只有在无其他镜像工作时,才会使用主 registry。
-
从系统上下文,
Insecure
标志用作回退。