Skip to navigation

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 kubectl connected 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

1

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:

read -rs IBEE_REGISTRY_TOKEN
# Paste the access token and press Enter.

Create the Secret in the namespace where the workloads run. The username contains a $ character, so wrap it in single quotes:

kubectl create secret docker-registry ibee-registry \
--namespace example-app \
--docker-server=registry.ibee.ai \
--docker-username='PASTE-REGISTRY-USERNAME' \
--docker-password="$IBEE_REGISTRY_TOKEN"

A Secret is only visible to pods in its own namespace. Create one in every namespace that pulls from the registry.

2

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:

kubectl patch serviceaccount default -n example-app \
-p '{"imagePullSecrets":[{"name":"ibee-registry"}]}'

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:

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-api
namespace: example-app
spec:
replicas: 2
selector:
matchLabels:
app: web-api
template:
metadata:
labels:
app: web-api
spec:
imagePullSecrets:
- name: ibee-registry
containers:
- name: web-api
image: registry.ibee.ai/acme-prod/web-api:v1.4.2
3

Restart pods that already failed

A service account’s pull secrets are added to pods when the pods are created. Pods already stuck in ImagePullBackOff keep failing until they’re replaced:

kubectl rollout restart deployment/web-api -n example-app

Verify

Check that the Secret holds the right server and username:

kubectl get secret ibee-registry -n example-app \
-o jsonpath='{.data.\.dockerconfigjson}' | base64 --decode

Then check that the pods are running:

kubectl get pods -n example-app

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:

read -rs IBEE_REGISTRY_TOKEN
kubectl create secret docker-registry ibee-registry \
--namespace example-app \
--docker-server=registry.ibee.ai \
--docker-username='PASTE-REGISTRY-USERNAME' \
--docker-password="$IBEE_REGISTRY_TOKEN" \
--dry-run=client -o yaml | kubectl apply -f -

--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:

Registry--docker-server
IBEE Container Registryregistry.ibee.ai
Docker Hubhttps://index.docker.io/v1/
GitHub Container Registryghcr.io
GitLab Container Registryregistry.gitlab.com

Troubleshooting

You seeWhat it meansWhat to do
ErrImagePull or ImagePullBackOff with 401 Unauthorized or authentication requiredThe pod has no valid credentials for the registryCheck that ibee-registry exists in the pod’s namespace and is attached to its service account or pod spec. Check that the username kept its $: recreate the Secret with the username in single quotes. Rotate the token if it expired or was revoked.
403 Forbidden or deniedThe token doesn’t cover this registryCreate a Pull only token for this registry and update the Secret.
not found or manifest unknownThe image name or tag doesn’t existCheck the full image name, registry.ibee.ai/REGISTRY-NAME/REPOSITORY:TAG, against the registry’s Overview tab.
Pods still fail after you patched the service accountThe pods were created before the patchRestart the workload with kubectl rollout restart.
Pods in one namespace pull, pods in another don’tSecrets are per namespaceCreate the Secret in the other namespace and attach it there too.
error: failed to create secret ... already existsThe Secret is already thereUpdate it with the kubectl apply command in Rotate the registry token.