9.4. 关于添加允许镜像短名称的 registry


使用镜像短名称,您可以在 pull spec 参数中包含完全限定域名来搜索镜像。

例如: 您可以使用 rhel7/etcd 而不是 registry.access.redhat.com/rhe7/etcd。您可以通过编辑 image.config.openshift.io/cluster 自定义资源(CR)来添加 registry 来搜索镜像短名称。

在无法使用完整路径的情况下,您可以使用简短名称。例如,如果您的集群引用了 DNS 经常更改的多个内部 registry,则需要更新 pull spec 中的完全限定域名并进行每次更改。在这种情况下,使用镜像简短名称可能很有用。

在拉取或推送镜像时,容器运行时会搜索 image.config.openshift.io/cluster CR 的 registrySources 参数中列出的 registry。如果您在 containerRuntimeSearchRegistries 参数下创建了 registry 列表,则容器运行时会搜索这些 registry。

9.4.1. 如果不使用镜像短名称

强烈建议不要将镜像短名称与公共 registry 搭配使用,因为如果公共 registry 需要身份验证,则镜像可能无法部署。将完全限定镜像名称与公共 registry 搭配使用。

红帽内部或私有 registry 通常支持使用镜像短名称。

如果每个公共 registry 需要不同的凭证,且集群不会在全局 pull secret 中列出公共 registry,则无法在 containerRuntimeSearchRegistries 参数下列出多个公共 registry。

对于需要身份验证的公共 registry,只有在 registry 具有其凭证存储在全局 pull secret 中时,才能使用镜像短名称。

警告

如果您在 containerRuntimeSearchRegistries 参数下列出公共 registry (包括 registry.redhat.iodocker.ioquay.io registry),您可以将凭证公开给列表上的所有 registry,并存在对网络和 registry 进行攻击的风险。因为您只能有一个 pull secret 用于拉取镜像,所以由全局 pull secret 定义,该 secret 用于对该列表中的每个 registry 进行身份验证。因此,如果您在列表中包含公共 registry,则会出现安全风险。

9.4.2. 添加允许镜像短名称的 registry

您可以通过编辑 image.config.openshift.io/cluster 自定义资源(CR)来添加 registry 来搜索镜像短名称。OpenShift Container Platform 会将对此 CR 的更改应用到集群中的所有节点。

警告

当您定义 allowedRegistries 参数时,除非明确列出,否则所有 registry (包括 registry.redhat.ioquay.io 和默认的 OpenShift 镜像 registry)都会被阻断。您必须将有效负载镜像所需的所有 registry 添加到 allowedRegistries 列表中。例如,列出 registry.redhat.ioquay.iointernalRegistryHostname registry。对于断开连接的集群,还必须添加镜像 registry。否则,您面临 pod 失败。

流程

  • 编辑 image.config.openshift.io/cluster 自定义资源:

    $ oc edit image.config.openshift.io/cluster
    Copy to Clipboard Toggle word wrap

    以下是 image.config.openshift.io/cluster CR 示例:

    apiVersion: config.openshift.io/v1
    kind: Image
    metadata:
      annotations:
        release.openshift.io/create-only: "true"
      creationTimestamp: "2019-05-17T13:44:26Z"
      generation: 1
      name: cluster
      resourceVersion: "8302"
      selfLink: /apis/config.openshift.io/v1/images/cluster
      uid: e34555da-78a9-11e9-b92b-06d6c7da38dc
    spec:
      allowedRegistriesForImport:
        - domainName: quay.io
          insecure: false
      additionalTrustedCA:
        name: myconfigmap
      registrySources:
        containerRuntimeSearchRegistries:
        - reg1.io
        - reg2.io
        - reg3.io
        allowedRegistries:
        - example.com
        - quay.io
        - registry.redhat.io
        - reg1.io
        - reg2.io
        - reg3.io
        - image-registry.openshift-image-registry.svc:5000
    ...
    status:
      internalRegistryHostname: image-registry.openshift-image-registry.svc:5000
    Copy to Clipboard Toggle word wrap
    1. 运行以下命令,获取节点列表:

      $ oc get nodes
      Copy to Clipboard Toggle word wrap

      输出示例

      NAME                STATUS   ROLES                  AGE   VERSION
      <node_name>         Ready    control-plane,master   37m   v1.27.8+4fab27b
      Copy to Clipboard Toggle word wrap
    2. 运行以下命令在节点上进入 debug 模式:

      $ oc debug node/<node_name>
      Copy to Clipboard Toggle word wrap
    3. 出现提示时,在终端中输入 chroot /host

      sh-4.4# chroot /host
      Copy to Clipboard Toggle word wrap

验证

  1. 运行以下命令,验证 registry 是否已添加到策略文件中:

    sh-5.1# cat /etc/containers/registries.conf.d/01-image-searchRegistries.conf
    Copy to Clipboard Toggle word wrap

    输出示例

    unqualified-search-registries = ['reg1.io', 'reg2.io', 'reg3.io']
    Copy to Clipboard Toggle word wrap

9.4.3. 为镜像 registry 访问配置额外的信任存储

您可以在镜像 registry 访问 image.config.openshift.io/cluster 自定义资源(CR)时添加对具有信任额外证书颁发机构(CA)的配置映射的引用。

先决条件

  • 证书颁发机构(CA)必须经过 PEM 编码。

流程

  1. openshift-config 命名空间中创建配置映射,然后在 image.config.openshift.io CR 的 AdditionalTrustedCA 参数中使用配置映射名称。这会添加集群联系外部镜像 registry 时应信任的 CA。

    镜像 registry CA 配置映射示例

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: my-registry-ca
    data:
      registry.example.com: |
        -----BEGIN CERTIFICATE-----
        ...
        -----END CERTIFICATE-----
      registry-with-port.example.com..5000: |
        -----BEGIN CERTIFICATE-----
        ...
        -----END CERTIFICATE-----
    Copy to Clipboard Toggle word wrap

    其中:

    data:registry.example.com:
    要信任此 CA 的 registry 主机名示例。
    data:registry-with-port.example.com..5000:

    信任此 CA 的端口的 registry 主机名示例。如果 registry 有一个端口,如 registry-with-port.example.com:5000: 必须替换为 ..

    PEM 证书内容是要信任的每个额外 registry CA 的值。

  2. 可选。运行以下命令来配置额外的 CA:

    $ oc create configmap registry-config --from-file=<external_registry_address>=ca.crt -n openshift-config
    Copy to Clipboard Toggle word wrap
    $ oc edit image.config.openshift.io cluster
    Copy to Clipboard Toggle word wrap
    spec:
      additionalTrustedCA:
        name: registry-config
    Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

Theme

© 2025 Red Hat