Image Registry Lab¶
40 min Intermediate
In the Containers Lab you pushed the greeting image to Quay.io. In this lab you'll run that image on OpenShift, pull it from a private repository with a pull secret, and then build the image inside the cluster and store it in OpenShift's internal registry.
Prerequisites¶
- You've finished the Containers Lab, and
quay.io/<your-username>/greeting:v1.0.0exists and is public. If you built it on an Apple Silicon Mac, build it forlinux/amd64(or multi-arch), as described at the end of that lab. - You're logged in to an OpenShift cluster with
oc. See lab environment options. - You still have the
greetingdirectory withmain.go,go.modandContainerfile.
Part 1: Deploy an image from a public registry¶
-
Create a Deployment named
greetingfrom your Quay image, with container port8080. -
Expose the Deployment as a Service, then expose the Service as a Route.
-
Call the app through its Route:
Part 2: Pull from a private repository¶
Most company images live in private repositories. The cluster needs credentials, a pull secret, to download them.
-
On quay.io, open the
greetingrepository, go to Settings, and make it private. -
Force a new pull by restarting the Deployment. The new pod fails to start. Find out why.
Solution
oc rollout restart deployment/greeting oc get pods oc describe pod -l app=greeting | grep -A 5 EventsThe new pod shows
ErrImagePull/ImagePullBackOff, with anunauthorizederror in the events. The old pod keeps serving traffic, because the rolling update never removes the working pod before its replacement is ready. -
Create a robot account with read access to the repository:
- In Quay, open Account Settings > Robot Accounts and create a robot account, for example
openshift_puller. - Give it Read permission on the
greetingrepository. - Open the robot account and copy its username (
<your-username>+openshift_puller) and token.
Robot accounts are better than your personal password: each one is scoped to specific repositories and can be revoked on its own.
- In Quay, open Account Settings > Robot Accounts and create a robot account, for example
-
Create a pull secret named
quay-pullfrom the robot credentials, and link it to thedefaultServiceAccount for pulling images.Solution
oc create secret docker-registry quay-pull \ --docker-server=quay.io \ --docker-username="${QUAY_USER}+openshift_puller" \ --docker-password='<robot-token>' oc secrets link default quay-pull --for=pulloc secrets linkadds the secret to the ServiceAccount'simagePullSecrets. Every pod that runs as that ServiceAccount can then pull with it. You could also list the secret underspec.imagePullSecretsin each Pod template. -
Delete the failing pod so it's recreated with the pull secret, and check that the rollout completes.
Part 3: Build inside the cluster¶
OpenShift can build images itself, without Podman on your laptop and without an external registry. A BuildConfig describes the build. The result goes into the cluster's internal registry, where an ImageStream tracks it.
-
From inside your
greetingdirectory, create a binary build namedgreeting-internalthat uses your Containerfile. -
Start the build, upload the current directory, and follow the build log.
-
Look at the ImageStream that tracks the image.
-
Deploy the image from the ImageStream with a custom greeting, and expose it.
Solution
oc new-app greeting-internal --name=greeting-internal -e GREETING="Built on OpenShift" oc expose service greeting-internal curl "http://$(oc get route greeting-internal -o jsonpath='{.spec.host}')/greeting"oc new-appcreates a Deployment and a Service. Because the Deployment uses an ImageStream tag, a later build automatically rolls out the new image.
Builds from Git
Instead of uploading a local directory, you can build straight from a Git repository: oc new-build https://github.com/<you>/<repo>.git --strategy=docker. In the Tekton lab you'll automate build and deploy with a pipeline.
Cleanup¶
What you learned¶
- Deploying and exposing an image from a public registry
- Pulling from private registries with robot accounts and pull secrets
- Building images in-cluster with BuildConfigs, ImageStreams and the internal registry