aws eks 配置授权额外的用户和角色访问集群
参考资料
- https://github.com/kubernetes-sigs/aws-iam-authenticator#full-configuration-format
- https://docs.amazonaws.cn/zh_cn/eks/latest/userguide/add-user-role.html
众所周知,aws eks使用 Authenticator 或者 aws 命令来进行账户级别的用户和角色的授权。
kubernetes和aws服务的认证是通过控制平面的webhook来进行转换的。对于x509认证的集群,有如下对应信息
- CN(common name):对应集群中的user
- Organization:对应集群中的group
对于新建的eks集群,则只有创建集群的用户有权限访问集群。之后如果需要将额外的用户或角色授权访问eks集群,需要进行额外配置
需要注意
- 如果新建的集群,并且没有节点组,则不会创建名为
aws-auth
的configmap
查看eksctl的相关参数,可以分别对account和arn进行配置
Usage: eksctl create iamidentitymapping [flags]IAMIdentityMapping flags:--account string Account ID to automatically map to its username--arn string ARN of the IAM role or user to create--username string User name within Kubernetes to map to IAM role--group strings Groups within Kubernetes to which IAM role is mapped--service-name string Service name; valid value: emr-containers--namespace string Namespace in which to create RBAC resources--no-duplicate-arns Throw error when an aws-auth record already exists
节点角色
在configmap中有如下配置,表明只有节点实例角色才有权访问集群
- 在创建新的节点组并指定新的节点角色时,该角色会自动写入configmap
mapRoles: |
- groups:- system:bootstrappers- system:nodesrolearn: arn:aws:iam::111122223333:role/my-node-roleusername: system:node:{{EC2PrivateDNSName}}
特定用户
可以使用eksctl配置用户加入集群,或者手动配置。这里授予admin权限,可以按需修改
eksctl create iamidentitymapping \--cluster testcluster \--region=cn-north-1 \--arn arn:aws-cn:iam::111122223333:user/test-user \--group system:masters \--no-duplicate-arns
在configmap中有如下配置,表明对应用户能够使用集群中user和group的权限
mapRoles: | - groups: - system:masters rolearn: arn:aws-cn:iam::111122223333:role/test-role
特定角色
可以使用eksctl配置角色加入集群,或者手动配置。这里授予admin权限,可以按需修改
eksctl create iamidentitymapping \--cluster testcluster \--region=cn-north-1 \--arn arn:aws-cn:iam::111122223333:role/service-role/test-role \--group system:masters \--no-duplicate-arns
在configmap中有如下配置,表明对应角色能够使用集群中user和group的权限
mapUsers: | - groups: - system:masters userarn: arn:aws-cn:iam::111122223333:user/test-user
特定账户
可以使用eksctl配置账户映射,根据会将账户级别资源映射为指定的username
- 这里只能配置账户id,不能指定group和username字段,否则会报错
Error: account cannot be configured with any other options
eksctl create iamidentitymapping \--cluster test124 \--account 442337510176 \--region=cn-north-1 \--username test
在configmap中有如下配置
mapAccounts: | - "442337510176"
由于该字段并未指定user和group,因此只是将该账户下的user和role映射为集群中可以识别的主体,但是并没有授予相应的权限。之后如果需要对该账户授权,需要手动创建clusterrolebinding
。例如以下
# read权限
kubectl create clusterrolebinding test-account-role --clusterrole=view --user="arn:aws-cn:iam::111122223333:role/111" --group=test-group
# 为所有鉴权主体授权admin权限
kubectl create clusterrolebinding account-admin --clusterrole=cluster-admin --group=system:authenticated
这里和前3种方式相比特殊的地方在于,这里无法通过aws命令更新kubeconfig凭证。
可以通过手动生成带profile的凭证之后,再修改到目标profile
apiVersion: v1
clusters:
- cluster:certificate-authority-data: dxxxxxxxxxxxxxxCg==server: https://C96x9B.yl4.cn-north-1.eks.amazonaws.com.cnname: arn:aws-cn:eks:cn-north-1:111122223333:cluster/test-cluster
contexts:
- context:cluster: arn:aws-cn:eks:cn-north-1:111122223333:cluster/test-clusteruser: arn:aws-cn:eks:cn-north-1:111122223333:cluster/test-clustername: arn:aws-cn:eks:cn-north-1:111122223333:cluster/test-cluster
current-context: arn:aws-cn:eks:cn-north-1:111122223333:cluster/test-cluster
kind: Config
preferences: {}
users:
- name: arn:aws-cn:eks:cn-north-1:111122223333:cluster/test-clusteruser:exec:apiVersion: client.authentication.k8s.io/v1beta1args:- --region- cn-north-1- eks- get-token- --cluster-name- test124command: awsenv:- name: AWS_PROFILEvalue: test-account