Sub topic 10
Fixing private registry access with imagePullSecrets
This chapter assumes you can read pod events with kubectl describe pod and know that a pod stuck in ErrImagePull or ImagePullBackOff failed to download its image. It covers the case where the image exists but the cluster has no permission to pull it.
Recognize an authentication failure
Start with the Events section of kubectl describe pod web. The message tells you which kind of pull failure you have.
| Event message contains | Likely cause |
|---|---|
| `manifest unknown` or `not found` | Wrong image name or tag |
| `pull access denied`, `401 Unauthorized`, or `authorization failed` | Missing or wrong credentials |
| `no such host` or `i/o timeout` | DNS or network problem |
Only the second row is fixed with an imagePullSecrets entry. Some registries return "not found" for private images when you are not logged in, so check the credentials even when the message says "not found".
Create the pull secret
A registry credential is stored as a Secret of type kubernetes.io/dockerconfigjson. This command builds one:
kubectl create secret docker-registry regcred \
--docker-server=registry.example.com \
--docker-username=<user> \
--docker-password=<password>The secret is namespaced. A pod can only reference a secret in its own namespace, so create regcred in every namespace that pulls from this registry. Use -n <namespace> to target one.
Reference the secret from the pod
Add imagePullSecrets under the pod spec. The kubelet (the node agent that starts containers) then sends these credentials to the registry.
apiVersion: v1
kind: Pod
metadata:
name: web
spec:
imagePullSecrets:
- name: regcred
containers:
- name: app
image: registry.example.com/team/web:1.4.2In a Deployment, the same field goes under spec.template.spec. To avoid repeating it, attach the secret to the namespace's default service account. New pods in that namespace then inherit it:
kubectl patch serviceaccount default \
-p '{"imagePullSecrets":[{"name":"regcred"}]}'Existing pods do not pick up this change. Delete them so their controller recreates them.
Verify and retry
The kubelet retries on a back-off timer, so a fixed pod may take a few minutes to recover. To skip the wait, delete the pod and let the controller recreate it. Then confirm the event sequence changes from Failed to Pulled:
kubectl get events --field-selector involvedObject.name=web --sort-by=.lastTimestampExercise
A pod in namespace staging reports pull access denied. You created regcred earlier in namespace default and listed it in the pod's imagePullSecrets. Why does the pull still fail, and how do you fix it?
Show answer
Secrets are namespaced, and a pod can only use pull secrets from its own namespace. Create regcred in staging with kubectl create secret docker-registry regcred ... -n staging. Then delete the pod so it is recreated and the pull is retried.