从外部公开OpenShift内部注册表并使用Docker/Podman CLI登录

时间:2020-02-23 14:31:30  来源:igfitidea点击:

OpenShift容器平台提供了一个内部集成的容器镜像注册表,可以将其部署在OpenShift容器平台环境中以本地管理镜像。通过此注册表,我们可以从源代码构建容器镜像,将它们部署在OpenShift平台上并管理其生命周期。在初始群集设置过程中,我们将设置内部注册表。文档的"在现有群集上部署注册表"部分下提供了完整的安装教程。

配置OpenShift内部镜像注册表

在不提供共享对象存储的基础架构平台上,OpenShift Image Registry Operator会将自身引导为"已删除"。由于我在裸机服务器上运行群集,因此我将注册表操作员配置managementState从Removed更改为Managed。

$oc edit configs.imageregistry/cluster
spec:
  managementState: Managed

我们还需要为内部注册表设置持久性批量声明。请参见以下示例。

...
storage:
    pvc:
      claim: ocs4registry

确认pvc已绑定在镜像注册表名称空间中。

$oc get pvc -n openshift-image-registry
NAME           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
ocs4registry   Bound    pvc-a07963ea-2b23-477f-936d-4f8f674de9a5   100Gi      RWX            cephfs         57d

确认我们没有注册表Pod:

$oc get pod -n openshift-image-registry
NAME                                               READY   STATUS      RESTARTS   AGE
cluster-image-registry-operator-674b759cfb-vvsmr   2/2     Running     0          41d
image-pruner-1600387200-5qzgn                      0/1     Completed   0          2d10h
image-pruner-1600473600-x8rd6                      0/1     Completed   0          34h
image-pruner-1600560000-ss6mn                      0/1     Completed   0          10h
image-registry-6f4b4db789-2wdmt                    1/1     Running     0          41d
node-ca-7pkp4                                      1/1     Running     0          53d
node-ca-f5pnq                                      1/1     Running     0          53d
node-ca-h5v2f                                      1/1     Running     0          53d
node-ca-ldgvv                                      1/1     Running     0          53d
node-ca-ldplz                                      1/1     Running     0          53d
node-ca-rl8xt                                      1/1     Running     0          53d
node-ca-s59td                                      1/1     Running     0          53d
node-ca-shk7l                                      1/1     Running     0          53d
node-ca-t7ghk                                      1/1     Running     0          53d
node-ca-vk9sl                                      1/1     Running     0          53d
node-ca-xjz45                                      1/1     Running     0          53d
node-ca-xr75h                                      1/1     Running     0          53d

在外部公开OpenShift内部镜像注册表

在安装注册表时,它不会在外部公开。这意味着只能在群集内部内部使用注册表。对于外部访问,需要使用OpenShift路由公开服务。

可以通过使用configs.imageregistry.operator.openshift.io资源中的DefaultRoute参数或者使用自定义路由来公开路由。我们将运行以下命令,以通过修改DefaultRoute参数公开路由。

oc patch configs.imageregistry.operator.openshift.io/cluster --patch '{"spec":{"defaultRoute":true}}' --type=merge

预期产量:

config.imageregistry.operator.openshift.io/cluster patched

确认路由已创建。

$oc get  route  -n openshift-image-registry
NAME            HOST/PORT                                                          PATH   SERVICES         PORT    TERMINATION   WILDCARD
default-route   default-route-openshift-image-registry.apps.ocp.example.net               image-registry   <all>   reencrypt     None

使用Docker登录到OpenShift注册表| Podman

使用oc命令行工具登录到OpenShift群集。

$oc login https://api.<cluster>.<domain>:6443

登录后,使用以下命令自动获取注册表路由。

HOST=$(oc get route default-route -n openshift-image-registry --template='{{ .spec.host }}')

我们可以使用以下方法验证该值:

$echo $HOST

然后,我们可以使用以下命令登录到我们公开的注册表:

$podman login -u $(oc whoami) -p $(oc whoami -t) --tls-verify=false $HOST

使用Docker CLI登录:

$docker login -u $(oc whoami) -p $(oc whoami -t) --tls-verify=false $HOST

将容器镜像推送到OpenShift注册表

要将容器镜像推送到注册表,我们首先需要标记它们。请参见以下示例。

$docker pull busybox:latest
$docker tag busybox:latest registry.dev.example.com/testplatform/busybox:latest
$docker push  registry.dev.example.com/testplatform/busybox:latest
$oc get is busybox

将图像推送到注册表后,将自动创建一个OpenShift ImageStream。