建立 IAM 角色和 AWS Secret
您可以設定 Kubernetes pod 以透過 AWS IAM 角色進行驗證來存取 AWS 資源,而不是提供明確的 AWS 憑證。
|
|
若要使用 AWS IAM 角色進行驗證,您必須擁有一個使用 EKS 部署的 Kubernetes 叢集。 |
建立 AWS Secrets Manager 金鑰
由於Trident將向 FSx 虛擬伺服器發出 API 來為您管理存儲,因此它需要憑證才能執行此操作。傳遞這些憑證的安全方法是透過 AWS Secrets Manager 金鑰。因此,如果您還沒有 AWS Secrets Manager 金鑰,則需要建立一個包含 vsadmin 帳戶憑證的金鑰。
此範例建立一個 AWS Secrets Manager 金鑰來儲存Trident CSI 憑證:
aws secretsmanager create-secret --name trident-secret --description "Trident CSI credentials"\
--secret-string "{\"username\":\"vsadmin\",\"password\":\"<svmpassword>\"}"
建立 IAM 策略
Trident也需要 AWS 權限才能正常運作。因此,您需要建立一個策略,賦予Trident所需的權限。
以下範例使用 AWS CLI 建立 IAM 原則:
aws iam create-policy --policy-name AmazonFSxNCSIDriverPolicy --policy-document file://policy.json
--description "This policy grants access to Trident CSI to FSxN and Secrets manager"
策略 JSON 範例:
{
"Statement": [
{
"Action": [
"fsx:DescribeFileSystems",
"fsx:DescribeVolumes",
"fsx:CreateVolume",
"fsx:RestoreVolumeFromSnapshot",
"fsx:DescribeStorageVirtualMachines",
"fsx:UntagResource",
"fsx:UpdateVolume",
"fsx:TagResource",
"fsx:DeleteVolume"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Action": "secretsmanager:GetSecretValue",
"Effect": "Allow",
"Resource": "arn:aws:secretsmanager:<aws-region>:<aws-account-id>:secret:<aws-secret-manager-name>*"
}
],
"Version": "2012-10-17"
}
建立 Pod 身分或 IAM 角色以關聯服務帳戶 (IRSA)
您可以設定 Kubernetes 服務帳戶,使其承擔 AWS Identity and Access Management (IAM) 角色(使用 EKS Pod Identity 或 IAM 角色進行服務帳戶關聯)(IRSA)。任何已配置為使用該服務帳戶的 Pod 都可以存取該角色有權存取的任何 AWS 服務。
Amazon EKS Pod 身分關聯可讓您管理應用程式的憑證,類似於 Amazon EC2 執行個體設定檔向 Amazon EC2 執行個體提供憑證的方式。
在 EKS 叢集上安裝 Pod Identity:
您可以透過 AWS 控制台建立 Pod 標識,也可以使用下列 AWS CLI 命令:
aws eks create-addon --cluster-name <EKS_CLUSTER_NAME> --addon-name eks-pod-identity-agent
更多資訊請參閱"設定 Amazon EKS Pod 身分代理"。
建立 trust-relationship.json 檔案:
建立 trust-relationship.json 文件,使 EKS 服務主體能夠承擔 Pod 身分的此角色。然後使用以下信任策略建立角色:
aws iam create-role \ --role-name fsxn-csi-role --assume-role-policy-document file://trust-relationship.json \ --description "fsxn csi pod identity role"
trust-relationship.json 文件:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "pods.eks.amazonaws.com"
},
"Action": [
"sts:AssumeRole",
"sts:TagSession"
]
}
]
}
將角色策略附加到 IAM 角色:
將上一個步驟中建立的角色策略附加到已建立的 IAM 角色:
aws iam attach-role-policy \ --policy-arn arn:aws:iam::aws:111122223333:policy/fsxn-csi-policy \ --role-name fsxn-csi-role
建立 Pod 身分關聯:
在 IAM 角色和Trident服務帳戶 (trident-controller) 之間建立 Pod 身分關聯
aws eks create-pod-identity-association \ --cluster-name <EKS_CLUSTER_NAME> \ --role-arn arn:aws:iam::111122223333:role/fsxn-csi-role \ --namespace trident --service-account trident-controller
使用 AWS CLI:
aws iam create-role --role-name AmazonEKS_FSxN_CSI_DriverRole \ --assume-role-policy-document file://trust-relationship.json
trust-relationship.json 文件:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::<account_id>:oidc-provider/<oidc_provider>"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"<oidc_provider>:aud": "sts.amazonaws.com",
"<oidc_provider>:sub": "system:serviceaccount:trident:trident-controller"
}
}
}
]
}
更新以下值 `trust-relationship.json`文件:
-
<account_id> - 您的 AWS 帳戶 ID
-
<oidc_provider> - 您的 EKS 叢集的 OIDC。您可以透過執行以下命令來取得oidc_provider:
aws eks describe-cluster --name my-cluster --query "cluster.identity.oidc.issuer"\ --output text | sed -e "s/^https:\/\///"
將 IAM 角色與 IAM 原則關聯:
角色創建完成後,使用以下命令將策略(在上一個步驟中建立的策略)附加到該角色:
aws iam attach-role-policy --role-name my-role --policy-arn <IAM policy ARN>
請核實 OICD 提供者是否已關聯:
請確認您的 OIDC 提供者已與您的叢集關聯。您可以使用以下命令進行驗證:
aws iam list-open-id-connect-providers | grep $oidc_id | cut -d "/" -f4
如果輸出為空,請使用以下命令將 IAM OIDC 關聯到您的叢集:
eksctl utils associate-iam-oidc-provider --cluster $cluster_name --approve
如果您使用的是 eksctl,請使用以下範例在 EKS 中為服務帳戶建立 IAM 角色:
eksctl create iamserviceaccount --name trident-controller --namespace trident \
--cluster <my-cluster> --role-name AmazonEKS_FSxN_CSI_DriverRole --role-only \
--attach-policy-arn <IAM-Policy ARN> --approve