为什么由于映像拉取认证而导致构建错误?

虚拟私有云 经典基础设施

当构建任务(例如来自Operator Hub或内置开发者内容目录的构建)尝试从注册表 Red Hat 拉取镜像时,可能会因身份验证错误导致构建失败,错误信息类似如下所示:

error: build error: After retrying 2 times, Pull image still failed due to error: unauthorized: authentication required

默认情况下,您的集群已配置用于从 Red Hat Docker Hub、Docker registry.redhat.io Registry registry.connect.redhat.com 和 GitHub Docker Registry cloud.openshift.com 等注册表拉取镜像的密钥。 此外,在该项目 default 中,您的集群拥有用于访问镜像 <region>.icr.io 仓库的镜像拉取 IBM Cloud Container Registry 密钥。

但是,如果操作程序或内置模板具有必须从专用注册表中拉取映像的构建组件,那么该构建可能会失败并产生认证错误,因为该构建无法访问其服务帐户中的缺省映像拉取私钥。 缺省情况下,构建可以拉取仅存储在内部注册表中的映像。

通过从专用注册表拉取映像或者通过将映像从专用注册表导入到内部注册表,设置具有对映像的访问权的构建。

有关更多信息,请参阅 Red Hat OpenShift 文档

  1. 检查构建配置文件以查看构建需要拉取访问权的注册表。 例如,如果您的构建是 Red Hat OpenShift 模板的一部分,那么构建配置 spec.strategy.sourceStrategy.from.name 值将引用 registry.redhat.io 专用注册表。

    oc -n openshift get template react-web-app-example -o yaml
    

    示例输出

    strategy:
      sourceStrategy:
        from:
          kind: DockerImage
          name: registry.redhat.io/rhoar-nodejs-tech-preview/rhoar-nodejs-10-webapp
    
  2. 设置具有映像拉取访问权的构建。 您可以选择从专用注册表中拉取映像,也可以使用内部注册表中的映像流。

    • 从专用注册表中拉取映像:
      1. 在每个项目中,添加具有对构建所使用的专用注册表的拉取访问权的映像拉取私钥。

        • 对于 Red Hat 注册表: openshift-config 项目中的 复制 pull-secret 私钥。 此私钥包含对以下专用注册表的拉取访问权: cloud.openshift.comquay.ioregistry.connect.redhat.comregistry.redhat.io
        • 对于 IBM Cloud Container Registry: default 项目中的 复制 <region>.icr.io 私钥
        • 对于其他专用注册表: 创建映像拉取私钥,具有对专用注册表的映像拉取访问权。
      2. 将私钥添加到构建器服务帐户在构建配置文件中指定映像拉取私钥

        用于将私钥链接到项目中的构建器服务帐户的示例。

        oc secrets link builder <pull-secret>
        

        用于引用构建配置文件中的私钥的示例。

        spec:
          output:
            to:
              kind: "DockerImage"
              name: "<private.registry.com>/<namespace>/<image>:<tag>"
            pushSecret:
              name: "<pull-secret>"
        
    • 使用内部注册表中的映像流: 从专用注册表导入的映像在内部注册表中创建映像流。 然后,更新构建配置文件以引用映像流,而不是直接从专用注册表中拉取映像。