Skip to main content
In this guide, we will walk through a hands-on lab focusing on securing container images within a Kubernetes environment. We will cover how to correctly use a Docker registry secret and update a deployment to pull an image from a private registry.

Choosing the Right Secret Type for a Docker Registry

Kubernetes supports three types of secrets: Docker registry, generic, and TLS. For our use case, as we need to authenticate with a Docker registry, we will be using the Docker registry secret. Below is the help output for the secret creation command:

Checking the Current Deployment

We begin by examining our cluster’s web application deployment. Running the following command shows that the deployment named web is running two replicas with the nginx:alpine image:
Since our goal is to pull the image from an internal private registry, we need to update the deployment to use an image hosted at myprivateregistry.com:5000 instead of the default Docker Hub image.

Inspecting the Deployment Configuration

To understand the current state of the deployment, inspect its detailed configuration with:

Updating the Deployment to Use a Private Registry Image

Edit the deployment configuration to specify the new image and prepare to add the required image pull secret. Below is an excerpt of the updated configuration (parts that remain unchanged have been omitted for brevity):
After saving the updated deployment, verify that the new image name is set. As part of the rolling update process, a new replica set is created:

Troubleshooting: ImagePullBackOff Error

When checking the pods, you may notice that while the old pods remain active, the new pod reports an ImagePullBackOff error:
Inspect the pod details to reveal an error message indicating an authentication failure when pulling the image:
The error indicates that the credentials for accessing your private registry are missing. Without valid credentials, Kubernetes will not be able to pull the image.

Creating the Docker Registry Secret

To resolve this issue, create a secret containing the necessary credentials. Replace the placeholders with your actual registry information:
For example, to create the secret for our private registry, run:

Updating the Pod Template for Image Pull Secrets

After successfully creating the secret, update the deployment’s pod template to include the image pull secret. According to the Kubernetes Documentation, add the following block under the pod specification:
Below is a complete example of a pod configuration that pulls an image from a private registry:
Edit the deployment with the following command to include the image pull secret:
After saving your changes, Kubernetes will update the deployment by terminating old pods and creating new ones that use the private registry image along with the correct authentication.

Final Verification

Ensure that all pods are updated and running with the new image and the secret is applied correctly. Use the following command to verify:
Once you confirm that all pods have successfully pulled the image and are running, the lab is complete. Enjoy the enhanced security and efficiency of your Kubernetes deployments!
For more information on securing Kubernetes deployments and managing Docker registry secrets, refer to the Kubernetes Documentation.

Watch Video