Openshift ImageStreams quickstart

|
Tags:  Openshift

ImageStreams are a feature in OpenShift that offer virtual Docker images.

An ImageStream virtual image points to actual Docker images located in other registries. During deployment, ImageStreams act as proxies, supplying the real images.

Utilizing ImageStreams is useful when changing the image of a deployment: Instead of altering the deployment template, you can simply update the ImageStream tag.

Moreover, you can configure triggers to automatically initiate rollouts when an ImageStream tag changes.

In this post I explain the basic commands for managing Openshift ImageStreams and how to configure ImageStreams in deployments.

Create an ImageStream

ImageStreams are stored in namespaces. First, select the namespace where you want to create it: oc project [NAMESPACE_NAME]

After, run this to create the ImageStream: oc create imagestream [NAME]

Execute oc describe imagestream [NAME] to see the properties of the ImageStream, including the repository of the virtual image.

Example:

PS C:\examples> oc create imagestream is-example
imagestream.image.openshift.io/is-example created
PS C:\examples> oc describe imagestream is-example
Name:                   is-example
Namespace:              examples
Created:                11 seconds ago
Labels:                 <none>
Annotations:            <none>
Image Repository:       default-route-openshift-image-registry.apps-crc.testing/examples/is-example
Image Lookup:           local=false
Tags:                   <none>
PS C:\examples>

Create or update ImageStream tags

Use the command oc tag [REMOTE_IMAGE]:[TAG] [IMAGESTREAM_NAME]:[TAG] to create or change the tags of the image stream.

Example:

PS C:\examples> oc tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 is-example:prod
Tag is-example:prod set to us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0.
PS C:\examples> oc tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0 is-example:prod
Tag is-example:prod set to us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0.
PS C:\examples>

Each ImageStream has a history of changes which provides useful information for debugging purposes. Run the oc describe imagestream [IMAGESTREAM_NAME] command to see it.

PS C:\examples> oc describe imagestream is-example
Name:                   is-example
Namespace:              examples
Created:                14 minutes ago
Labels:                 <none>
Annotations:            openshift.io/image.dockerRepositoryCheck=2023-08-20T18:15:06Z
Image Repository:       default-route-openshift-image-registry.apps-crc.testing/examples/is-example
Image Lookup:           local=false
Unique Images:          2
Tags:                   1

prod
  tagged from us-docker.pkg.dev/google-samples/containers/gke/hello-app:2.0

  * us-docker.pkg.dev/google-samples/containers/gke/hello-app@sha256:c4b0d869bd22cb3d1207848b385c389f7a4ab7a481184476ee7b98e7876ee594
      24 seconds ago
    us-docker.pkg.dev/google-samples/containers/gke/hello-app@sha256:845f77fab71033404f4cfceaa1ddb27b70c3551ceb22a5e7f4498cdda6c9daea
      About a minute ago
PS C:\examples>

Add ImageStreams to deployment templates

To use ImageStreams in deployments just put the image repository and the desired tag in the value of the property image

Example:

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: is-example-deployment
spec:
  selector:
    app: is-example
  replicas: 1
  template:
    metadata:
      labels:
        app: is-example
    spec:
      containers:
      - name: is-example
        image: default-route-openshift-image-registry.apps-crc.testing/examples/is-example:prod
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 3
          periodSeconds: 3

Trigger rollout when ImageStream changes

To run a rollout automatically when an ImageStream tag changes add an ImageChange trigger with the reference to the ImageStreams image, tag, and the containers that you want to rollout.

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: is-example-deployment
spec:
  selector:
    app: is-example
  replicas: 1
  template:
    metadata:
      labels:
        app: is-example
    spec:
      containers:
      - name: is-example
        image: default-route-openshift-image-registry.apps-crc.testing/examples/is-example:prod
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 3
          periodSeconds: 3
  triggers:
  - type: ImageChange
    imageChangeParams:
      automatic: true
      containerNames:
        - is-example
      from:
        kind: ImageStreamTag
        name: 'is-example:prod'
        namespace: examples

Example:

PS C:\examples> oc get pods
NAME                            READY   STATUS    RESTARTS   AGE
is-example-deployment-3-l4xk9   1/1     Running   0          68s
PS C:\examples> oc tag us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0 is-example:prod
Tag is-example:prod set to us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0.
PS C:\examples> oc get pods
NAME                             READY   STATUS      RESTARTS   AGE
is-example-deployment-4-d6ql2    1/1     Running     0          8s
is-example-deployment-4-deploy   0/1     Completed   0          10s
PS C:\examples>

References and sources

Author

Sergio Monedero

DevOps engineer, Systems Administrator, and Developer. I enjoy staying up to date on the latest technology trends. This is my personal website where I share my knowledge and insights.

You can also follow me on LinkedIn as SergioCoder, or Github as Branyac