Pull private images in Kubernetes
Kubernetes needs registry credentials to pull images from a private registry. You store them in an image pull secret (a Secret of type kubernetes.io/dockerconfigjson) in each namespace that runs the images, then point workloads at it: once on the namespace’s service account, or in each pod spec.
The examples use IBEE Container Registry (registry.ibee.ai). The same steps work for other private registries with their own server address and credentials.
Before you begin
- A cluster and
kubectlconnected to it. - A private registry in Container Registry with the image pushed to it.
- A registry access token with Pull only permission for that registry. See Create an access token. Keep the Registry username and Access token at hand.
- The namespace the workloads run in. The examples use
example-app.
Steps
Create the image pull secret
Read the token into a shell variable without echoing it, so it doesn’t end up in your shell history:
Create the Secret in the namespace where the workloads run. The username contains a $ character, so wrap it in single quotes:
A Secret is only visible to pods in its own namespace. Create one in every namespace that pulls from the registry.
Attach it to your workloads
Pick one of these.
Attach it to the service account (recommended). Every pod that runs as the service account uses the secret, so your manifests don’t need to mention it. Pods use the default service account unless their spec says otherwise:
This replaces the service account’s list of pull secrets. If it already has some, include them in the list. Check with kubectl get serviceaccount default -n example-app -o yaml.
Or name it in the pod spec. Add imagePullSecrets to each Deployment, StatefulSet, Job or Pod:
Verify
Check that the Secret holds the right server and username:
Then check that the pods are running:
The pods show Running. If one shows ErrImagePull or ImagePullBackOff, run kubectl describe pod POD-NAME -n example-app and read the Events at the end. See Troubleshooting.
Rotate the registry token
Registry access tokens expire (after 90 days by default), and Rotate in the portal stops the old token immediately. After that, every new pull fails until you update the Secret: new deployments, scale-ups, and pods moved to another node. Pods that are already running keep running.
Before the token expires, or right after you rotate it, update the Secret in place:
--dry-run=client -o yaml | kubectl apply -f - creates the Secret if it’s missing and updates it if it exists, so the same command works from a CI pipeline. Workloads pick up the new credentials on their next pull without a restart.
To keep the token out of your cluster manifests and rotate it in one place, store it in Secret Manager and sync it with External Secrets Operator.
Other registries
Use the registry’s own server address and a read-only token or deploy token created as your provider documents:
Troubleshooting
Related pages
- Container Registry: create registries, access tokens and push images
- Deploy to Kubernetes from GitHub Actions
- Kubernetes with External Secrets
