
我正在嘗試學習 AWS EKS 並關注Amazon EKS 入門 – AWS 管理主控台和 AWS CLI逐步引導,除了我已更改為 us-east-1 的區域。
我能夠建立叢集 -my-cluster
但是當我嘗試配置我的電腦(EC2 實例 (T2.Micro))以與叢集通訊時,出現error: You must be logged in to the server (Unauthorized)
錯誤。經過深入研究,我發現 EC2 角色 (ARN - arn:aws:iam::123456789012:role/ec2-admin) 需要承擔創建群集的角色 (ARN - arn:aws:iam::123456789012:role /myAmazonEKSClusterRole)。我還透過修改myAmazonEKSClusterRole
獲取中的信任關係在角色定義中進行了這些修改arn:aws:sts::123456789012:assumed-role/myAmazonEKSClusterRole/test-session
,在此之後,我能夠執行aws eks
命令,但kubectl
命令仍然無法執行。
用於執行kubectl
命令,我嘗試按照以下步驟操作管理叢集的使用者或 IAM 角色。但是,他們要求我對aws-auth-cm.yaml
configMap 檔案進行更改,但我無法執行此操作,因為我無法執行kubectl apply
命令。
我的kubeconfig
文件-
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: <cert>
name: arn:aws:eks:us-east-1:123456789012:cluster/my-cluster
contexts:
- context:
cluster: arn:aws:eks:us-east-1:123456789012:cluster/my-cluster
user: arn:aws:eks:us-east-1:123456789012:cluster/my-cluster
name: arn:aws:eks:us-east-1:123456789012:cluster/my-cluster
current-context: arn:aws:eks:us-east-1:123456789012:cluster/my-cluster
kind: Config
preferences: {}
users:
- name: arn:aws:eks:us-east-1:123456789012:cluster/my-cluster
user:
exec:
apiVersion: client.authentication.k8s.io/v1alpha1
args:
- --region
- us-east-1
- eks
- get-token
- --cluster-name
- my-cluster
command: aws
我進行更改後 aws-auth-cm.yaml 檔案的內容是
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-auth
namespace: kube-system
data:
mapRoles: |
# - rolearn: <ARN of instance role (not instance profile)>
# username: system:node:{{EC2PrivateDNSName}}
# groups:
# - system:bootstrappers
# - system:nodes
- rolearn: arn:aws:iam::375712918983:role/myAmazonEKSClusterRole
username: myAmazonEKSClusterRole
groups:
- system:masters
你能幫忙解決這個問題或給一些指示嗎?
請讓我知道,如果你有任何問題。
答案1
你會想要以下三件事之一;任何一個:
- 假設
arn:aws:iam::123456789012:role/myAmazonEKSClusterRole
在終端會話中,這樣aws eks get-token
運行kubectl
將正確運行 - 建立一個承擔該角色的 awscli 設定檔
arn:aws:iam::123456789012:role/myAmazonEKSClusterRole
並更新args:
以包含[..., "--profile", "whatever-you-call-that-profile",...]
- 或在終端機中承擔該角色,運行
aws eks get-token
並將該靜態憑證放入您的 kubeconfig 中,因為您似乎不需要很長時間才能更新該 ConfigMap
對於第一個,我的意思aws sts assume-role --role-arn arn:aws:iam::123456789012:role/myAmazonEKSClusterRole ... | tee sts-creds.json
是然後export AWS_ACCESS_KEY_ID= AWS_SECRET_KEY= AWS_SESSION_TOKEN=
基於該 json 的內容
對於第二個,我的意思是:
$ cat >> $HOME/.aws/config <<FOO
[profile my-cluster]
assume_role = arn:aws:iam::123456789012:role/myAmazonEKSClusterRole
source_profile = whatever
; or credential_source = whatever
FOO
根據本文檔