OpenShift External/Mirror Image registry是怎麼運作的?

Albert Weng
8 min readOct 19, 2022

--

今天來談談每次我建置OpenShift cluster時,基本上已經算是我的預設配置之一的External Image registry的運作方式,同時也說明另一種稱之為Mirror image registry要如何使用。

圖片來源:https://www.australtech.net/running-a-local-docker-registry/

1. Concept

OpenShift image registry基本上依照取得Image的來源,可以分成內部(internal) 與 外部(external)二種類型。

(1) Internal Registry: 指的就是內建的 image-registry.openshift-image-registry.svc:5000

(2) External Registry: 簡單來說就是可以將OpenShift有支援的各家registry都可以用這種方式與OpenShift串連起來,這種類型又可以再細分成:

  • Remote image registry: 像是docker.io, quay.io
  • Mirror image registry: 可以用來加速取得Image的時間、安裝airgap cluster也會需要
Access relationship within OpenShift

實作上,會將External registry設定透過MCO的方式將修改過的內容,推送至所有節點,可在 /etc/containers/registry.conf 內看到變更後的內容。

2. External registry connect to cluster

#------------------------------------
# S2-1. 如何確認OCP現在有接那些registry url
#------------------------------------
[bastion]$ oc describe image.config.openshift.io/cluster
#------------------------------------
# S2-2. 建立configmap,讓OCP可以通過憑證的檢查
# 注意".."
#------------------------------------
[bastion]$ oc create configmap registry-harbor -n openshift-config --from-file=harbor.dc1.ocp.poc..8443=<certificate_path>
[bastion]$ oc edit image.config.openshift.io/cluster
spec:
additionalTrustedCA:
name: registry-harbor
registrySources:
allowedRegistries:
- example.com
- quay.io
- registry.redhat.io
- reg1.io/myrepo/myapp:latest
- image-registry.openshift-image-registry.svc:5000

確認已完成串接動作

#------------------------------------
# S2-4. Verify
#------------------------------------
[bastion]$ oc debug node/worker01.dc1.ocp.poc
sh4-4$ chroot /host
sh4-4# podman pull harbor.support.ocp.com:8443/library/redis:latest

3. Mirror registry setting

在一個受限網速的環境,利用External registry建構一個mirror功能,用來同步Internet的image使用。

註:外部registry也要設定好mirror mode

When node needs image…

3–1. 建立 ImageContentSourcePolicy object

[bastion]$ vim registryrepomirror.yaml
apiVersion: operator.openshift.io/v1alpha1
kind: ImageContentSourcePolicy
metadata:
name: ubi8repo
spec:
repositoryDigestMirrors:
- mirrors:
- example.io/example/ubi-minimal
source: registry.access.redhat.com/ubi8/ubi-minimal
- mirrors:
- example.com/example/ubi-minimal
source: registry.access.redhat.com/ubi8/ubi-minimal
※ 要取得"registry.access.redhat.com/ubi8/ubi-minimal" 設定走以上二個registry位置

3–2. 到node上確認是否設定成功

[bastion]$ oc debug node/worker01.dc1.ocp.poc
sh4-4$ chroot /host
sh4-4# cat /etc/containers/registry.conf
...
[[registry]]
prefix = ""
location = "registry.access.redhat.com/ubi8/ubi-minimal"
mirror-by-digest-only = true
[[registry.mirror]]
location = "example.com/example/ubi-minimal"
[[registry.mirror]]
location = "example.io/example/ubi-minimal"
[[registry]]
prefix = ""
location = "ocr.example.com"
insecure = true
[[registry]]
prefix = ""
location = "untrusted.com"
blocked = true

4. Conclusion

OpenShift4 在cluster-level對image的處理是由image.config.openshift.io/cluster物件負責。上述範例指的主要是如何串接到我們在外部已經建立好的registry。亦可反過來針對不信任的位置加上blockedRegistries將這些不受信任的全部丟進黑名單,不允許使用。

學會Image registry的操作是很重要的,在未來CI/CD pipeline的設計上,有很大一部分需要知道。簡單二個例子:

Scenario 1: 將測試環境的Image,自動Promo到到正式環境,再push至正式環境的image registryScenario 2: 透過自動化腳本(ex. ansible)push到多個地區的image registry

--

--

Albert Weng
Albert Weng

Written by Albert Weng

You don't have to be great to start, but you have to start to be great

No responses yet