Scenario
A Deployment named backend in the dev namespace is failing to start. Pods are stuck in ImagePullBackOff due to configuration issues.
Task
Fix the Deployment so pods successfully pull the image and enter the Running state.
Image Information
| Property |
Value |
| Registry |
ghcr.io |
| Repository |
prepare-sh/alpine |
| Version |
3.23.2 |
| Architecture |
amd64 |
| Size |
8.44 MB |
| Username |
preparesh-bot |
| Access Token |
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 |
Step 1: Verify Pod status and identify the error
kubectl describe pod -n dev -l app=backend
Observe status is ImagePullBackOff or ErrImagePull. Events show "Failed to pull image" due to incorrect image name or missing credentials.
Step 2: Fix the image name
kubectl set image deployment/backend alpine=ghcr.io/prepare-sh/alpine:3.23.2 -n dev
Updates the Deployment with the correct image ghcr.io/prepare-sh/alpine:3.23.2.
Step 3: Create the registry secret
kubectl create secret docker-registry regcred \
--docker-server=ghcr.io \
--docker-username=preparesh-bot \
--docker-password=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 \
-n dev
Creates a docker-registry secret for GitHub Container Registry authentication.
Step 4: Configure ServiceAccount with the secret
kubectl patch serviceaccount default -n dev -p '{"imagePullSecrets": [{"name": "regcred"}]}'
Patches the default ServiceAccount to include regcred, applying credentials to all Pods using that account.
Step 5: Restart Pods to apply changes
kubectl delete pod -n dev -l app=backend
Deletes existing Pods. The Deployment recreates them with the new ServiceAccount configuration.
Step 6: Verify Pods are Running
kubectl get pods -n dev -w
Watch as Pods transition from Pending → ContainerCreating → Running. Press Ctrl+C to exit.