概念
OpenShift 可访问的 Image Registy 包括其自身内部的 Image Registry 和其外部的 Image Registry,而外部的 Image Registry 又可分为运行在远端的 Image Registry,以及为了加速远端访问而运行在本地(或近地的)Mirror Image Registry。Mirror Image Registry 除了可以用来加速访问外部 Image 外,我们在离线安装 OpenShift 的时候也会用到它。
配置 OpenShift 的 Image Registry
OpenShift 把它能访问到的 Image Registry 的配置保存在 image.config.openshift.io/cluster 对象中。当 OpenShift 的 Machine Config Operator(MCO)发现 image.config.openshift.io 配置变化后,会将最新配置发送到所有 Node 并写入 /etc/containers/registries.conf 文件,然后重启对应的 Node 使配置生效。
OpenShift 使用一个名为 cluster 的 image.config.openshift.io 对象管理其能访问到的 Image Registry 目标和策略。
$ oc get image.config.openshift.io
NAME AGE
cluster 12d
查看 OpenShift 可以访问到 Registry 的配置,其中 External Registry Hostnames 和 Internal Registry Hostname 定义了外部和内部Registry。
$ oc describe image.config.openshift.io cluster
Name: cluster
Namespace:
Labels: <none>
Annotations: release.openshift.io/create-only: true
API Version: config.openshift.io/v1
Kind: Image
Metadata:
Creation Timestamp: 2020-07-18T09:54:27Z
Generation: 1
Resource Version: 25273
Self Link: /apis/config.openshift.io/v1/images/cluster
UID: c94ebed4-684a-4c98-86aa-658a8685ef79
Spec:
Status:
External Registry Hostnames:
default-route-openshift-image-registry.apps.cluster-beijing-959a.beijing-959a.example.opentlc.com
Internal Registry Hostname: image-registry.openshift-image-registry.svc:5000
Events: <none>
上面只是 Registry 的配置信息,其中 OpenShift 内部的 Registry 是通过以下的 Cluster Operator 运行的。
$ oc get co image-registry
NAME VERSION AVAILABLE PROGRESSING DEGRADED SINCE
image-registry 4.5.4 True False False 2d2h
如果要修改 OpenShift 可访问的 Registry 目标,可先编辑 image.config.openshift.io/cluster 对象:
$ oc edit image.config.openshift.io/cluster
增加以下 registrySources 部分内容。
spec:
registrySources:
insecureRegistries:
- ocr.example.com
allowedRegistries:
- quay.io
- ocr.example.com
保存后 OpenShift 会自动通过 MCO 将最新的配置同步到各个节点。可以从 Node 的 Events 查看节点的重启过程。
如果需要查看一个 Node 的 Registry 配置,可先通过 debug 进入该 Node,然后查看自动生成的 /etc/containers/registries.conf(该文件是节点 crio 可以访问的 registry 的配置),确认最新的 Registry 配置已经加进去了。另外,如果使用非完整名称的 Image,crio 会使用由 unqualified-search-registries 定义的 Registry 查找该镜像。
$ oc debug node/<NODE-NAME>
sh-4.2# chroot /host
sh-4.2# cat /etc/containers/registries.conf
unqualified-search-registries = ['registry.access.redhat.com', 'docker.io']
short-name-mode = ""
[[registry]]
prefix = ""
location = "ocr.example.com"
insecure = true
查看自动生成的 /etc/containers/policy.json 文件。注意:由于前面使用了 allowedRegistries 配置,因此在 policy.json 中的 default 为 reject 类型,即缺省所有 registry 都 reject 访问。而对于从 allowedRegistries 中 registry 获得的镜像将使用 insecureAcceptAnything 验签策略,即不进行验签,即使镜像没有签名或签名无效,也会接受该镜像。
# cat /etc/containers/policy.json
{
"default": [
{
"type": "reject"
}
],
"transports": {
"atomic": {
"ocr.example.com": [
{
"type": "insecureAcceptAnything"
}
],
"quay.io": [
{
"type": "insecureAcceptAnything"
}
]
},
"docker": {
"ocr.example.com": [
{
"type": "insecureAcceptAnything"
}
],
"quay.io": [
{
"type": "insecureAcceptAnything"
}
],
"docker-daemon": {
"": [
{
"type": "insecureAcceptAnything"
}
]
}
}
配置 OpenShift 的 Mirror Image Registry
在一个受限的网络中(或访问远程 Image Registry 的网速比较慢),可以为 OpenShift 使用的外部 Image Registry 建一个 Mirror,以加速OpenShift 访问远程 Registry 上 Image。Registry Mirror 可以是运行在自己本地的 Docker Distribution、Quay 等提供的 Registry 服务。
Image Registry Mirror 的配置信息是保存在 OpenShift 集群范围的 ImageDigestMirrorSet 或 ImageTagMirrorSet 对象。当 OpenShift 的 Machine Config Operator(MCO)发现 ImageDigestMirrorSet 或 ImageTagMirrorSet 配置变化后,会将最新配置对送到所有 Node 并写入 /etc/containers/registries.conf 文件,然后重启对应的 Node 使配置生效。
注意:本文只对说明如何将 Mirror Image Registry 的配置加到 OpenShift 中,而部署一套 Mirror Image Registry 可参见《OpenShift 4 - 部署 Mirror Registry》。
- 执行命令创建一个 ImageTagMirrorSet 对象(说明:ImageTagMirrorSet 和 ImageDigestMirrorSet 的区别可参见这里)。它为 registry.access.redhat.com/ubi8/ubi-minimal 镜像提供了 2 个 Mirror,一个是 example.io/example/ubi-minimal,另一个是 example.com/example/ubi-minimal,OpenShift 会按照顺序访问 Mirror 以获取 Image。
$ oc apply -f - << EOF
apiVersion: config.openshift.io/v1
kind: ImageTagMirrorSet
metadata:
name: ubi8-mirror
spec:
imageTagMirrors:
- mirrors:
- example.io/example/ubi-minimal
- example.com/example/ubi-minimal
source: registry.access.redhat.com/ubi8/ubi-minimal
EOF
- 查看 Node 的状态,确认 Node 会重启以使得配置生效。
$ oc get node
NAME STATUS ROLES AGE VERSION
ip-10-0-131-190.ap-southeast-1.compute.internal Ready master 13d v1.18.3+012b3ec
ip-10-0-141-25.ap-southeast-1.compute.internal Readyy,SchedulingDisabled worker 13d v1.18.3+012b3ec
ip-10-0-165-133.ap-southeast-1.compute.internal Ready master 13d v1.18.3+012b3ec
ip-10-0-174-85.ap-southeast-1.compute.internal Ready worker 13d v1.18.3+012b3ec
ip-10-0-216-155.ap-southeast-1.compute.internal Ready master 13d v1.18.3+012b3ec
- 确认配置已经更新,有2个 registry.mirror 的配置。
$ oc debug node <NODE-NAME>
sh-4.2# chroot /host
sh-4.4# cat /etc/containers/registries.conf
unqualified-search-registries = ["registry.access.redhat.com", "docker.io"]
short-name-mode = ""
[[registry]]
prefix = ""
location = "registry.access.redhat.com/ubi8/ubi-minimal"
[[registry.mirror]]
location = "example.com/example/ubi-minimal"
pull-from-mirror = "tag-only"
[[registry.mirror]]
location = "example.io/example/ubi-minimal"
pull-from-mirror = "tag-only"
[[registry]]
prefix = ""
location = "ocr.example.com"
insecure = true
- 在 Node 内部发起对远程镜像 registry.access.redhat.com/ubi8/ubi-minimal 的访问,通过日志确认系统是从 Mirror 中获得该 Image 的。
sh-4.2# podman pull --log-level=debug registry.access.redhat.com/ubi8/ubi-minimal
参考
- https://computingforgeeks.com/how-to-setup-red-hat-quay-registry-on-centos-rhel/
- https://access.redhat.com/documentation/zh-cn/openshift_container_platform/4.7/html-single/images/index#installation-restricted-network-samples_samples-operator-alt-registry
- https://access.redhat.com/documentation/zh-cn/openshift_container_platform/4.7/html-single/images/index#images-configuration-parameters_image-configuration
- https://computingforgeeks.com/allow-insecure-registries-in-openshift-okd-4-cluster/
- https://rhthsa.github.io/openshift-demo/build-with-oc.html
- https://docs.openshift.com/container-platform/4.17/openshift_images/image-configuration.html#images-configuration-registry-mirror_image-configuration
- https://docs.openshift.com/container-platform/4.7/post_installation_configuration/preparing-for-users.html#images-configuration-shortname_post-install-preparing-for-users
- https://www.redhat.com/sysadmin/manage-container-registries
- https://access.redhat.com/solutions/6969479
- https://www.redhat.com/en/blog/filtering-external-registry-images-by-signature-in-openshift-4