When deploying applications in Kubernetes, you might need to use container images stored in a private or authenticated registry. By default, Kubernetes cannot pull images from registries that require authentication. To solve this, you must provide the registry credentials to Kubernetes in a secure way.
In this blog post, we’ll explain how to configure Kubernetes to pull images from an authenticated registry using both command-line options and a dockerconfigjson Secret. Let’s start!
1. Using the Command Line to Create a Docker Registry Secret
The simplest way to let Kubernetes access your private registry is by creating a Docker registry Secret using the kubectl
command. Here’s how you can do it:
Command
Explanation
my-registry-secret
: The name of the Secret you are creating.--docker-server
: The URL of your private container registry (e.g.,dockerhub.example.com
).--docker-username
: Your registry username.--docker-password
: Your registry password or access token.--docker-email
: Your email address.
Check if the Secret is Created
2. Using a YAML File to Create a Docker Registry Secret
If you prefer a declarative approach, you can create a Secret using a YAML file. This method is useful for version-controlling your configurations.
Steps
- Generate a
.dockerconfigjson
File
Create the Docker credentials file manually:
To get the value for auth
(which is username:password
encoded in Base64), run:
- Encode the
.dockerconfigjson
Base64-encode the entire .dockerconfigjson
file:
- Write the YAML File
Here’s an example YAML file (docker-registry-secret.yaml
):
- Apply the YAML File
Run the following command to create the Secret:
3. Using the Secret in a Deployment
Once the Secret is created, you need to tell Kubernetes to use it when pulling images. This is done by adding the imagePullSecrets
field to your Pod or Deployment specification.
Here’s an example Deployment YAML file:
Deployment YAML
Apply the Deployment:
4. Verifying the Setup
Check Pod Status After applying the Deployment, verify the Pod status:
Inspect Events for Errors If the Pod is not starting, describe it to check for errors:
No comments:
Post a Comment