Lab 03 · An image that never arrives
This lab runs on the kind cluster from Build a Lab Cluster on Your Laptop. The nodes need internet access to pull from Docker Hub. Check kubectl config current-context first: it should say kind-lab.
Goal
Two Deployments, web and api, never start a container. Read the pull errors, tell the two causes apart, and get both running.
Setup
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Namespace
metadata:
name: lab-03
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: lab-03
spec:
replicas: 1
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx:1.27-alpnie
---
# The team's private mirror of nginx
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
namespace: lab-03
spec:
replicas: 1
selector:
matchLabels:
app: api
template:
metadata:
labels:
app: api
spec:
containers:
- name: api
image: registry.lab.invalid/mirror/nginx:1.27
EOF
Symptoms
kubectl -n lab-03 get pods
NAME READY STATUS RESTARTS AGE
api-5f7d9b8c6-wq4lz 0/1 ImagePullBackOff 0 50s
web-6c8f7d5b9-h2kxn 0/1 ErrImagePull 0 50s
Run it a few times. Each Pod switches between ErrImagePull (a pull just failed) and ImagePullBackOff (the kubelet is waiting before the next try, with a longer wait each time).
Investigate
kubectl -n lab-03 describe pod -l app=web # Events: the full pull error
kubectl -n lab-03 describe pod -l app=api
kubectl -n lab-03 get events --sort-by=.lastTimestamp
kubectl -n lab-03 get deploy -o wide # The IMAGES column
The status is the same for both Pods. The difference is in the last words of the Failed to pull image event.
Hints
Hint 1
Pull errors fall into three groups. not found: the registry answered, but that name or tag does not exist. unauthorized / access denied: the registry wants credentials. no such host / timeout: the node cannot reach the registry at all. Which group is each Pod in?
Hint 2
For web, read the tag letter by letter. The official nginx image has -alpine tags. A short name like nginx expands to docker.io/library/nginx.
Hint 3
For api, the error ends with no such host. Would an image pull Secret change that? Credentials only matter once the node can connect to the registry.
Solution
Solution
Problem 1: a tag typo. The web event says the reference cannot be resolved and ends with not found, for example:
Warning Failed ... Failed to pull image "nginx:1.27-alpnie": ... docker.io/library/nginx:1.27-alpnie: not found
The registry was reachable, but 1.27-alpnie is not a tag. Use the real one:
kubectl -n lab-03 set image deployment/web nginx=nginx:1.27-alpine
Problem 2: a private registry the node cannot use. The api event ends with a DNS error, ... lookup registry.lab.invalid ...: no such host. The .invalid domain never resolves, which stands in for a private registry that is misspelled or only reachable from another network.
With a real private registry that the node can reach, the usual error is an authorization error (401 Unauthorized, authorization failed or access denied). The fix for that is a pull Secret referenced from the Pod spec:
# For a real private registry (not needed to finish this lab)
kubectl -n lab-03 create secret docker-registry regcred \
--docker-server=registry.example.com --docker-username=<user> --docker-password=<token>
kubectl -n lab-03 patch deployment api \
-p '{"spec":{"template":{"spec":{"imagePullSecrets":[{"name":"regcred"}]}}}}'
Here a Secret would not help, because the node never got as far as the login. The mirror is gone, so point api back at the public image:
kubectl -n lab-03 set image deployment/api api=nginx:1.27
Careful: Docker Hub answers a pull of a repository that does not exist with an authorization error, not not found. So an "unauthorized" error can also mean a misspelled image name.
Verify
kubectl -n lab-03 rollout status deployment web
kubectl -n lab-03 rollout status deployment api
kubectl -n lab-03 get pods
NAME READY STATUS RESTARTS AGE
api-7b9c5d6f8-p8vjs 1/1 Running 0 25s
web-58d7c4f9b6-xr5tm 1/1 Running 0 40s
Both Pods are Running and 1/1. The describe events now show Pulled and Started.
Clean up
kubectl delete ns lab-03