AWS Builder Center: Learn, Build and Connect with builders in the AWS community
AWS Builder Center is the official home for builders on AWS. Share and read what others are working on, follow people who inspire you, explore training and workshops, and find tools to support what you're building.
如何使用 Amazon EKS 對 RBAC 問題進行疑難排解?
當我使用 Amazon Elastic Kubernetes Service (Amazon EKS) 叢集時,我想要對錯誤進行疑難排解,例如存取遭拒、未經授權和禁止。
簡短描述
AWS Identity and Access Management (IAM) 為叢集提供身分驗證,並依賴原生 Kubernetes 角色型存取控制 (RBAC) 來進行授權。當 IAM 使用者或角色建立 Amazon EKS 叢集時,IAM 實體會新增至具有 system:masters 許可的 Kubernetes RBAC 授權表中。
若要將具有管理員存取權的使用者新增至 Amazon EKS 叢集,請完成以下步驟:
- 為關聯的 IAM 使用者授予所需的 IAM 主控台許可,以便使用者可以執行必要的叢集作業。
- 更新 aws-auth ConfigMap,為其他 IAM 使用者提供叢集角色和角色繫結。如需詳細資訊,請參閱將 IAM 使用者或角色新增至您的 Amazon EKS 叢集。
**注意:**aws-auth ConfigMap 不支援萬用字元。最佳實務是使用 eksctl 來編輯 ConfigMap。項目格式不正確可能會導致鎖定。
執行以下 kubectl auth can-i 命令,以確認是否正確設定 RBAC 許可:
kubectl auth can-i list secrets --namespace dev --as dave
當您執行 kubectl 命令時,身分驗證機制會完成以下主要步驟:
- Kubectl 從 ~/.kube/config 讀取內容組態。
- 執行 AWS Command Line Interface (AWS CLI) 命令 aws eks get-token 以取得憑證,如 .kube/config 中所定義。
- 已傳送 k8s api 請求並使用先前的字符簽署。
**注意:**您無法修改透過 aws eks get-token 獲取的字符的 15 分鐘到期時間。
解決方案
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請確保您使用的是最新的 AWS CLI 版本。
身分驗證問題
Error: "The cluster is inaccessible due to the cluster creator IAM user being deleted" (錯誤:「由於叢集建立者 IAM 使用者已刪除,無法存取叢集」)
如果您收到上述錯誤,則必須使用與叢集相同的名稱重新建立叢集建立者 IAM 使用者。為此,請尋找叢集管理員和叢集建立者的相關資訊。
如果您在過去 90 天內建立了叢集,則可以搜尋 AWS CloudTrail 以進行 CreateCluster API 呼叫。叢集建立者許可與 system:masters 許可相同。如果您有其他具有 system:masters 許可的使用者,則您不需依賴叢集建立者。如果您之前使用 Amazon EKS 叢集進行身分驗證,則可以在 Amazon CloudWatch 日誌群組中檢閱先前的身分驗證日誌。使用以下 CloudWatch 日誌洞察查詢,以檢查叢集管理員使用者和角色詳細資訊:
fields @timestamp, @message | sort @timestamp desc | filter @logStream like /authenticator/ | filter @message like "system:masters"
若要重新建立叢集建立者 IAM 使用者和角色,請執行以下命令:
重要事項:請務必檢查所有 AWS CLI 命令,並用您所需的值取代範例字串的所有執行個體。例如,用使用者名稱取代 EXAMPLE-USER。
aws iam create-user --user-name <EXAMPLE-USER>
aws iam create-role --role-name <EXAMPLE-ROLE>
Error: "Could not be assumed because it does not exist or the trusted entity is not correct or an error occurred when calling the AssumeRole operation" (錯誤:「無法擔任,因為它不存在或信任的實體不正確或呼叫 AssumeRole 作業時發生錯誤」)
如果您收到上述錯誤,則請確認信任政策是否正確地授予使用者擔任許可。如需詳細資訊,請參閱 IAM 教學:使用 IAM 角色將存取權委派至 AWS 帳戶。
若要識別預設部署 Amazon EKS 叢集的本機使用者,請執行以下命令:
kubectl get clusterroles -l kubernetes.io/bootstrapping=rbac-defaults
關閉 API 動作的匿名存取。匿名使用者已將主旨設定為 name: system:unauthenticated。若要識別匿名使用者,請執行以下命令:
kubectl get clusterrolebindings.rbac.authorization.k8s.io -o json | jq '.items[] | select(.subjects[]?.name=="system:unauthenticated")'
如需詳細資訊,請參閱 Amazon EKS 最佳實務指南。
授權問題
Error: "Couldn't get current server API group list" (錯誤:「無法取得目前的伺服器 API 群組清單」)
若要對上述錯誤進行疑難排解,請參閱未經授權或存取遭拒 (kubectl)。
Error: "You must be logged in to the server (Unauthorized)" (錯誤:「您必須登入伺服器 (未經授權)」)
若要對上述錯誤進行疑難排解,請參閱如何解決「您必須登入伺服器 (未經授權)」錯誤?
Error: "You must be logged in to the server (the server has asked for the client to provide credentials)" (錯誤:「您必須登入伺服器 (伺服器已請求用戶端提供憑證)」)
當您使用 IAM 實體進行 API 呼叫且未正確映射 IAM 實體時,就會發生上述錯誤。您必須將 IAM 實體映射至叢集的 aws-auth ConfigMap 中的 Amazon EKS 角色。如需詳細資訊,請參閱開啟叢集的 IAM 使用者和角色存取權。
Error: "Can't describe cluster control plane: AccessDeniedException" (錯誤:「無法描述叢集控制平面:AccessDeniedException」)
當您將 kubeconfig 更新為沒有執行 eks:DescribeCluster 動作許可的使用者和角色時,就會發生上述錯誤。
Error: "Current user or role does not have access to Kubernetes objects on this EKS cluster" (錯誤:「目前的使用者或角色沒有此 EKS 叢集上 Kubernetes 物件的存取權」)
如需上述錯誤的相關資訊,請參閱解決 Amazon EKS 中的 Kubernetes 物件存取錯誤。
Error: "Changing the cluster creator IAM to another user/role" (錯誤:「將叢集建立者 IAM 變更為其他使用者/角色」)
建立叢集後,您無法將叢集建立者 IAM 變更為其他使用者,因為您無法設定叢集建立者 IAM。
網路問題
Error: "Unable to connect to the server: dial tcp 172.xx.xx.xx.xx:443: i/o timeout" (錯誤:「無法連接到伺服器:撥打 tcp 172.xx.xx.xx.xx:443: i/o 逾時」)
如果您收到此錯誤,則請確認安全群組允許來自寄件者來源 IP 地址的流量。
Error: "Unable to connect to the server: x509: certificate is valid for *.example.com , example.com , not https://xxx.gr7.us-east-1.eks.amazonaws.com" (錯誤:「無法連接到伺服器:X.509 憑證對 *.example.com、example.com、 not https://xxx.gr7.us-east-1.eks.amazonaws.com 有效」)
如果您收到此錯誤,則請確認代理設定正確無誤。
KUBECONFIG 問題
Error: "The connection to the server localhost:8080 was refused" (錯誤:「與伺服器 localhost:8080 的連接遭拒」)
缺少 kubeconfig 檔案時,就會發生上述錯誤。kubeconfig 檔案位於 ~/.kube/config 中,且 kubectl 需要該檔案。此檔案包含連接到叢集 API 伺服器所需的叢集憑證。如果 kubectl 找不到此檔案,則其會嘗試連接到預設地址 (localhost:8080)。
Error: "Kubernetes configuration file is group-readable" (錯誤:「Kubernetes 組態檔案為群組可讀」)
當 kubeconfig 檔案的許可不正確時,就會發生上述錯誤。若要解決此問題,請執行以下命令:
chmod o-r ~/.kube/config chmod g-r ~/.kube/config
AWS IAM Identity Center (AWS Single Sign-On 的後繼者) 組態問題
**重要事項:**從 rolearn URL 中移除 /aws-reserved/sso.amazonaws.com/。如果不這樣做,則無法授權您成為有效使用者。
將使用者群組指派給 IAM 許可政策
1. 開啟 IAM Identity Center 主控台。
2. 選擇 AWS Accounts (AWS 帳戶) 標籤,然後選擇 AWS account (AWS 帳戶) 指派使用者。
3. 選擇 Assign Users (指派使用者)。
4. 搜尋使用者群組,然後選擇 Next: Permission sets (下一步:許可集)。
5. 選擇 Create new permission set (建立新的許可集),然後選擇 Create a custom permission set (建立自訂許可集)。
6. 為許可集命名,然後選取 Create a custom permissions policy (建立自訂許可政策) 核取方塊。
7. 複製以下許可政策,然後將其貼到視窗中:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "sts:AssumeRole", "Resource": "*" } ] }
8. 選擇 Create (建立)。
使用 Kubernetes RBAC 設定角色許可
若要使用 Kubernetes RBAC 設定角色許可,請使用以下清單檔案來建立 RBAC 角色:
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: <example name of the RBAC group> namespace: <example name of namespace> rules: - apiGroups: [""] resources: ["services", "endpoints", "pods", "deployments", "ingress"] verbs: ["get", "list", "watch"]
修改 IAM 驗證器 ConfigMap
1. 執行以下命令,以擷取 IAM Identity Center 使用者群組 (包含所需使用者的資料) 的 IAM 角色:
aws iam list-roles | grep Arn
2. 執行以下 h 命令,以修改驗證器 ConfigMap:
kubectl edit configmap aws-auth --namespace kube-system
3. 將以下屬性新增到 mapRoles 下的 ConfigMap 中:
- rolearn: <example arn of the AWS SSO IAM role> username: <example preferred username> groups: - <example name of the RBAC group>
**重要事項:**從 rolearn URL 中移除 /aws-reserved/sso.amazonaws.com/。如果不這樣做,則無法授權您成為有效使用者。
4. 請執行下列命令,來更新 kubeconfig 檔案:
aws eks update-kubeconfig —-name <example eks cluster> —-region <example region>
5. 使用 IAM Identity Center 使用者名稱登入,然後執行 kubectl 命令。
相關資訊
- 語言
- 中文 (繁體)
