【问题标题】:Could not get token Access Denied with AWS IAM Authenticator无法使用 AWS IAM Authenticator 获取令牌访问被拒绝
【发布时间】:2019-05-15 05:52:12
【问题描述】:

我正在尝试为我的 k8s 集群设置 AWS IAM Authenticator。我有两个 AWS 账户:A 和 B。

k8s账户运行在B账户中。

我在 A 帐户中创建了以下资源:

政策

Description: Grants permissions to assume the kubernetes-admin role
Policy:
  Statement:
    - Action: sts:*
      Effect: Allow
      Resource: arn:aws:iam::<AccountID-B>:role/kubernetes-admin
      Sid: KubernetesAdmin
  Version: 2012-10-17

该策略与一个组相关联,我将我的 IAM 用户添加到该组。

在 B 帐户中我创建了以下角色:

AssumeRolePolicyDocument:
  Statement:
    - Action: sts:AssumeRole
      Effect: Allow
      Principal:
        AWS: arn:aws:iam::<AccountID-A>:root
  Version: 2012-10-17

这是用于配置 aws-iam-authenticator 的 ConfigMap

apiVersion: v1
data:
  config.yaml: |
    # a unique-per-cluster identifier to prevent replay attacks
    # (good choices are a random token or a domain name that will be unique to your cluster)
    clusterID: k8s.mycluster.net
    server:
      # each mapRoles entry maps an IAM role to a username and set of groups
      # Each username and group can optionally contain template parameters:
      # "{{AccountID}}" is the 12 digit AWS ID.
      # "{{SessionName}}" is the role session name.
      mapRoles:
      - roleARN: arn:aws:iam::<AccountID-B>:role/kubernetes-admin
        username: kubernetes-admin:{{AccountID}}:{{SessionName}}
        groups:
        - system:masters
kind: ConfigMap
metadata:
  creationTimestamp: 2018-12-13T19:41:39Z
  labels:
    k8s-app: aws-iam-authenticator
  name: aws-iam-authenticator
  namespace: kube-system
  resourceVersion: "87401"
  selfLink: /api/v1/namespaces/kube-system/configmaps/aws-iam-authenticator
  uid: 1bc39653-ff0f-11e8-a580-02b4590539ba

kubeconfig 是:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: <certificate>
    server: https://api.k8s.mycluster.net
  name: k8s.mycluster.net
contexts:
- context:
    cluster: k8s.mycluster.net
    namespace: kube-system
    user: k8s.mycluster.net
  name: k8s.mycluster.net
current-context: k8s.mycluster.net
kind: Config
preferences: {}
users:
- name: k8s.mycluster.net
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      command: aws-iam-authenticator
      env:
      - name: "AWS_PROFILE"
        value: "myaccount"
      args:
        - "token"
        - "-i"
        - "k8s.mycluster.net"
        - "-r"
        - "arn:aws:iam::<AccountID-B>:role/kubernetes-admin"

结果是:

could not get token: AccessDenied: Access denied
    status code: 403, request id: 6ceac161-ff2f-11e8-b263-2b0e32831969
Unable to connect to the server: getting token: exec: exit status 1

有什么想法吗?我不明白我错过了什么。

【问题讨论】:

  • 您使用什么来配置集群?您需要设置 Kubernetes API 服务器以包含 aws-iam-authenticator 作为身份验证方法。
  • 我已经解决了这个问题。我会回复解决方案
  • 请发布解决方案,以便对其他人有所帮助。

标签: amazon-web-services kubernetes amazon-iam


【解决方案1】:

添加到这一点 - 我的解决方案是执行以下操作:

在 ~/.kube 目录中:

aws eks update-kubeconfig --name eks-dev-cluster --role-arn=XXXXXXXXXXXX

这会创建一个文件 config-my-eks-cluster

vi config-my-eks-cluster

注释掉上面提到的两行:

  apiVersion: client.authentication.k8s.io/v1alpha1
  args:
  - token
  - -i
  - eks-dev-cluster
  #- -r
  #- arn:aws:iam::XXXXXXXXX:role/eks-dev-role (the role you made for eks)
  command: aws-iam-authenticator

然后确保您导出您的用户个人资料:

export AWS_PROFILE=XXXXXXXXX(您在控制台或通过cli创建集群时使用的用户)

运行:

kubectl get svc --v=10

这会将输出置于详细模式,并为您提供有关任何错误的详细信息。

【讨论】:

    【解决方案2】:

    使其正常工作的方法是删除

    - "-r"
    - "arn:aws:iam::<AccountID-B>:role/kubernetes-admin" 
    

    并将要承担的角色传递给AWS_PROFILE env var

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 2021-10-02
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-20
      相关资源
      最近更新 更多