[Refer To] https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/
Kubernetes
This page shows how to create a Pod that uses a Secret to pull an image from a private container image registry or repository. There are many private registries in use. This task uses Docker Hub as an example registry.
Log in to Docker Hub
First log in to you private registry with:
docker loginView the config.json file:
cat ~/.docker/config.jsonYou should see output contains a section similar to this:
{
"auths": {
"https://your.private.registry.example.com/": {
"auth": "s9a...Ou1"
}
}
}Create a Secret based on existing credentials
A Kubernetes cluster uses the Secret of kubernetes.io/dockerconfigjson type to authenticate with a container registry to pull a private image.
If you already ran docker login, you can copy that credential into Kubernetes:
kubectl create secret generic regcred \
--from-file=.dockerconfigjson=<path/to/.docker/config.json> \
--type=kubernetes.io/dockerconfigjsonNOTE: By default, the secrets are stored under default namespace. if you want to use the secrets for other namespaces, you should first switch namespace, and then you can use the above command to create secret.
TIP: You can use kubectl delete secret <secret-name> if you want to delete the existing secret.
Create a Secret by providing credentials on the command line
Create this Secret, naming it regcred
kubectl create secret docker-registry regcred --docker-server=<your-registry-server> --docker-username=<your-name> --docker-password=<your-pword> --docker-email=<your-email>Inspecting the Secret regcred
kubectl get secret regcred --output=yamlThe output is similar to this:
apiVersion: v1
kind: Secret
metadata:
...
name: regcred
...
data:
.dockerconfigjson: eyJodHRwczovL2luZGV4L ... J0QUl6RTIifX0=
type: kubernetes.io/dockerconfigjsonThe value of the .dockerconfigjson field is a base64 representation of your Docker credentials.
To understand what is in the .dockerconfigjson field, convert the secret data to a readable format:
kubectl get secret regcred --output="jsonpath={.data.\.dockerconfigjson}" | base64 --decodeThe output is similar to this:
{"auths":{"your.private.registry.example.com":{"username":"user","password":"xxxxxxxxxxx","email":"[email protected]","auth":"s9a...Ou1"}}}To understand what is in the auth field, convert the base64-encoded data to a readable format:
echo "s9a...Ou1" | base64 --decodeThe output, username and password concatenated with a :, is similar to this:
user:xxxxxxxxxxxNotice that the Secret data contains the authorization token similar to your local ~/.docker/config.json file.
You have successfully set your Docker credentials as a Secret called regcred in the cluster.

