如何疑難排解 Amazon EKS 的 Amazon ECR 問題?

3 分的閱讀內容
0

使用 Amazon Elastic Kubernetes Service (Amazon EKS) 時,我無法從 Amazon Elastic Container Registry (Amazon ECR) 提取映像檔。

簡短說明

因為下列其中一個原因,您無法從 Amazon ECR 提取映像檔:

  • 您無法與 Amazon ECR 端點通訊。
  • 您在工作節點的節點執行個體角色中沒有適當權限。
  • 您尚未建立介面 VPC 端點。

若要解決這些問題,請根據您的使用案例使用下列一或多節的解決方法。

解決方法

疑難排解工作節點和 Amazon ECR 端點之間的通訊問題

如果您的工作節點無法與 Amazon ECR 端點通訊,您會收到下列錯誤訊息:

Failed to pull image "ACCOUNT.dkr.ecr.REGION.amazonaws.com/imagename:tag": rpc error: code = Unknown desc =
Error response from daemon: Get https://ACCOUNT.dkr.ecr.REGION.amazonaws.com/v2/: net/http:
request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

若要解決此錯誤,請確認下列項目:

  • 工作節點的子網路具有到網際網路的路由。檢查與子網路相關聯的路由表
  • 與工作節點相關聯的安全群組允許輸出網際網路流量。
  • 網路存取控制清單 (ACL) 的輸入和輸出規則允許存取網際網路。

更新工作節點的執行個體 IAM 角色

假設工作節點的執行個體 AWS 身分和存取管理 (IAM) 角色沒有從 Amazon ECR 提取映像檔所需的權限。然後,您會從 Amazon EKS Pod 收到下列錯誤:

Warning  Failed     14s (x2 over 28s)  kubelet, ip-000-000-000-000.us-west-2.compute.internalFailed to pull image "ACCOUNT.dkr.ecr.REGION.amazonaws.com/imagename:tag": rpc error: code = Unknown desc = Error response from daemon: Get https://ACCOUNT.dkr.ecr.REGION.amazonaws.com/v2/imagename/manifests/tag: no basic auth credentials
Warning  Failed     14s (x2 over 28s)  kubelet, ip-000-000-000-000.us-west-2.compute.internal  Error: ErrImagePull
Normal   BackOff    2s (x2 over 28s)   kubelet, ip-000-000-000-000.us-west-2.compute.internal  Back-off pulling image "ACCOUNT.dkr.ecr.REGION.amazonaws.com/imagename:tag"
Warning  Failed     2s (x2 over 28s)   kubelet, ip-000-000-000-000.us-west-2.compute.internal  Error: ImagePullBackOff

若要解決此錯誤,請確認您的工作節點使用 AmazonEC2ContainerRegistryReadOnly AWS 身分和存取管理 (IAM) 受管原則。或者,使用下列 IAM 權限更新工作節點的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體設定檔

"ecr:GetAuthorizationToken",
"ecr:BatchCheckLayerAvailability",
"ecr:GetDownloadUrlForLayer",
"ecr:GetRepositoryPolicy",
"ecr:DescribeRepositories",
"ecr:ListImages",
"ecr:DescribeImages",
"ecr:BatchGetImage",
"ecr:GetLifecyclePolicy",
"ecr:GetLifecyclePolicyPreview",
"ecr:ListTagsForResource",
"ecr:DescribeImageScanFindings"

**重要事項:**最佳實務是使用 AmazonEC2ContainerRegistryReadOnly 原則,而不是建立重複原則。

更新後的執行個體 IAM 角色會向工作節點授予存取 Amazon ECR 並透過 kubelet 提取映像檔的權限。kubelet 負責擷取和定期重新整理 Amazon ECR 憑證。如需詳細資訊,請參閱 Kubernetes 映像檔 (來自 Kubernetes 網站)。

確認您的儲存器原則正確無誤

儲存器原則是 IAM 原則的子集,可控制對個別 Amazon ECR 儲存器的存取權限。IAM 原則通常用於套用整個 Amazon ECR 服務的權限,但也可以控制對特定資源的存取權限。

1.    開啟主要帳戶的 Amazon ECR 主控台

2.    瀏覽至包含 ECR 儲存器的 AWS 區域。

3.    在瀏覽窗格中,選擇儲存器,然後選擇您想檢查的儲存器。

4.    在瀏覽窗格中,選擇權限,然後檢查您的儲存器是否有正確權限。

此範例原則允許特定 IAM 使用者說明儲存器和儲存器內的映像檔:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ECR Repository Policy",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:user/MyUsername"
      },
      "Action": [
        "ecr:DescribeImages",
        "ecr:DescribeRepositories"
      ]
    }
  ]
}

如果您的 EKS 位於不同的 AWS 帳戶,請確認您的儲存器原則允許存取

如果您無法存取另一個 AWS 帳戶中的容器映像檔,kubelet 即會失敗,並出現下列錯誤:

Failed to pull image "cross-aws-account-id:.dkr.ecr.REGION.amazonaws.com/repo-name:image-tag": rpc error: code = Unknown desc = Error response from daemon: pull access denied for arn:aws:ecr:REGION:cross-aws-account-id:repository/repo-name, repository does not exist or may require 'docker login': denied: User: arn:aws:sts::<aws-account-containing-eks-cluster>:assumed-role/<node-instance-role-for-worker-node is not authorized to perform: ecr:BatchGetImage on resource: arn:aws:ecr:REGION:cross-aws-account-id:repository/repo-name

下列範例原則允許一個 AWS 帳戶中的執行個體 IAM 角色,說明和提取另一個 AWS 帳戶中 ECR 儲存器的容器映像檔:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::123456789012:role/eksctl-cross-account-ecr-access-n-NodeInstanceRole"
      },
      "Action": [
        "ecr:GetAuthorizationToken",
        "ecr:BatchCheckLayerAvailability",
        "ecr:GetDownloadUrlForLayer",
        "ecr:GetRepositoryPolicy",
        "ecr:DescribeRepositories",
        "ecr:ListImages",
        "ecr:DescribeImages",
        "ecr:BatchGetImage",
        "ecr:GetLifecyclePolicy",
        "ecr:GetLifecyclePolicyPreview",
        "ecr:ListTagsForResource",
        "ecr:DescribeImageScanFindings"
      ],
      "Resource": "*"
    }
  ]
}

注意: 使用 ECR 原則中的執行個體 IAM 角色 ARN,而不是執行個體設定檔 ARN。

建立介面 VPC 端點

若要從 Amazon ECR 提取映像檔,您必須設定介面 VPC 端點。請參閱 Amazon ECR 介面 VPC 端點 (AWS PrivateLink)建立 Amazon ECS 的 VPC 端點區段。

確認您的 Fargate Pod 執行角色設定正確

當您從 Amazon 託管儲存器擷取映像檔時,如果您的 Fargate CoreDNS Pod 卡在 ImagePullBackOff 狀態,您就會收到下列錯誤訊息:

Warning   Failed           27s (x2 over 40s)  kubelet            Failed to pull image "151284513677.dkr.ecr.eu-central-1.amazonaws.com/coredns:latest ": rpc error: code = Unknown desc = failed to pull and unpack image "151284513677.dkr.ecr.eu-central-1.amazonaws.com/coredns:latest ": failed to resolve reference "151284513677.dkr.ecr.eu-central-1.amazonaws.com/coredns:latest ": pulling from host 151284513677.dkr.ecr.eu-central-1.amazonaws.com failed with status code [manifests latest]: 401 Unauthorized

若要疑難排解此錯誤,請務必將 Fargate Pod 執行角色設為使用 AmazonEKSFargatePodExecutionRolePolicy。請確定與下列項目類似的信任原則也已附加至角色:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Condition": {
        "ArnLike": {
          "aws:SourceArn": "arn:aws:eks:example-region:1111222233334444:fargateprofile/example-cluster/*"
        }
      },
      "Principal": {
        "Service": "eks-fargate-pods.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

注意:

記得在原則中取代下列項目:

  • 用您的 AWS 區域名稱取代 example-region
  • 用帳戶號碼取代 1111222233334444
  • 用您的叢集名稱取代 example-cluster
AWS 官方
AWS 官方已更新 1 年前