第 5 章 将镜像固定到节点


到一个镜像 registry 的缓慢、不可靠的连接可能会影响需要拉取镜像的操作,如更新集群或部署应用程序。这可能包括低带宽的集群、具有不可靠互联网连接的集群,或处于断开连接环境中的集群。例如,集群更新可能需要拉取一百个镜像。无法拉取这些镜像可能会导致重试,这可能会影响更新过程,并可能导致更新失败。

防止此问题的一种方法是在实际需要前,提前拉取所需的镜像,并将这些镜像固定到一个特定的机器配置池 (MCP)。这可确保镜像在需要时可供您的节点使用。固定镜像可以提供一个更加一致的更新过程,这在将更新调度到维护窗口时非常重要。

固定镜像还可以确保镜像在部署应用程序时可用,以便您可以以一个更可靠的方式进行部署。

您可以使用 PinnedImageSet 自定义资源 (CR) 将镜像固定到特定的节点,如固定镜像中所述。固定的镜像被存储在这些节点上的 /etc/crio/crio.conf.d/50-pinned-images 文件中的节点上。该文件的内容类似于以下示例:

[crio]
  [crio.image]
    pinned_images = ["quay.io/openshift-release-dev/ocp-release@sha256:4198606580b69c8335ad7ae531c3a74e51aee25db5faaf368234e8c8dae5cbea", "quay.io/openshift-release-dev/ocp-release@sha256:513cf1028aa1a021fa73d0601427a0fbcf6d212b88aaf9d76d4e4841a061e44e", "quay.io/openshift-release-dev/ocp-release@sha256:61eae2d261e54d1b8a0e05f6b5326228b00468364563745eed88460af04f909b"]
Copy to Clipboard Toggle word wrap

固定镜像的另一个优点是,镜像垃圾回收并不会删除固定的镜像。

在拉取镜像前,Machine Config Operator (MCO)会验证每个受影响的节点上是否有足够的存储空间。如果节点有足够的空间,MCO 会创建固定镜像文件,拉取镜像并重新载入 CRI-O。如果没有足够的空间,MCO 不会拉取镜像并显示一个错误消息。

5.1. 固定镜像

您可以使用 PinnedImageSet 自定义资源 (CR) 将镜像固定到节点。固定镜像集定义了要预加载的镜像列表,以及镜像应固定到的机器配置池。

镜像存储在节点上的 /etc/crio/crio.conf.d/50-pinned-images 文件中。

流程

  1. 创建一个定义了 PinnedImageSet 对象的 YAML 文件,如下例所示:

    apiVersion: machineconfiguration.openshift.io/v1
    kind: PinnedImageSet
    metadata:
      labels: 
    1
    
        machineconfiguration.openshift.io/role: worker
      name: worker-pinned-images
    spec:
      pinnedImages: 
    2
    
       - name: quay.io/openshift-release-dev/ocp-release@sha256:513cf1028aa1a021fa73d0601427a0fbcf6d212b88aaf9d76d4e4841a061e44e
       - name: quay.io/openshift-release-dev/ocp-release@sha256:61eae2d261e54d1b8a0e05f6b5326228b00468364563745eed88460af04f909b
    Copy to Clipboard Toggle word wrap

    其中:

    labels
    指定一个可选的节点选择器,以指定将镜像固定到的机器配置池。如果没有指定,镜像将固定到集群中的所有节点。
    pinnedImages
    指定要预加载的一个或多个镜像的列表。
  2. 运行以下命令来创建 PinnedImageSet 对象:

    $ oc create -f <file_name>.yaml
    Copy to Clipboard Toggle word wrap

验证

  • 运行以下命令,检查机器配置在受影响的集群配置池的集群配置节点对象中报告。

    $ oc describe machineconfignode <machine_config_node_name>
    Copy to Clipboard Toggle word wrap

    示例命令

    $ oc describe machineconfignode ci-ln-25hlkvt-72292-jrs48-worker-a-2bdj
    Copy to Clipboard Toggle word wrap

    成功镜像拉取并固定的输出示例

    apiVersion: machineconfiguration.openshift.io/v1
    kind: MachineConfigNode
    metadata:
      creationTimestamp: "2025-04-28T18:40:29Z"
      generation: 3
      name: <machine_config_node_name>
    # ...
    status
      pinnedImageSets:
      - currentGeneration: 1
        desiredGeneration: 1
        name: worker-pinned-images 
    1
    Copy to Clipboard Toggle word wrap

    1 1
    PinnedImageset 对象与机器配置节点关联。

    MachineConfigNode 对象状态字段中出现任何失败或错误信息,如下例所示:

    镜像拉取和固定失败的输出示例

    apiVersion: machineconfiguration.openshift.io/v1
    kind: MachineConfigNode
    metadata:
      creationTimestamp: "2025-04-28T18:40:29Z"
      generation: 3
      name: <machine_config_node_name>
    # ...
      - lastTransitionTime: "2025-04-29T19:37:23Z"
        message: One or more PinnedImageSet is experiencing an error. See PinnedImageSet
          list for more details
        reason: PrefetchFailed
        status: "True"
        type: PinnedImageSetsDegraded
      configVersion:
        current: rendered-worker-cef1b52c532e19a20add12e369261fba
        desired: rendered-worker-cef1b52c532e19a20add12e369261fba
      observedGeneration: 3
      pinnedImageSets:
      - desiredGeneration: 1
        lastFailedGeneration: 1
        lastFailedGenerationError: 'failed to execute podman manifest inspect for "quay.io/rh-ee/machine-config-operator@sha256:65d3a308767b1773b6e3499dde6ef085753d7e20e685f78841079":
          exit status 125'
        name: worker-pinned-images
    Copy to Clipboard Toggle word wrap

  • 检查固定镜像文件已被创建并包含正确的镜像。

    1. 运行以下命令,为节点启动 debug 会话:

      $ oc debug node/<node_name>
      Copy to Clipboard Toggle word wrap
    2. 运行以下命令,将 /host 设置为 debug shell 中的根目录:

      sh-5.1# chroot /host
      Copy to Clipboard Toggle word wrap
    3. 运行以下命令,验证固定镜像文件的内容:

      $ cat /etc/crio/crio.conf.d/50-pinned-images
      Copy to Clipboard Toggle word wrap

      输出示例

      [crio]
        [crio.image]
          pinned_images = ["quay.io/openshift-release-dev/ocp-release@sha256:4198606580b69c8335ad7ae531c3a74e51aee25db5faaf368234e8c8dae5cbea", "quay.io/openshift-release-dev/ocp-release@sha256:513cf1028aa1a021fa73d0601427a0fbcf6d212b88aaf9d76d4e4841a061e44e", "quay.io/openshift-release-dev/ocp-release@sha256:61eae2d261e54d1b8a0e05f6b5326228b00468364563745eed88460af04f909b"]
      Copy to Clipboard Toggle word wrap

      其中:

      pinnedImages
      指定为受影响的机器配置池拉取和固定的镜像。
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat