OpenShift 4 Hands-on Lab (3) - 用ImageStream操作Image

文章目录

  • 什么是ImageStream
  • 从Image生成ImageStream
  • ImageStream的操作
  • 生成ImageStream过程的选项
  • 常见问题

什么是ImageStream

Image Stream是OpenShift中的一种对象,它并不是用来存储Image的,而是用来记录并管理Image信息的。它就像是所有Image的虚拟视图,可以通过它统一访问存放在不同Image Registry(OpenShift外部独立运行的Registry、或是OpenShift内置的Registry)上的Image。通过这个视图可以简化对那些来自不同Registry的Image的访问。Image Stream会使用访问标签(Image Stream Tag)和指向信息(Image Stream Image)记录这种对应关系,其中指向Image的指针是由Image Stream Name和Image ID构成的。

OpenShift 4 Hands-on Lab (3) - 用ImageStream操作Image_第1张图片
根据Image Stream所在项目,可将其分为集群级和项目级的Image Stream。无论当前在哪个项目中,任何用户都可访问到集群级的Image Stream;而项目级Image Stream只能该项目用户或被授权用户才可访问。

从Image生成ImageStream

如果我们跟随《OpenShift 4 Hands-on Lab (1) - 部署应用》成功创建应用后可以通过以下命令发现OpenShift会自动生成ImageStream对象,这就说明在运行应用前,OpenShift要创建ImageStream并建立App Image的指向关系。

oc get is

OpenShift除了可以自动创建ImageStream外,我们还可以从定义ImageStream的YAML文件创建它,但是我们通常用以下两种方法手工从Image生成ImageStream:

  1. 首选创建名为imagestream-lab1的OpenShift项目,以下所有操作都缺省在该项目中进行。
$ oc new-project USER-ID-imagestream-lab1
  1. 通过import-image导入镜像的信息并生成ImageStream。以下将docker.io/openshift/deployment-example:v1的镜像信息导入到名为is-example的ImageStream中,并用v1指向该远程镜像。
$ oc import-image --from=docker.io/openshift/deployment-example:v1 is-example:v1 --confirm
Name:                   is-example
Namespace:              USER-ID-imagestream-lab1
Created:                Less than a second ago
Labels:                 <none>
Annotations:            openshift.io/image.dockerRepositoryCheck=2020-02-02T04:27:15Z
Image Repository:       image-registry.openshift-image-registry.svc:5000/USER-ID-imagestream-lab1/is-example
Image Lookup:           local=false
Unique Images:          1
Tags:                   1
 
v1
  tagged from docker.io/openshift/deployment-example:v1
 
  * docker.io/openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
      Less than a second ago
 
Image Name:     is-example:v1
Docker Image:   docker.io/openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
Name:           sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
Created:        Less than a second ago
Annotations:    image.openshift.io/dockerLayersOrder=ascending
Image Size:     5.77MB in 6 layers
Layers:         0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                5.77MB  sha256:50438f3701c47319ff1c8189ff19f5a8c779f2479aa2066979b930c7dbb3bde8
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
Image Created:  4 years ago
Author:         Clayton Coleman <[email protected]>
Arch:           amd64
Entrypoint:     /deployment v1
Working Dir:    <none>
User:           <none>
Exposes Ports:  8080/tcp
Docker Labels:  <none>
Environment:    COLOR=#006e9c
  1. 通过tag对执行镜像打标签。下面是将docker上的openshift/deployment-example:v2打标签为v2,并保存到名为is-example的ImageStream中。
$ oc tag --source=docker openshift/deployment-example:v2 is-example:v2
Tag is-example:v2 set to openshift/deployment-example:v2.

ImageStream的操作

  1. 查看ImageStream列表
$ oc get is
NAME         IMAGE REPOSITORY                                                                                                               TAGS    UPDATED
is-example   default-route-openshift-image-registry.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com/USER-ID-imagestream-lab1/is-example   v2,v1   9 minutes ag
  1. 查看ImageStream配置
$ oc describe is is-example
Name:                   is-example
Namespace:              USER-ID-imagestream-lab1
Created:                12 minutes ago
Labels:                 <none>
Annotations:            openshift.io/image.dockerRepositoryCheck=2020-02-02T04:33:29Z
Image Repository:       default-route-openshift-image-registry.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com/USER-ID-imagestream-lab1/is-example
Image Lookup:           local=false
Unique Images:          2
Tags:                   2
 
v1
  tagged from docker.io/openshift/deployment-example:v1
 *. docker.io/openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
      12 minutes ago
 
v2
  tagged from openshift/deployment-example:v2
 *. openshift/deployment-example@sha256:1318f08b141aa6a4cdca8c09fe8754b6c9f7802f8fc24e4e39ebf93e9d58472b
      12 minutes ago
  1. 查看ImageStreamTag列表
$ oc get istag
NAME            IMAGE REF                                                                                                        UPDATED
is-example:v1   docker.io/openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b   10 minutes ago
is-example:v2   openshift/deployment-example@sha256:1318f08b141aa6a4cdca8c09fe8754b6c9f7802f8fc24e4e39ebf93e9d58472b             10 minutes ago
  1. 查看ImageStreamTag配置
$ oc describe istag is-example:v1
Image Name:     sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
Docker Image:   docker.io/openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
Name:           sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
Created:        2 hours ago
Annotations:    image.openshift.io/dockerLayersOrder=ascending
Image Size:     5.77MB in 6 layers
Layers:         0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                5.77MB  sha256:50438f3701c47319ff1c8189ff19f5a8c779f2479aa2066979b930c7dbb3bde8
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
                0B      sha256:a3ed95caeb02ffe68cdd9fd84406680ae93d633cb16422d00e8a7c22955b46d4
Image Created:  4 years ago
Author:         Clayton Coleman <[email protected]>
Arch:           amd64
Entrypoint:     /deployment v1
Working Dir:    <none>
User:           <none>
Exposes Ports:  8080/tcp
Docker Labels:  <none>
Environment:    COLOR=#006e9c
  1. 按大小排序查看USER-ID-imagestream-lab1项目中的imagestreams
$ oc adm top imagestreams -n USER-ID-imagestream-lab1
NAME                       			STORAGE  IMAGES LAYERS
USER-ID-imagestream-lab1/is-example 5.503MiB 2      12
  1. 执行命令,将“is-example:v1”指向“docker openshift/deployment-example:v2”
$ oc tag --source=docker openshift/deployment-example:v2 is-example:v1
  1. 查看is-example,其中v1的指向的ImageStreamImage记录为2条,当前v1和v2指向的是同一个Image。而v1以前指向的是另一个Image。
$ oc describe is is-example
Name:                   is-example
Namespace:              USER-ID-imagestream-lab1
Created:                4 minutes ago
Labels:                 <none>
Annotations:            openshift.io/image.dockerRepositoryCheck=2020-02-02T14:07:48Z
Image Repository:       default-route-openshift-image-registry.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com/USER-ID-imagestream-lab1/is-example
Image Lookup:           local=false
Unique Images:          2
Tags:                   2
 
v1
  tagged from openshift/deployment-example:v2
 
  * openshift/deployment-example@sha256:1318f08b141aa6a4cdca8c09fe8754b6c9f7802f8fc24e4e39ebf93e9d58472b
      About a minute ago
    docker.io/openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
      4 minutes ago
 
v2
  tagged from openshift/deployment-example:v2
  * openshift/deployment-example@sha256:1318f08b141aa6a4cdca8c09fe8754b6c9f7802f8fc24e4e39ebf93e9d58472b
      3 minutes ago
  1. 删除名为“is-example:v2”的ImageStreamTag
$ oc delete istag/is-example:v2

生成ImageStream过程的选项

在使用“oc tag”和“oc import-image”命令生成ImageStream的时候,可以使用参数“–reference-policy=local”让OpenShift只在本地(其内部的Image Registry)找镜像;另外还可以用“–reference”参数让OpenShift只创建一个非导入的ImageStreamTag,即不导入目标Image的元数据。下面我们看看这两个选项对ImageStream有什么影响?

  1. 新建imagestream-lab2项目。
$ oc new-project USER-ID-imagestream-lab2
  1. 执行以下命令,使用不同选项生从“openshift/deployment-example:v1”成相关ImageStream资源。
$ oc tag --source=docker openshift/deployment-example:v1 is-example:v1 
$ oc tag --source=docker openshift/deployment-example:v1 is-example:v2 --reference-policy=local 
$ oc tag --source=docker openshift/deployment-example:v1 is-example:v3 --reference=true
  1. 查看ImageStream的信息,注意3个Tags它们之间的差异,其中v3没有相应的镜像ID(说明当使用–reference=true,OpenShift不会主动获取镜像元数据)。
$ oc describe is is-example
Name:                   is-example
Namespace:              imagestream-lab2
Created:                2 minutes ago
Labels:                 <none>
Annotations:            openshift.io/image.dockerRepositoryCheck=2020-02-02T11:14:02Z
Image Repository:       default-route-openshift-image-registry.apps.cluster-beijing-c70a.beijing-c70a.example.opentlc.com/USER-ID-imagestream-lab2/is-example
Image Lookup:           local=false
Unique Images:          2
Tags:                   3
 
v1
  tagged from openshift/deployment-example:v1
  * openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
      2 minutes ago
  
v2
  tagged from openshift/deployment-example:v1
    prefer registry pullthrough when referencing this tag
  * openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b
      2 minutes ago
 
v3
  reference to registry openshift/deployment-example:v1
  * openshift/deployment-example:v1
      2 minutes ago
  1. 查看ImageStreamTag列表,确认没有“is-example:v3”,说明它是无效的。
$ oc get istag
NAME            IMAGE REF                                                                                                                                             UPDATED
is-example:v1   openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b                                                  2 minutes ago
is-example:v2   image-registry.openshift-image-registry.svc:5000/USER-ID-imagestream-lab2/is-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b   2 minutes ago
  1. 在OpenShift控制台也可看出“is-example:v3”的错误提示。
    OpenShift 4 Hands-on Lab (3) - 用ImageStream操作Image_第2张图片
  2. 执行以下命令,根据已有的ImageSteamTag生成新的ImageSteamTag,这次使用了“–reference=true”参数。
oc tag --source=istag is-example:v1 is-example:v4 --reference=true
  1. 再次查看ImageStreamTag,确认“is-example:v4”是正常的。虽然这次使用了“–reference=true”参数,即OpenShift不会主动获取镜像元数据,但是这些信息已经在本地有了(是在创建“is-example:v1”的步骤有的)。
$ oc get istag
NAME            IMAGE REF                                                                                                                                             UPDATED
is-example:v1   openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b                                                  22 minutes ago
is-example:v2   image-registry.openshift-image-registry.svc:5000/USER-ID-imagestream-lab2/is-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b   22 minutes ago
is-example:v4   openshift/deployment-example@sha256:c505b916f7e5143a356ff961f2c21aee40fbd2cd906c1e3feeb8d5e978da284b                                                  About a minute ago

常见问题

  1. 根据远程的Image创建ImageStream的时候,Image是否会下载到本地?
    说明:不会。只有第一次基于这个Image运行应用的时候才去下载这个Image。
  2. 当使用“–reference-policy=local”参数和不使用它的时候,对使用Image有什么影响?
    说明:当使用“–reference-policy=local”后,OpenShift就不会访问其外部的Registry获得Image了,只会使用其内置的Registry获取Image了。因此需要手动将Image提前push到其内置的Registry才可 使用这些Image。
  3. 删除了ImageStreamTag后,对应的本地Image有影响么?
    说明:没有。两者是若关联关系,删除任何一个不会自动删除相关的另一方。

你可能感兴趣的:(OpenShift,4,Ops)