搜索

9.3. 了解镜像 registry 仓库镜像

download PDF

通过设置容器 registry 存储库镜像,您可以执行以下任务:

  • 配置 OpenShift Container Platform 集群,以便重定向从源镜像 registry 上的存储库拉取(pull)镜像的请求,并通过已镜像 (mirror) 的镜像 registry 上的存储库来解决该请求。
  • 为每个目标存储库识别多个已镜像 (mirror)的存储库,以确保如果一个镜像停止运作,仍可使用其他镜像。

OpenShift Container Platform 中的存储库镜像包括以下属性:

  • 镜像拉取(pull)可应对 registry 停机的问题。
  • 在断开连接的环境中的集群可以从关键位置(如 quay.io)拉取镜像,并让公司防火墙后面的 registry 提供请求的镜像。
  • 发出镜像拉取(pull)请求时尝试特定 registry 顺序,通常最后才会尝试持久性 registry。
  • 您所输入的镜像信息会添加到 OpenShift Container Platform 集群中每个节点上的 /etc/containers/registries.conf 文件中。
  • 当节点从源存储库中请求镜像时,它会依次尝试每个已镜像的存储库,直到找到所请求的内容。如果所有镜像均失败,集群则会尝试源存储库。如果成功,则镜像拉取至节点中。

可通过以下方式设置存储库镜像:

  • 在 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 中拉取。
    • ImageContentSourcePolicy (ICSP)。此对象允许您使用摘要规格从镜像 registry 中拉取镜像。如果镜像无法正常工作,ICSP CR 始终回退到源 registry。
    重要

    使用 ImageContentSourcePolicy (ICSP)对象配置存储库镜像是一个已弃用的功能。弃用的功能仍然包含在 OpenShift Container Platform 中,并将继续被支持。但是,这个功能会在以后的发行版本中被删除,且不建议在新的部署中使用。如果您有用于创建 ImageContentSourcePolicy 对象的 YAML 文件,您可以使用 oc adm migrate icsp 命令将这些文件转换为 ImageDigestMirrorSet YAML 文件。如需更多信息,请参阅以下部分"协调 ImageContentSourcePolicy (ICSP)文件以进行镜像 registry 存储库镜像"。

每个自定义资源对象都标识以下信息:

  • 您希望镜像 (mirror) 的容器镜像存储库的源。
  • 您希望为其提供从源存储库请求的内容的每个镜像存储库的单独条目。

对于新集群,您可以根据需要使用 IDMS、ITMS 和 ICSP CR 对象。但是,建议使用 IDMS 和 ITMS。

如果您升级了集群,则任何现有 ICSP 对象都会保持稳定,并且支持 IDMS 和 ICSP 对象。使用 ICSP 对象的工作负载可以按预期工作。但是,如果要利用 IDMS CR 中引入的回退策略,您可以使用 oc adm migrate icsp 命令将当前工作负载迁移到 IDMS 对象,如后面的镜像 registry 存储库镜像部分所示。迁移到 IDMS 对象不需要重启集群。

注意

如果您的集群使用 ImageDigestMirrorSetImageTagMirrorSetImageContentSourcePolicy 对象来配置存储库镜像,则只能使用镜像的 registry 的全局 pull secret。您不能在项目中添加 pull secret。

其他资源

9.3.1. 配置镜像 registry 存储库镜像

您可以创建安装后镜像配置自定义资源 (CR),将源镜像 registry 中的镜像拉取请求重定向到镜像 registry。

先决条件

  • 使用具有 cluster-admin 角色的用户访问集群。

流程

  1. 通过以下方法配置已镜像的存储库:

    • 按照 Red Hat Quay 存储库镜像中所述,使用 Red Hat Quay 来设置已镜像的存储库。使用 Red Hat Quay 有助于您将镜像从一个存储库复制到另一存储库,并可随着时间的推移重复自动同步这些存储库。
    • 使用 skopeo 等工具手动将镜像从源存储库复制到已镜像的存储库。

      例如:在 Red Hat Enterprise Linux(RHEL 7 或 RHEL 8)系统上安装 skopeo RPM 软件包后,使用 skopeo 命令,如下例所示:

      $ 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 集群配置为将源存储库的请求重定向到已镜像的存储库。

  2. 使用以下示例之一创建安装后镜像配置 CR:

    • 根据需要,创建一个 ImageDigestMirrorSetImageTagMirrorSet CR,将源和镜像(mirror)替换为您自己的 registry、存储库对和镜像:

      apiVersion: config.openshift.io/v1 1
      kind: ImageDigestMirrorSet 2
      metadata:
        name: ubi9repo
      spec:
        imageDigestMirrors: 3
        - mirrors:
          - example.io/example/ubi-minimal 4
          - example.com/example/ubi-minimal 5
          source: registry.access.redhat.com/ubi9/ubi-minimal 6
          mirrorSourcePolicy: AllowContactingSource 7
        - mirrors:
          - mirror.example.com/redhat
          source: registry.example.com/redhat 8
          mirrorSourcePolicy: AllowContactingSource
        - mirrors:
          - mirror.example.com
          source: registry.example.com 9
          mirrorSourcePolicy: AllowContactingSource
        - mirrors:
          - mirror.example.net/image
          source: registry.example.com/example/myimage 10
          mirrorSourcePolicy: AllowContactingSource
        - mirrors:
          - mirror.example.net
          source: registry.example.com/example 11
          mirrorSourcePolicy: AllowContactingSource
        - mirrors:
          - mirror.example.net/registry-example-com
          source: registry.example.com 12
          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: 防止继续尝试从源存储库拉取镜像。
      8
      可选:指示 registry 中的命名空间,它允许您使用该命名空间中的任何镜像。如果您使用 registry 域作为源,则对象将应用到 registry 中的所有存储库。
      9
      可选:指示一个 registry,它允许您使用该 registry 中的任何镜像。如果指定了 registry 名称,对象将应用到源 registry 中的所有存储库到镜像 registry。
      10
      从 mirror mirror.example.net/image@sha256:.. 拉取镜像 registry.example.com/example/myimage@sha256:…​
      11
      从 mirror mirror.example.net/image@sha256:…​ 的源 registry 命名空间中拉取镜像 registry.example.com/example/image@sha256:…​
      12
      从 mirror registry example.net/registry-example-com/myimage@sha256:…​ 中拉取镜像 registry.example.com/myimage@sha256
    • 创建 ImageContentSourcePolicy 自定义资源,将源和镜像替换为您自己的 registry、存储库对和镜像:

      apiVersion: operator.openshift.io/v1alpha1
      kind: ImageContentSourcePolicy
      metadata:
        name: mirror-ocp
      spec:
        repositoryDigestMirrors:
        - mirrors:
          - mirror.registry.com:443/ocp/release 1
          source: quay.io/openshift-release-dev/ocp-release 2
        - mirrors:
          - mirror.registry.com:443/ocp/release
          source: quay.io/openshift-release-dev/ocp-v4.0-art-dev
      1
      指定镜像 registry 和存储库的名称。
      2
      指定包含所镜像内容的在线 registry 和存储库。
  3. 创建新对象:

    $ oc create -f registryrepomirror.yaml

    创建对象后,Machine Config Operator (MCO) 只会排空 ImageTagMirrorSet 对象的节点。MCO 不会排空 ImageDigestMirrorSetImageContentSourcePolicy 对象的节点。

  4. 要检查是否应用了镜像的配置设置,请在其中一个节点上执行以下操作。

    1. 列出您的节点:

      $ oc get node

      输出示例

      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

    2. 启动调试过程以访问节点:

      $ oc debug node/ip-10-0-147-35.ec2.internal

      输出示例

      Starting pod/ip-10-0-147-35ec2internal-debug ...
      To use host binaries, run `chroot /host`

    3. 将您的根目录改为 /host

      sh-4.2# chroot /host
    4. 检查 /etc/containers/registries.conf 文件,确保已完成更改:

      sh-4.2# cat /etc/containers/registries.conf

      以下输出代表了应用安装后镜像配置 CR 的 registry.conf 文件。最后的两个条目分别标记为 digest-onlytag-only

      输出示例

      unqualified-search-registries = ["registry.access.redhat.com", "docker.io"]
      short-name-mode = ""
      
      [[registry]]
        prefix = ""
        location = "registry.access.redhat.com/ubi9/ubi-minimal" 1
      
        [[registry.mirror]]
          location = "example.io/example/ubi-minimal" 2
          pull-from-mirror = "digest-only" 3
      
        [[registry.mirror]]
          location = "example.com/example/ubi-minimal"
          pull-from-mirror = "digest-only"
      
      [[registry]]
        prefix = ""
        location = "registry.example.com"
      
        [[registry.mirror]]
          location = "mirror.example.net/registry-example-com"
          pull-from-mirror = "digest-only"
      
      [[registry]]
        prefix = ""
        location = "registry.example.com/example"
      
        [[registry.mirror]]
          location = "mirror.example.net"
          pull-from-mirror = "digest-only"
      
      [[registry]]
        prefix = ""
        location = "registry.example.com/example/myimage"
      
        [[registry.mirror]]
          location = "mirror.example.net/image"
          pull-from-mirror = "digest-only"
      
      [[registry]]
        prefix = ""
        location = "registry.example.com"
      
        [[registry.mirror]]
          location = "mirror.example.com"
          pull-from-mirror = "digest-only"
      
      [[registry]]
        prefix = ""
        location = "registry.example.com/redhat"
      
        [[registry.mirror]]
          location = "mirror.example.com/redhat"
          pull-from-mirror = "digest-only"
      [[registry]]
        prefix = ""
        location = "registry.access.redhat.com/ubi9/ubi-minimal"
        blocked = true 4
      
        [[registry.mirror]]
          location = "example.io/example/ubi-minimal-tag"
          pull-from-mirror = "tag-only" 5

      1
      指明在 pull spec 中引用的存储库。
      2
      指明该存储库的镜像。
      3
      表示从 mirror 的镜像拉取是一个摘要引用镜像。
      4
      表示为此存储库设置了 NeverContactSource 参数。
      5
      表示从 mirror 的镜像拉取是一个标签引用镜像。
    5. 从源拉取镜像到节点,并检查是否通过 mirror 解析。

      sh-4.2# podman pull --log-level=debug registry.access.redhat.com/ubi9/ubi-minimal@sha256:5cf...

存储库镜像故障排除

如果存储库镜像流程未按规定工作,请使用以下有关存储库镜像如何工作的信息协助排查问题。

  • 首个工作镜像用于提供拉取(pull)的镜像。
  • 只有在无其他镜像工作时,才会使用主 registry。
  • 从系统上下文,Insecure 标志用作回退。
  • 最近更改了 /etc/containers/registries.conf 文件的格式。现在它是第 2 版,采用 TOML 格式。

9.3.2. 为镜像 registry 存储库镜像转换 ImageContentSourcePolicy (ICSP) 文件

使用 ImageContentSourcePolicy (ICSP)对象配置存储库镜像是一个已弃用的功能。此功能仍然包含在 OpenShift Container Platform 中,并将继续被支持。但是,这个功能会在以后的发行版本中被删除,且不建议在新的部署中使用。

ICSP 对象被 ImageDigestMirrorSetImageTagMirrorSet 对象替代,以配置存储库镜像。如果您有用于创建 ImageContentSourcePolicy 对象的 YAML 文件,您可以使用 oc adm migrate icsp 命令将这些文件转换为 ImageDigestMirrorSet YAML 文件。命令将 API 更新至当前版本,将 kind 值更改为 ImageDigestMirrorSet,并将 spec.repositoryDigestMirrors 更改为 spec.imageDigestMirrors。文件的其余部分不会改变。

因为迁移不会更改 registry.conf 文件,所以集群不需要重启。

有关 ImageDigestMirrorSetImageTagMirrorSet 对象的更多信息,请参阅上一节中的"配置镜像 registry 存储库镜像"。

先决条件

  • 使用具有 cluster-admin 角色的用户访问集群。
  • 确保集群中具有 ImageContentSourcePolicy 对象。

流程

  1. 使用以下命令,将一个或多个 ImageContentSourcePolicy YAML 文件转换为 ImageDigestMirrorSet YAML 文件:

    $ oc adm migrate icsp <file_name>.yaml <file_name>.yaml <file_name>.yaml --dest-dir <path_to_the_directory>

    其中:

    <file_name>
    指定源 ImageContentSourcePolicy YAML 的名称。您可以列出多个文件名。
    --dest-dir
    可选:指定输出 ImageDigestMirrorSet YAML 的目录。如果未设置,则会将该文件写入当前目录中。

    例如,以下命令可将 icsp.yamlicsp-2.yaml 文件转换,并将新的 YAML 文件保存到 idms-files 目录中。

    $ oc adm migrate icsp icsp.yaml icsp-2.yaml --dest-dir idms-files

    输出示例

    wrote ImageDigestMirrorSet to idms-files/imagedigestmirrorset_ubi8repo.5911620242173376087.yaml
    wrote ImageDigestMirrorSet to idms-files/imagedigestmirrorset_ubi9repo.6456931852378115011.yaml

  2. 运行以下命令来创建 CR 对象:

    $ oc create -f <path_to_the_directory>/<file-name>.yaml

    其中:

    <path_to_the_directory>
    如果使用 --dest-dir 标志,请指定目录的路径。
    <file_name>
    指定 ImageDigestMirrorSet YAML 的名称。
  3. 在推出 IDMS 对象后,删除 ICSP 对象。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.