Sub topic 12
Loading locally built images into the cluster
This chapter assumes you have a running kind or minikube cluster, can apply a Deployment manifest, and have Docker installed. If you need a refresher, see the earlier chapters on creating clusters and on kubectl apply.
Why a local image fails to start
When you run docker build, the image is stored in your host's Docker image store. A kind node is a container with its own container runtime, and a minikube node has its own image store as well (unless you use the Docker driver, as covered below). Neither can see the image you just built.
If a pod references myapp:dev, the node tries to pull it from a registry. The pull fails and the pod shows ErrImagePull or ImagePullBackOff.
Load the image
Build the image, then copy it into the cluster nodes.
docker build -t myapp:dev .
kind load docker-image myapp:dev --name dev
# minikube equivalent:
minikube image load myapp:dev -p devkind load copies the image into every node of the cluster named by --name. minikube image load uses the profile given by -p. Without these flags, each tool targets its default cluster, which is named kind for kind and minikube for minikube. Because the example cluster is named dev, you must pass the name.
Use the image in a manifest
Reference the image by the same name and tag. Set imagePullPolicy so Kubernetes does not try to pull it.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:dev
imagePullPolicy: IfNotPresentCommon errors
- The tag is
latestor missing. Kubernetes defaultsimagePullPolicytoAlwaysfor thelatesttag, and the pull fails. Use an explicit tag such asdev, or setimagePullPolicyyourself. - Wrong cluster name. Loading into
kindwhen your cluster isdevsucceeds or fails against a different cluster than the one you are using. Check withkubectl config current-context. - Rebuilt image, same tag. Loading again replaces the image in the node, but running pods keep the old one. Restart them with
kubectl rollout restart deployment/myapp. - Image not found after loading. Confirm the name and tag match exactly, including any registry prefix.
Check what the node has
For kind, list images inside a node container. The node name is the cluster name plus -control-plane.
docker exec -it dev-control-plane crictl imagesFor minikube, run:
minikube image ls -p devExercise
A pod using myapp:dev shows ImagePullBackOff right after you ran docker build -t myapp:dev .. What is the cause, and which commands fix it for a kind cluster named dev?
Show answer
The image exists only in your host's Docker store, so the node tries to pull it from a registry and fails. Run kind load docker-image myapp:dev --name dev. If the Deployment already exists, then run kubectl rollout restart deployment/myapp so new pods start with the loaded image.
Exercise
You rebuilt myapp:dev and loaded it again, but the running pod still behaves as before. Why, and what do you do?
Show answer
Existing pods keep the image they started with. Run kubectl rollout restart deployment/myapp to create new pods that use the updated image.