Sub topic 10
Pulling private images in Kubernetes
This chapter assumes you can push an image to GHCR or ECR, and that you know what a Pod and a namespace are. It covers how a cluster gets credentials to pull from a private registry.
Who pulls the image
The kubelet, the agent running on each node, pulls images. Your kubectl login and your laptop's docker login play no part. The cluster needs its own credentials, and it gets them in one of two ways:
- An image pull secret that you create and attach to a Pod or service account. This works with any registry.
- Node-level identity. On Amazon EKS, the node's IAM role can authenticate to ECR without a secret.
Pulling from GHCR with an image pull secret
Create a GitHub personal access token (classic) with only the read:packages scope. Do not reuse a token that can push. Then create the secret in the same namespace as the Pod:
kubectl create secret docker-registry ghcr-pull \
--docker-server=ghcr.io \
--docker-username=YOUR_GITHUB_USERNAME \
--docker-password="$CR_PAT"Reference the secret from the Pod:
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
imagePullSecrets:
- name: ghcr-pull
containers:
- name: my-app
image: ghcr.io/your-github-username/my-app:1.0.0The secret has type kubernetes.io/dockerconfigjson. It stores the same JSON that docker login writes to ~/.docker/config.json, keyed by registry host. The kubelet picks the entry whose host matches the image reference.
Attaching the secret to a service account
Listing imagePullSecrets in every Pod is easy to forget. Add the secret to the namespace's service account instead:
kubectl patch serviceaccount default \
-p '{"imagePullSecrets": [{"name": "ghcr-pull"}]}'Pods that use that service account then get the secret automatically.
Pulling from ECR
You have two options on EKS.
- Node IAM role (preferred on EKS). The worker node role needs ECR read permissions, such as the AWS-managed
AmazonEC2ContainerRegistryReadOnlypolicy. No secret is needed, and the kubelet handles token refresh. - Pull secret. This is necessary outside EKS. ECR passwords expire after 12 hours, so a secret made from
aws ecr get-login-passwordstops working within a day. If you must use one, refresh it on a schedule, for example with a CronJob.
A static ECR secret is the most common cause of pods that ran fine yesterday and fail today.
Pinning to a digest
Tags can move. In the Pod spec, reference the digest to guarantee which bytes run:
image: ghcr.io/your-github-username/my-app@sha256:<digest>Replace <digest> with the real value printed by docker push or shown in the registry.
Diagnosing a failed pull
Run kubectl describe pod my-app and read the Events section.
| Status | Likely cause |
|---|---|
| `ErrImagePull` | The first attempt failed. Read the event message for the reason. |
| `ImagePullBackOff` | Kubernetes is retrying with growing delays after failures. |
| `unauthorized` or `denied` | Missing, wrong-namespace, or expired secret, or a token without `read:packages`. |
| `not found` | Typo in the host, repository, or tag. GHCR paths must be lowercase. |
Exercise
A Pod in the staging namespace pulls an ECR image through a pull secret. It worked yesterday and now shows ImagePullBackOff with an "authorization token has expired" message. What happened, and what is a more durable fix on EKS?
Show answer
ECR passwords from get-login-password last 12 hours, so the secret expired. On EKS, remove the secret and give the node IAM role ECR read permissions, such as AmazonEC2ContainerRegistryReadOnly. The kubelet then fetches fresh tokens itself.