Skip to main content
In this lab you’ll secure image pulls from a private Docker registry by creating a Kubernetes Secret of type docker-registry and referencing it in a Deployment’s pod spec via imagePullSecrets. This enables Kubernetes to authenticate to private registries when creating Pods.

Which Secret type should you use for Docker registries?

Kubernetes supports multiple Secret types. For authenticating to a Docker registry you must use the docker-registry Secret type. Reference: Kubernetes Secret Types

1) Inspect the existing Deployment and its pod template

List Deployments and inspect the web Deployment to see which image it is currently configured to use:
Example (trimmed) Deployment pod template showing the current image:
If no registry is specified in the image name (for example nginx:alpine), Kubernetes pulls the image from Docker Hub by default.

2) Update the Deployment to use an image from a private registry

Change the container image to point to your private registry. For this lab we’ll use myprivateregistry.com:5000/nginx:alpine as an example. Edit the Deployment and update the image:
After saving your edit, confirm the change:
Relevant excerpt:

3) Pods fail image pull due to missing credentials

Because the registry is private, newly created Pods will fail to pull the image unless credentials are provided. Typical failure symptoms include ImagePullBackOff and error messages indicating unauthorized access. Example error snippet:

4) Create a docker-registry Secret with registry credentials

Create a Secret of type docker-registry that contains the credentials needed to access the private registry. Replace the placeholders with your actual registry server, username, password, and email:
Expected response:
This Secret stores the equivalent of a Docker config.json entry that Kubernetes can use to authenticate when pulling images.

5) Configure the Deployment to use the Secret via imagePullSecrets

Add an imagePullSecrets entry under the Deployment’s pod spec (spec.template.spec) that references the Secret name (private-reg-cred). This tells Kubernetes to use those credentials when pulling images for Pods created by the Deployment. Kubernetes example of the pod field:
Edit your Deployment to include imagePullSecrets:
Modify the pod template portion to include the Secret reference:
Save the edit. The Deployment controller will perform a rolling update to replace Pods with new ones that include the imagePullSecrets configuration. Monitor rollout and pod status:
Example pod status during/after rolling update:
You should see new Pods successfully transition to Running while older replica set Pods are scaled down/terminated. Kubernetes will use the credentials stored in the docker-registry Secret, allowing image pulls from your private registry to succeed.
Note: If you do not specify a registry in the image name (for example, just nginx:alpine), Kubernetes pulls the image from Docker Hub by default. Use imagePullSecrets in the pod spec to supply credentials for private registries.

References and further reading

Watch Video