解决 Kubernetes Pod 报 ImagePullBackOff 且提示 authorization failed
分析 Kubernetes 拉取私有镜像报 authorization failed 诱因,详解 imagePullSecrets 凭据创建与 ServiceAccount 绑定。
问题现象
在 Kubernetes 集群部署私有仓库镜像时,Pod 卡死在拉取阶段:
NAME READY STATUS RESTARTS AGE
web-app-12345678-abcde 0/1 ImagePullBackOff 0 2m
查看 kubectl describe pod 报错:
Failed to pull image "registry.example.com/proj/app:v1.0": rpc error: code = Unknown desc =
failed to pull and unpack image ... authorization failed
触发根因
- 私有镜像仓库未配置凭据:镜像保存在私有 Harbor、阿里云 ACR 或 Docker Hub 私有库,但 Pod 未关联包含认证 Token 的
imagePullSecrets。 - Secret 命名空间隔离:
docker-registry类型的 Secret 被创建在default命名空间,而 Deployment 运行在其他命名空间,无法跨空间引用。 - 密码过期或具备只读权限的 Token 失效。
诊断排查步骤
检查 Pod 所在命名空间是否有镜像拉取凭证:
kubectl get secret -n <namespace>
解决方案
方案一:在目标命名空间创建 docker-registry Secret
kubectl create secret docker-registry harbor-secret --docker-server=registry.example.com --docker-username=myuser --docker-password=mypassword --docker-email=myuser@example.com -n <namespace>
方案二:在 Deployment YAML 中显式声明 imagePullSecrets
spec:
template:
spec:
imagePullSecrets:
- name: harbor-secret
containers:
- name: web-app
image: registry.example.com/proj/app:v1.0
方案三:为默认 ServiceAccount 统一绑定镜像凭证(免去每个 YAML 手写)
kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "harbor-secret"}]}' -n <namespace>
验证方法
再次查看 Pod 状态:
kubectl get pods -w
状态由 ImagePullBackOff 顺利变为 Running,镜像拉取成功。


