9.3. 配置镜像 registry 设置
您可以通过编辑 image.config.openshift.io/cluster 自定义资源(CR)来配置镜像 registry 设置。
流程
运行以下命令来编辑
image.config.openshift.io/clusterCR:$ oc edit image.config.openshift.io/cluster以下是
image.config.openshift.io/clusterCR 示例: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: allowedRegistries: - example.com - quay.io - registry.redhat.io - image-registry.openshift-image-registry.svc:5000 - reg1.io/myrepo/myapp:latest insecureRegistries: - insecure.com status: internalRegistryHostname: image-registry.openshift-image-registry.svc:5000注意当使用
allowedRegistries、blockedRegistries或insecureRegistries参数时,您可以在 registry 中指定单独的存储库。例如:reg1.io/myrepo/myapp:latest。避免不安全的外部 registry,以减少可能的安全风险。
验证
要验证您的更改,请运行以下命令列出节点:
$ oc get nodes输出示例
NAME STATUS ROLES AGE VERSION ip-10-0-137-182.us-east-2.compute.internal Ready,SchedulingDisabled worker 65m v1.29.4 ip-10-0-139-120.us-east-2.compute.internal Ready,SchedulingDisabled control-plane 74m v1.29.4 ip-10-0-176-102.us-east-2.compute.internal Ready control-plane 75m v1.29.4 ip-10-0-188-96.us-east-2.compute.internal Ready worker 65m v1.29.4 ip-10-0-200-59.us-east-2.compute.internal Ready worker 63m v1.29.4 ip-10-0-223-123.us-east-2.compute.internal Ready control-plane 73m v1.29.4
9.3.1. 在允许列表中添加特定的 registry 复制链接链接已复制到粘贴板!
您可以通过编辑 image.config.openshift.io/cluster 自定义资源(CR)在 registry 中为镜像拉取和推送(push)添加允许 registry 或单独的存储库。
OpenShift Container Platform 会将对此 CR 的更改应用到集群中的所有节点。
在拉取或推送镜像时,容器运行时会搜索 image.config.openshift.io/cluster CR 的 registrySources 参数中列出的 registry。如果您在 allowedRegistries 参数下创建了 registry 列表,则容器运行时仅搜索这些 registry。allowlist 中没有的 registry 会被阻止。
当您定义 allowedRegistries 参数时,除非明确列出,否则所有 registry (包括 registry.redhat.io、quay.io 和默认的 OpenShift 镜像 registry)都会被阻断。您必须将有效负载镜像所需的所有 registry 添加到 allowedRegistries 列表中。例如,列出 registry.redhat.io、quay.io 和 internalRegistryHostname registry。对于断开连接的集群,还必须添加镜像 registry。否则,您面临 pod 失败。
流程
运行以下命令来编辑
image.config.openshift.io/cluster自定义资源:$ oc edit image.config.openshift.io/cluster以下是一个带有允许列表的
image.config.openshift.io/clusterCR 示例: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: registrySources: allowedRegistries: - example.com - quay.io - registry.redhat.io - reg1.io/myrepo/myapp:latest - image-registry.openshift-image-registry.svc:5000 status: internalRegistryHostname: image-registry.openshift-image-registry.svc:5000在进行了配置更新后,运行以下命令来列出节点:
$ oc get nodes输出示例
NAME STATUS ROLES AGE VERSION <node_name> Ready control-plane,master 37m v1.27.8+4fab27b运行以下命令在节点上进入 debug 模式:
$ oc debug node/<node_name>将 <node_name> 替换为节点的名称。
出现提示时,在终端中输入
chroot /host:sh-4.4# chroot /host
验证
运行以下命令,检查 registry 是否在策略文件中:
sh-5.1# cat /etc/containers/policy.json | jq '.'以下策略表示,只有
example.com、quay.io和registry.redhat.ioregistry 中的镜像才可以进行镜像拉取(pull)和推送:镜像签名策略文件示例
{ "default":[ { "type":"reject" } ], "transports":{ "atomic":{ "example.com":[ { "type":"insecureAcceptAnything" } ], "image-registry.openshift-image-registry.svc:5000":[ { "type":"insecureAcceptAnything" } ], "insecure.com":[ { "type":"insecureAcceptAnything" } ], "quay.io":[ { "type":"insecureAcceptAnything" } ], "reg4.io/myrepo/myapp:latest":[ { "type":"insecureAcceptAnything" } ], "registry.redhat.io":[ { "type":"insecureAcceptAnything" } ] }, "docker":{ "example.com":[ { "type":"insecureAcceptAnything" } ], "image-registry.openshift-image-registry.svc:5000":[ { "type":"insecureAcceptAnything" } ], "insecure.com":[ { "type":"insecureAcceptAnything" } ], "quay.io":[ { "type":"insecureAcceptAnything" } ], "reg4.io/myrepo/myapp:latest":[ { "type":"insecureAcceptAnything" } ], "registry.redhat.io":[ { "type":"insecureAcceptAnything" } ] }, "docker-daemon":{ "":[ { "type":"insecureAcceptAnything" } ] } } }注意如果您的集群使用
registrySources.insecureRegistries参数,请确保将任何不安全的 registry 包含在允许的列表中。例如:
spec: registrySources: insecureRegistries: - insecure.com allowedRegistries: - example.com - quay.io - registry.redhat.io - insecure.com - image-registry.openshift-image-registry.svc:5000
9.3.2. 阻塞特定的 registry 复制链接链接已复制到粘贴板!
您可以通过编辑 image.config.openshift.io/cluster 自定义资源(CR)来阻止 registry 中的任何 registry 或单独的存储库。
OpenShift Container Platform 会将对此 CR 的更改应用到集群中的所有节点。
在拉取或推送镜像时,容器运行时会搜索 image.config.openshift.io/cluster CR 的 registrySources 参数中列出的 registry。如果您在 blockedRegistries 参数下创建了 registry 列表,则容器运行时不会搜索这些 registry。允许所有其他 registry。
要防止 pod 失败,请不要将 registry.redhat.io 和 quay.io registry 添加到 blockedRegistries 列表中。环境中的有效负载镜像需要访问这些 registry。
流程
运行以下命令来编辑
image.config.openshift.io/cluster自定义资源:$ oc edit image.config.openshift.io/cluster以下是一个带有块列表的
image.config.openshift.io/clusterCR 示例: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: registrySources: blockedRegistries: - untrusted.com - reg1.io/myrepo/myapp:latest status: internalRegistryHostname: image-registry.openshift-image-registry.svc:5000您不能同时设置
blockedRegistries和allowedRegistries参数。您必须选择一个或另一个。运行以下命令,获取节点列表:
$ oc get nodes输出示例
NAME STATUS ROLES AGE VERSION <node_name> Ready control-plane,master 37m v1.27.8+4fab27b运行以下命令在节点上进入 debug 模式:
$ oc debug node/<node_name>将 <node_name> 替换为您要详情的节点名称。
出现提示时,在终端中输入
chroot /host:sh-4.4# chroot /host
验证
运行以下命令,验证 registry 是否在策略文件中:
sh-5.1# cat etc/containers/registries.conf以下示例显示,来自
untrusted.comregistry 的镜像被阻止进行镜像拉取(pull)和推送:输出示例
unqualified-search-registries = ["registry.access.redhat.com", "docker.io"] [[registry]] prefix = "" location = "untrusted.com" blocked = true
9.3.3. 阻塞一个 payload registry 复制链接链接已复制到粘贴板!
在镜像配置中,您可以使用 ImageContentSourcePolicy (ICSP)对象在断开连接的环境中阻止上游有效负载 registry。以下示例步骤演示了如何阻止 quay.io/openshift-payload payload registry。
流程
使用
ImageContentSourcePolicy(ICSP) 对象创建镜像配置,以便将 payload 镜像到您的实例中 registry。以下示例 ICSP 文件对 payloadinternal-mirror.io/openshift-payload进行了镜像:apiVersion: operator.openshift.io/v1alpha1 kind: ImageContentSourcePolicy metadata: name: my-icsp spec: repositoryDigestMirrors: - mirrors: - internal-mirror.io/openshift-payload source: quay.io/openshift-payload对象部署到节点上后,通过检查
/etc/containers/registries.conf自定义资源(CR)来验证是否设置了镜像配置:输出示例
[[registry]] prefix = "" location = "quay.io/openshift-payload" mirror-by-digest-only = true [[registry.mirror]] location = "internal-mirror.io/openshift-payload"使用以下命令来编辑
image.config.openshift.ioCR:$ oc edit image.config.openshift.io cluster要阻止有效负载 registry,请在
image.config.openshift.ioCR 中添加以下配置:spec: registrySources: blockedRegistries: - quay.io/openshift-payload
验证
通过检查节点上的
/etc/containers/registries.conf文件,验证上游 payload registry 是否被阻止。/etc/containers/registries.conf文件示例[[registry]] prefix = "" location = "quay.io/openshift-payload" blocked = true mirror-by-digest-only = true [[registry.mirror]] location = "internal-mirror.io/openshift-payload"
9.3.4. 允许不安全的 registry 复制链接链接已复制到粘贴板!
您可以通过编辑 image.config.openshift.io/cluster 自定义资源(CR)在 registry 中添加不安全的 registry 或单独的存储库。
OpenShift Container Platform 会将对此 CR 的更改应用到集群中的所有节点。没有使用有效 SSL 证书或不需要 HTTPS 连接的 registry 被视为是不安全的 registry。
避免不安全的外部 registry,以减少可能的安全风险。
+ :leveloffset: +1
当您定义 allowedRegistries 参数时,除非明确列出,否则所有 registry (包括 registry.redhat.io、quay.io 和默认的 OpenShift 镜像 registry)都会被阻断。您必须将有效负载镜像所需的所有 registry 添加到 allowedRegistries 列表中。例如,列出 registry.redhat.io、quay.io 和 internalRegistryHostname registry。对于断开连接的集群,还必须添加镜像 registry。否则,您面临 pod 失败。
流程
运行以下命令来编辑
image.config.openshift.io/cluster自定义资源(CR):$ oc edit image.config.openshift.io/cluster以下是一个带有不安全 registry 列表的
image.config.openshift.io/clusterCR 示例: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: registrySources: insecureRegistries: - insecure.com - reg4.io/myrepo/myapp:latest allowedRegistries: - example.com - quay.io - registry.redhat.io - insecure.com - reg4.io/myrepo/myapp:latest - image-registry.openshift-image-registry.svc:5000 status: internalRegistryHostname: image-registry.openshift-image-registry.svc:5000
验证
在节点上运行以下命令来检查 registry 是否已添加到策略文件中:
$ cat /etc/containers/registries.conf以下示例显示来自
insecure.comregistry 的镜像不安全,并允许镜像拉取和推送。输出示例
unqualified-search-registries = ["registry.access.redhat.com", "docker.io"] [[registry]] prefix = "" location = "insecure.com" insecure = true