在 CodeBuild 阶段 运行 kubectl apply -f hello-k8s.yml 时出现 "Unable to recognize \"hello-k8s.yml\": Unauthorized" 错误

Getting "Unable to recognize \"hello-k8s.yml\": Unauthorized" error when running kubectl apply -f hello-k8s.yml in CodeBuild phase

我是 Kubernetes 新手,正在尝试创建 AWS CodePipeline 以将服务部署到 EKS 堆栈。

我正在关注 this 教程 我已完成所有步骤,包括创建角色和添加权限,以便 CodeBuild 能够与 EKS 通信。

我现在面临的问题是,当 CodePipeline 运行时,CodeBuild 阶段的以下命令失败。

kubectl apply -f hello-k8s.yml

并给出这个错误

[Container] 2019/12/04 07:41:43 Running command kubectl apply -f hello-k8s.yml 
unable to recognize "hello-k8s.yml": Unauthorized 
unable to recognize "hello-k8s.yml": Unauthorized 

我不太确定这是否是凭据问题,因为我已经按照教程使用了所有步骤来添加 user/role。

谁能帮我解决这个问题?

从 CodeBuild 将 Yaml 清单部署到 Kubernetes 需要执行以下步骤:

高级流程包括以下步骤:

  1. 为 CodeBuild 创建 IAM 服务角色

  2. 使用“aws-auth”ConfigMap

  3. 映射 EKS 中的 CodeBuild 服务角色
  4. 在代码存储库中创建源文件

  5. 创建并启动 CodeBuild 项目

  6. 确认在 EKS 集群中创建了所需的对象

为 CodeBuild 创建 IAM 服务角色(不要使用现有服务角色,因为它包含“/路径/”)

运行 使用以下命令创建 CodeBuild 服务角色并附加所需的策略:

TRUST = "{   \"Version\": \"2012-10-17\",   \"Statement\": [     {       \"Effect\": \"Allow\",       \"Principal\": {         \"Service\": \"codebuild.amazonaws.com\"       },       \"Action\": \"sts:AssumeRole\"     }   ] }"

$ echo '{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "eks:Describe*", "Resource": "*" } ] }' > /tmp/iam-role-policy

$ aws iam create-role --role-name CodeBuildKubectlRole --assume-role-policy-document "$TRUST" --output text --query 'Role.Arn'

$ aws iam put-role-policy --role-name CodeBuildKubectlRole --policy-name eks-describe --policy-document file:///tmp/iam-role-policy

$ aws iam attach-role-policy --role-name CodeBuildKubectlRole --policy-arn arn:aws:iam::aws:policy/CloudWatchLogsFullAccess

$ aws iam attach-role-policy --role-name CodeBuildKubectlRole --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildAdminAccess

使用“aws-auth”ConfigMap 在 EKS 中映射 CodeBuild 服务角色

编辑“aws-auth”ConfigMap 并为 CodeBuild 服务角色添加角色映射:

$ vi aws-auth.yaml



apiVersion: v1

kind: ConfigMap

metadata:

  name: aws-auth

  namespace: kube-system

data:

  mapRoles: |

    - rolearn:  arn:aws:iam::AccountId:role/devel-worker-nodes-NodeInstanceRole-14W1I3VCZQHU7

      username: system:node:{{EC2PrivateDNSName}}

      groups:

        - system:bootstrappers

        - system:nodes

    - rolearn: arn:aws:iam::AccountId:role/CodeBuildKubectlRole

      username: build

      groups:

        - system:masters


$ kubectl apply -f aws-auth.yaml

在代码存储库中创建源文件

使用示例文件在 Github/CodeCommit 中创建存储库,如下所示:

.
├── buildspec.yml
└── deployment
   └── pod.yaml

示例存储库位于此处:https://github.com/shariqmus/codebuild-to-eks

备注:

  • buildspec.yml文件在CodeBuild环境中安装kubectl、aws-iam-authenticator并配置kubectl

  • 用正确的区域和第 16 行的 cluster_name 更新 buildspec.yml 文件

  • 在“部署”目录添加部署YAML文件

创建并启动构建项目

  1. 打开 CodeBuild 控制台

  2. 单击“创建构建项目”按钮

  3. 为项目命名

  4. 使用您已添加附件的 CodeCommit 存储库:“buildspec.yml”和“pod.yaml”

  5. 使用托管映像 > Ubuntu > 标准 1.0

  6. 在角色名称中,select“CodeBuildKubectlRole”

  7. 单击“创建构建项目”按钮

  8. 创建“开始构建”按钮以开始构建

确认在 EKS 集群中创建了所需的对象

您可以通过一个简单的命令来确认这一点,例如

$ kubectl get all --all-namespaces