首页 > 解决方案 > 如何将私有容器从 AWS ECR 拉到本地集群

问题描述

我目前在尝试拉取通过 AWS ECR 托管的远程 docker 映像时遇到问题。运行部署时出现此错误

步骤1)

aws ecr get-login-password --region cn-north-1 | docker login --username AWS --password-stdin xxxxxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn

第2步)

kubectl create -f backend.yaml

从这里发生以下情况:

➜  backend git:(kubernetes-fresh) ✗ kubectl get pods
NAME                      READY   STATUS             RESTARTS   AGE
backend-89d75f7df-qwqdq   0/1     Pending            0          2s

➜  backend git:(kubernetes-fresh) ✗ kubectl get pods
NAME                      READY   STATUS              RESTARTS   AGE
backend-89d75f7df-qwqdq   0/1     ContainerCreating   0          4s

➜  backend git:(kubernetes-fresh) ✗ kubectl get pods
NAME                      READY   STATUS             RESTARTS   AGE
backend-89d75f7df-qwqdq   0/1     ErrImagePull       0          6s

➜  backend git:(kubernetes-fresh) ✗ kubectl get pods
NAME                      READY   STATUS             RESTARTS   AGE
backend-89d75f7df-qwqdq   0/1     ImagePullBackOff   0          7s

然后我运行kubectl describe pod backend它会输出:

Events:
  Type     Reason     Age                 From               Message
  ----     ------     ----                ----               -------
  Normal   Scheduled  117s                default-scheduler  Successfully assigned default/backend-89d75f7df-qwqdq to minikube
  Normal   Pulling    32s (x4 over 114s)  kubelet, minikube  Pulling image "xxxxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/baopals:latest"
  Warning  Failed     31s (x4 over 114s)  kubelet, minikube  Failed to pull image "xxxxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/baopals:latest": rpc error: code = Unknown desc = Error response from daemon: Get https://xxxxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/v2/baopals/manifests/latest: no basic auth credentials
  Warning  Failed     31s (x4 over 114s)  kubelet, minikube  Error: ErrImagePull
  Warning  Failed     19s (x6 over 113s)  kubelet, minikube  Error: ImagePullBackOff
  Normal   BackOff    4s (x7 over 113s)   kubelet, minikube  Back-off pulling image "xxxxxxxxx.dkr.ecr.cn-north-1.amazonaws.com.cn/baopals:latest"

主要错误是no basic auth credentials

现在我感到困惑的是,我可以很好地将图像推送到我的 ECR,我也可以推送到我的远程 EKS 集群我觉得基本上我现在唯一不能做的就是从我托管在 ECR 上的私有存储库中提取。

是否有一些明显的东西我在这里遗漏了阻止我从私人回购中提取,以便我可以在我的本地机器上使用它们?

标签: amazon-web-servicesdockerkubernetes

解决方案


要在本地获取 ECR 映像,您必须登录到 ECR 并获取 docker 映像。而如果您在 Kubernetes 上,则必须使用secret它来存储 ECR 登录详细信息,并每次都使用它从 ECR 中提取图像。

如果您在 Kubernetes 上,这里的 shell 脚本会自动从 AWS 配置中获取值,否则您可以在脚本开始时更新变量。

ACCOUNT=$(aws sts get-caller-identity --query 'Account' --output text) #aws account number
REGION=ap-south-1                                     #aws ECR region
SECRET_NAME=${REGION}-ecr-registry                    #secret_name
EMAIL=abc@xyz.com                                     #can be anything

TOKEN=`aws ecr --region=$REGION get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`

kubectl delete secret --ignore-not-found $SECRET_NAME
kubectl create secret docker-registry $SECRET_NAME \
 --docker-server=https://$ACCOUNT.dkr.ecr.${REGION}.amazonaws.com \
 --docker-username=AWS \
 --docker-password="${TOKEN}" \
 --docker-email="${EMAIL}"

imagePullSecret在 YAML 文件中用于提取私有 Docker 存储库的存储秘密。

https://github.com/harsh4870/ECR-Token-automation/blob/master/aws-token.sh


推荐阅读