×

先决条件

  • 您必须安装oc命令行界面 (CLI) 工具。

  • 您必须使用容器镜像为您更新配置本地容器镜像注册表,如镜像OpenShift Container Platform镜像中所述。

  • 您必须以具有admin权限的用户身份访问集群。请参阅使用RBAC定义和应用权限

  • 万一更新失败并且您必须将集群恢复到以前的状态,您必须拥有最新的etcd备份

  • 您已将之前通过Operator Lifecycle Manager (OLM) 安装的所有Operator更新到与目标版本兼容的版本。更新Operator可确保在集群更新期间默认OperatorHub目录从当前次要版本切换到下一个版本时,它们具有有效的更新路径。有关如何检查兼容性以及必要时更新已安装的Operator的更多信息,请参阅更新已安装的Operator

  • 您必须确保所有机器配置池 (MCP) 都正在运行且未暂停。在更新过程中,将跳过与已暂停MCP关联的节点。如果您正在执行金丝雀推出更新策略,则可以暂停MCP。

  • 如果您的集群使用手动维护的凭据,请更新新版本的云提供商资源。有关更多信息,包括如何确定这是否是您集群的要求,请参阅准备更新使用手动维护的凭据的集群

  • 如果您运行Operator或使用Pod中断预算配置任何应用程序,则在更新过程中可能会遇到中断。如果在PodDisruptionBudget中将minAvailable设置为1,则会清空节点以应用挂起的机器配置,这可能会阻止逐出过程。如果重新启动多个节点,所有Pod都可能仅在一个节点上运行,并且PodDisruptionBudget字段可以阻止节点清空。

如果您运行Operator或使用Pod中断预算配置任何应用程序,则在更新过程中可能会遇到中断。如果在PodDisruptionBudget中将minAvailable设置为1,则会清空节点以应用挂起的机器配置,这可能会阻止逐出过程。如果重新启动多个节点,所有Pod都可能仅在一个节点上运行,并且PodDisruptionBudget字段可以阻止节点清空。

暂停MachineHealthCheck资源

在更新过程中,集群中的节点可能会暂时不可用。对于工作节点,机器健康检查可能会将此类节点识别为不健康并重新启动它们。为了避免重新启动此类节点,请在更新集群之前暂停所有MachineHealthCheck资源。

先决条件
  • 安装OpenShift CLI (oc)。

步骤
  1. 要列出要暂停的所有可用的MachineHealthCheck资源,请运行以下命令

    $ oc get machinehealthcheck -n openshift-machine-api
  2. 要暂停机器健康检查,请将cluster.x-k8s.io/paused=""注释添加到MachineHealthCheck资源。运行以下命令

    $ oc -n openshift-machine-api annotate mhc <mhc-name> cluster.x-k8s.io/paused=""

    带注释的MachineHealthCheck资源类似于以下YAML文件

    apiVersion: machine.openshift.io/v1beta1
    kind: MachineHealthCheck
    metadata:
      name: example
      namespace: openshift-machine-api
      annotations:
        cluster.x-k8s.io/paused: ""
    spec:
      selector:
        matchLabels:
          role: worker
      unhealthyConditions:
      - type:    "Ready"
        status:  "Unknown"
        timeout: "300s"
      - type:    "Ready"
        status:  "False"
        timeout: "300s"
      maxUnhealthy: "40%"
    status:
      currentHealthy: 5
      expectedMachines: 5

    更新集群后恢复机器健康检查。要恢复检查,请通过运行以下命令从MachineHealthCheck资源中删除暂停注释

    $ oc -n openshift-machine-api annotate mhc <mhc-name> cluster.x-k8s.io/paused-

检索发行版镜像摘要

为了使用带有--to-image选项的oc adm upgrade命令在断开连接的环境中更新集群,您必须引用与目标发行版镜像对应的sha256摘要。

步骤
  1. 在连接到互联网的设备上运行以下命令

    $ oc adm release info -o 'jsonpath={.digest}{"\n"}' quay.io/openshift-release-dev/ocp-release:${OCP_RELEASE_VERSION}-${ARCHITECTURE}

    对于{OCP_RELEASE_VERSION},请指定要更新到的OpenShift Container Platform版本,例如4.10.16

    对于{ARCHITECTURE},请指定集群的架构,例如x86_64aarch64s390xppc64le

    示例输出
    sha256:a8bfba3b6dddd1a2fbbead7dac65fe4fb8335089e4e7cae327f3bad334add31d
  2. 复制 sha256 校验和,以便在更新集群时使用。

更新断开连接的集群

将断开连接的集群更新到您已下载其发行版镜像的 OpenShift Container Platform 版本。

如果您有本地 OpenShift 更新服务,则可以使用连接的 Web 控制台或 CLI 指令进行更新,而无需执行此过程。

先决条件
  • 您已将新版本的镜像镜像到您的注册表。

  • 您已将新版本的发行版镜像签名 ConfigMap 应用到您的集群。

    发行版镜像签名配置映射允许集群版本操作符 (CVO) 通过验证实际镜像签名是否与预期签名匹配来确保发行版镜像的完整性。

  • 您已获取目标发行版镜像的 sha256 校验和。

  • 您已安装 OpenShift CLI (oc)。

  • 您已暂停所有MachineHealthCheck资源。

步骤
  • 更新集群

    $ oc adm upgrade --allow-explicit-upgrade --to-image <defined_registry>/<defined_repository>@<digest>

    其中

    <defined_registry>

    指定您已将镜像镜像到的镜像注册表名称。

    <defined_repository>

    指定您要在镜像注册表上使用的镜像仓库名称。

    <digest>

    指定目标发行版镜像的 sha256 校验和,例如,sha256:81154f5c03294534e1eaf0319bef7a601134f891689ccede5d705ef659aa8c92

    • 请参阅“镜像 OpenShift Container Platform 镜像”以查看镜像注册表和仓库名称的定义方式。

    • 如果您使用了ImageContentSourcePolicyImageDigestMirrorSet,则可以使用规范注册表和仓库名称,而不是您定义的名称。规范注册表名称为quay.io,规范仓库名称为openshift-release-dev/ocp-release

    • 您只能为具有ImageContentSourcePolicy对象的集群配置全局拉取密钥。您不能向项目添加拉取密钥。

了解镜像注册表仓库镜像

设置容器注册表仓库镜像使您可以执行以下任务:

  • 配置您的 OpenShift Container Platform 集群以重定向请求,以便从源镜像注册表上的仓库拉取镜像,并使其由镜像镜像注册表上的仓库解析。

  • 为每个目标仓库识别多个镜像仓库,以确保如果一个镜像不可用,则可以使用另一个镜像。

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

  • 镜像拉取能够应对注册表停机。

  • 断开连接环境中的集群可以从关键位置(例如 quay.io)拉取镜像,并让公司防火墙后面的注册表提供请求的镜像。

  • 发出镜像拉取请求时,会尝试按照特定顺序尝试注册表,永久注册表通常是最后尝试的一个。

  • 您输入的镜像信息将添加到 OpenShift Container Platform 集群中每个节点上的/etc/containers/registries.conf文件中。

  • 当节点从源仓库请求镜像时,它会依次尝试每个镜像仓库,直到找到请求的内容。如果所有镜像都失败,集群将尝试源仓库。如果成功,则将镜像拉取到节点。

设置仓库镜像可以通过以下方式完成:

  • 在 OpenShift Container Platform 安装期间

    通过拉取 OpenShift Container Platform 需要并随后将这些镜像置于公司防火墙后的容器镜像,您可以将 OpenShift Container Platform 安装到断开连接环境中的数据中心。

  • 在 OpenShift Container Platform 安装后

    如果您在 OpenShift Container Platform 安装期间未配置镜像,则可以在安装后使用以下任何自定义资源 (CR) 对象进行配置:

    • ImageDigestMirrorSet (IDMS)。此对象允许您使用摘要规范从镜像注册表拉取镜像。IDMS CR 使您可以设置回退策略,如果镜像拉取失败,则允许或停止继续尝试从源注册表拉取。

    • ImageTagMirrorSet (ITMS)。此对象允许您使用镜像标签从镜像注册表拉取镜像。ITMS CR 使您可以设置回退策略,如果镜像拉取失败,则允许或停止继续尝试从源注册表拉取。

    • ImageContentSourcePolicy (ICSP)。此对象允许您使用摘要规范从镜像注册表拉取镜像。如果镜像不起作用,ICSP CR 将始终回退到源注册表。

    使用ImageContentSourcePolicy (ICSP) 对象配置仓库镜像是一个已弃用的功能。已弃用的功能仍包含在 OpenShift Container Platform 中,并将继续得到支持;但是,它将在该产品的未来版本中删除,不建议用于新的部署。如果您有用于创建ImageContentSourcePolicy对象的现有 YAML 文件,则可以使用oc adm migrate icsp命令将这些文件转换为ImageDigestMirrorSet YAML 文件。有关更多信息,请参阅下一节中的“转换用于镜像注册表仓库镜像的 ImageContentSourcePolicy (ICSP) 文件”。

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

  • 您要镜像的容器镜像仓库的来源。

  • 您要为从源仓库请求的内容提供的每个镜像仓库的单独条目。

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

如果您升级了集群,任何现有的 ICSP 对象都将保持稳定,并且同时支持 IDMS 和 ICSP 对象。使用 ICSP 对象的工作负载将继续按预期工作。但是,如果您想利用 IDMS CR 中引入的回退策略,则可以使用oc adm migrate icsp命令将当前工作负载迁移到 IDMS 对象,如后面的**转换用于镜像注册表仓库镜像的 ImageContentSourcePolicy (ICSP) 文件**部分所示。迁移到 IDMS 对象不需要重新启动集群。

如果您的集群使用ImageDigestMirrorSetImageTagMirrorSetImageContentSourcePolicy对象配置仓库镜像,则只能对镜像注册表使用全局拉取密钥。您不能向项目添加拉取密钥。

配置镜像注册表仓库镜像

您可以创建安装后镜像配置自定义资源 (CR) 以将镜像拉取请求从源镜像注册表重定向到镜像镜像注册表。

先决条件
  • 以具有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的容器镜像注册表,其中包含一个名为example的镜像仓库,您希望将registry.access.redhat.com中的ubi9/ubi-minimal镜像复制到该仓库。创建镜像注册表后,您可以配置您的 OpenShift Container Platform 集群,将对源仓库的请求重定向到镜像仓库。

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

    • 根据需要创建ImageDigestMirrorSetImageTagMirrorSet CR,用您自己的注册表和仓库对以及镜像替换源和镜像。

      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 根据拉取类型指示对象的类型
      • ImageDigestMirrorSet:拉取摘要引用镜像。

      • ImageTagMirrorSet:拉取标签引用镜像。

      3 指示镜像拉取方法的类型,可以是:
      • imageDigestMirrors:用于ImageDigestMirrorSet CR。

      • imageTagMirrors:用于ImageTagMirrorSet CR。

      4 指示镜像镜像注册表和仓库的名称。
      5 可选:为每个目标仓库指示辅助镜像仓库。如果一个镜像不可用,目标仓库可以使用辅助镜像。
      6 指示注册表和仓库源,即镜像拉取规范中引用的仓库。
      7 可选:指示如果镜像拉取失败的回退策略
      • AllowContactingSource:允许继续尝试从源仓库拉取镜像。这是默认设置。

      • NeverContactSource:阻止继续尝试从源仓库拉取镜像。

      8 可选:指示注册表内的命名空间,允许您使用该命名空间中的任何镜像。如果您使用注册表域名作为源,则该对象将应用于该注册表中的所有仓库。
      9 可选:指示注册表,允许您使用该注册表中的任何镜像。如果您指定注册表名称,则该对象将应用于从源注册表到镜像注册表的所有仓库。
      10 从镜像mirror.example.net/image@sha256:..拉取镜像registry.example.com/example/myimage@sha256:…​
      11 从镜像mirror.example.net/image@sha256:…​拉取源注册表命名空间中的镜像registry.example.com/example/image@sha256:…​
      12 从镜像注册表example.net/registry-example-com/myimage@sha256:…​拉取镜像registry.example.com/myimage@sha256
    • 创建一个ImageContentSourcePolicy自定义资源,用您自己的注册表和仓库对以及镜像替换源和镜像。

      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 指定镜像镜像注册表和仓库的名称。
      2 指定包含镜像内容的在线注册表和仓库。
  3. 创建新的对象

    $ oc create -f registryrepomirror.yaml

    创建对象后,机器配置操作员 (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.30.3
      ip-10-0-138-148.ec2.internal   Ready                      master   11m  v1.30.3
      ip-10-0-139-122.ec2.internal   Ready                      master   11m  v1.30.3
      ip-10-0-147-35.ec2.internal    Ready                      worker   7m   v1.30.3
      ip-10-0-153-12.ec2.internal    Ready                      worker   7m   v1.30.3
      ip-10-0-154-10.ec2.internal    Ready                      master   11m  v1.30.3
    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 的registries.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 指示在拉取规范中引用的仓库。
      2 指示该仓库的镜像。
      3 指示从镜像拉取的是摘要引用镜像。
      4 指示为此仓库设置了NeverContactSource参数。
      5 指示从镜像拉取的是标签引用镜像。
    5. 将镜像拉取到节点的源并检查镜像是否已解析。

      sh-4.2# podman pull --log-level=debug registry.access.redhat.com/ubi9/ubi-minimal@sha256:5cf...
镜像仓库故障排除

如果镜像仓库过程无法按描述工作,请使用以下关于镜像仓库如何工作的相关信息来帮助排查问题。

  • 第一个可用的镜像用于提供拉取的镜像。

  • 只有在没有其他镜像可用时才使用主注册表。

  • 从系统上下文来看,Insecure标志用作回退。

  • /etc/containers/registries.conf文件的格式最近已更改。它现在是版本 2,并且采用 TOML 格式。

转换用于镜像注册表仓库镜像的 ImageContentSourcePolicy (ICSP) 文件

使用ImageContentSourcePolicy (ICSP) 对象配置仓库镜像是一个已弃用的功能。此功能仍包含在 OpenShift Container Platform 中并继续受支持;但是,它将在该产品的未来版本中删除,不建议用于新部署。

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

由于迁移不会更改registries.conf文件,因此集群不需要重新启动。

有关ImageDigestMirrorSetImageTagMirrorSet对象的更多信息,请参见上一节中的“配置镜像注册表仓库镜像”。

先决条件
  • 以具有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 对象。

扩大镜像镜像目录的范围以减少集群节点重新启动的频率

您可以将镜像镜像目录的范围限定在仓库级别或更广泛的注册表级别。范围广泛的ImageContentSourcePolicy资源可减少节点需要重新启动以响应资源更改的次数。

要扩大ImageContentSourcePolicy资源中镜像镜像目录的范围,请执行以下过程。

先决条件
  • 安装 OpenShift Container Platform CLI oc

  • 以具有cluster-admin权限的用户身份登录。

  • 配置镜像镜像目录以在您的断开连接的集群中使用。

步骤
  1. 运行以下命令,并为<local_registry><pull_spec><pull_secret_file>指定值。

    $ oc adm catalog mirror <local_registry>/<pull_spec> <local_registry> -a <pull_secret_file> --icsp-scope=registry

    其中

    <local_registry>

    是您为断开连接的集群配置的本地注册表,例如,local.registry:5000

    <pull_spec>

    是在断开连接的注册表中配置的拉取规范,例如,redhat/redhat-operator-index:v4.17

    <pull_secret_file>

    .json文件格式的registry.redhat.io拉取密钥。您可以从Red Hat OpenShift 集群管理器下载拉取密钥。

    oc adm catalog mirror命令会创建一个/redhat-operator-index-manifests目录,并生成imageContentSourcePolicy.yamlcatalogSource.yamlmapping.txt文件。

  2. 将新的ImageContentSourcePolicy资源应用到集群。

    $ oc apply -f imageContentSourcePolicy.yaml
验证
  • 验证oc apply是否成功将更改应用于ImageContentSourcePolicy

    $ oc get ImageContentSourcePolicy -o yaml
    示例输出
    apiVersion: v1
    items:
    - apiVersion: operator.openshift.io/v1alpha1
      kind: ImageContentSourcePolicy
      metadata:
        annotations:
          kubectl.kubernetes.io/last-applied-configuration: |
            {"apiVersion":"operator.openshift.io/v1alpha1","kind":"ImageContentSourcePolicy","metadata":{"annotations":{},"name":"redhat-operator-index"},"spec":{"repositoryDigestMirrors":[{"mirrors":["local.registry:5000"],"source":"registry.redhat.io"}]}}
    ...

更新ImageContentSourcePolicy资源后,OpenShift Container Platform 会将新设置部署到每个节点,并且集群开始使用镜像仓库来处理对源仓库的请求。