本文介紹在首次使用ACK時如何通過Terraform授權Container Service角色。
前提條件
已安裝並配置Terraform。
步驟一:開通Container ServiceACK
在建立ACK叢集前您需要開通相應服務。
建立一個工作目錄,並在工作目錄中建立名為
main.tf
的設定檔。將如下代碼複製到
main.tf
設定檔。執行如下命令,初始化Terraform運行環境。
terraform init
返回資訊如下,Terraform初始化成功。
Initializing the backend... Initializing provider plugins... ... Terraform has been successfully initialized! ...
執行如下命令,開通Container ServiceACK。
terraform apply
返回資訊如下,輸入
yes
,按Enter鍵,服務開通成功。You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
步驟二:授權角色
首次登入Container Service for Kubernetes時,需要為服務帳號授予系統服務角色,具體步驟如下。
在
main.tf
設定檔中添加如下代碼,並執行terraform apply
查詢帳號中是否存在已授權的角色。說明由於Terraform本身的限制,無法自動檢測角色是否存在,且無法自動授權不存在的角色,因此需要您手動查詢角色資訊,並為帳號手動授權需要的角色。
// 判斷角色是否存在。 data "alicloud_ram_roles" "roles" { policy_type = "System" } // 列舉出帳號已被完整授權角色資訊。 output "exist_role" { value = data.alicloud_ram_roles.roles }
返回資訊如下。
No changes. Your infrastructure matches the configuration. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed. Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: ... exist_role = { "id" = "1788****59" "ids" = tolist([ "3009617019****1438", "3023233020****0278", "3302003419****4675", "3178548808****5924", "3371411011****5177", "3475619590****3519", ]) "name_regex" = tostring(null) "names" = tolist([ "AliyunCASDefaultRole", "AliyunContainerRegistryDefaultRole", "AliyunCSDefaultRole", "AliyunCSKubernetesAuditRole", "AliyunCSManagedArmsRole", "AliyunCSManagedCmsRole", "AliyunCSManagedCsiRole", "AliyunCSManagedKubernetesRole", "AliyunCSManagedLogRole", "AliyunCSManagedNetworkRole", "AliyunCSManagedVKRole", "AliyunCSServerlessKubernetesRole", "AliyunServiceRoleForCSB", "AliyunServiceRoleForECI", "AliyunServiceRoleForGws", "AliyunServiceRoleForResourceDirectory", "AliyunServiceRoleForServiceMesh", ]) "output_file" = tostring(null) "policy_name" = tostring(null) "policy_type" = "System" "roles" = tolist([ { "arn" = "acs:ram::1848450434088535:role/aliyuncasdefaultrole" "assume_role_policy_document" = <<-EOT { "Statement": [{ "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": {"Service": ["cas.aliyuncs.com"]}}], "Version": "1"} EOT "create_date" = "2023-07-17T03:27:28Z" "description" = "Apsara Stack Security認證服務(CAS)預設使用此角色來訪問您在其他雲產品中的資源" "document" = <<-EOT { "Statement": [{ "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": {"Service": ["cas.aliyuncs.com"]}}], "Version": "1"} EOT "id" = "300961701980****" "name" = "AliyunCASDefaultRole" "update_date" = "2023-07-17T03:27:28Z" }, { "arn" = "acs:ram::1848450434****:role/aliyuncontainerregistrydefaultrole" "assume_role_policy_document" = <<-EOT { "Statement": [{ "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": {"Service": ["cr.aliyuncs.com"]}}], "Version": "1"} "id" = "3502335964487******" "name" = "AliyunServiceRoleForServiceMesh" "update_date" = "2022-09-27T10:26:50Z" }, ]) }
在
main.tf
設定檔中替換如下授權模板。provider "alicloud" { } // 建立角色。 resource "alicloud_ram_role" "role" { for_each = { for r in var.roles : r.name => r } name = each.value.name document = each.value.policy_document description = each.value.description force = true } // 角色關聯絡統許可權。 resource "alicloud_ram_role_policy_attachment" "attach" { for_each = { for r in var.roles : r.name => r } policy_name = each.value.policy_name policy_type = "System" role_name = each.value.name depends_on = [alicloud_ram_role.role] } // 所需角色。 variable "roles" { type = list(object({ name = string policy_document = string description = string policy_name = string })) default = [ { name = "AliyunCSManagedLogRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集的日誌組件使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedLogRolePolicy" }, { name = "AliyunCSManagedCmsRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集的CMS組件使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedCmsRolePolicy" }, { name = "AliyunCSManagedCsiRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集的儲存外掛程式使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedCsiRolePolicy" }, { name = "AliyunCSManagedVKRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "ACK Serverless叢集的VK組件使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedVKRolePolicy" }, { name = "AliyunCSClusterRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集在應用運行期使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSClusterRolePolicy" }, { name = "AliyunCSServerlessKubernetesRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集預設使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSServerlessKubernetesRolePolicy" }, { name = "AliyunCSKubernetesAuditRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集審計功能使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSKubernetesAuditRolePolicy" }, { name = "AliyunCSManagedNetworkRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集網路組件使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedNetworkRolePolicy" }, { name = "AliyunCSDefaultRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集操作時預設使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSDefaultRolePolicy" }, { name = "AliyunCSManagedKubernetesRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集預設使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedKubernetesRolePolicy" }, { name = "AliyunCSManagedArmsRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集Arms外掛程式使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCSManagedArmsRolePolicy" }, { name = "AliyunCISDefaultRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"cs.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "Container Service(CS)智能營運使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunCISDefaultRolePolicy" }, { name = "AliyunOOSLifecycleHook4CSRole" policy_document = "{\"Statement\":[{\"Action\":\"sts:AssumeRole\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"oos.aliyuncs.com\"]}}],\"Version\":\"1\"}" description = "叢集擴縮容節點池依賴OOS服務,OOS使用此角色來訪問您在其他雲產品中的資源。" policy_name = "AliyunOOSLifecycleHook4CSRolePolicy" } ] }
執行如下命令,初始化Terraform運行環境。
terraform init
返回資訊如下:“Terraform初始化成功”。
Initializing the backend... Initializing provider plugins... ... Terraform has created a lock file .terraform.lock.hcl to record the providerselections it made above. Include this file in your version control repositoryso that Terraform can guarantee to make the same selections by default whenyou run "terraform init" in the future. Terraform has been successfully initialized! ...
執行
terraform apply
,為您的帳號進行角色授權。返回資訊如下,輸入
yes
,按Enter鍵,表示授權成功。..... Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:
執行如下命令,查看已存在的角色。
terraform show
返回資訊如下,列舉出了帳號授權的所有角色資訊,表示角色授權已完成。
data "alicloud_ram_roles" "roles" { ... "names" = [ "AliyunCISDefaultRole", "AliyunCSClusterRole", "AliyunCSDefaultRole", ... ] ... }
服務角色
AliyunCSManagedLogRole
說明:ACK託管叢集和ACK Serverless叢集的日誌組件使用該角色訪問您在SLS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedLogRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的日誌組件使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedLogRolePolicy" }
AliyunCSManagedCmsRole
說明:ACK託管叢集和ACK Serverless叢集的監控組件使用該角色訪問您在CMS、SLS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedCmsRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的CMS組件使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedCmsRolePolicy" }
AliyunCSManagedCsiRole
說明:ACK託管叢集和ACK Serverless叢集的儲存群組件使用該角色訪問您在ECS、NAS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedCsiRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的儲存外掛程式使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedCsiRolePolicy" }
AliyunCSManagedVKRole
說明:ACK Serverless叢集的Virtual Node組件使用該角色訪問您在ECS、VPC、ECI等服務中的資源。
授權碼:
{ "name": "AliyunCSManagedVKRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "ACK Serverless叢集的VK組件使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedVKRolePolicy" }
AliyunCSServerlessKubernetesRole
說明:ACK Serverless叢集使用該角色來訪問您在ECS、VPC、SLB、PVTZ等服務中的資源。
授權碼:
{ "name": "AliyunCSServerlessKubernetesRole" "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "ACK Serverless叢集預設使用此角色來訪問您在其他雲產品中的資源。" "policy_name": "AliyunCSServerlessKubernetesRolePolicy" }
AliyunCSKubernetesAuditRole
說明:ACK託管叢集和ACK Serverless叢集的審計功能使用該角色來訪問您在SLS服務中的資源。
授權碼:
{ "name": "AliyunCSKubernetesAuditRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的審計功能使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSKubernetesAuditRolePolicy" }
AliyunCSManagedNetworkRole
說明:ACK託管叢集和ACK Serverless叢集的網路組件使用該角色訪問您在ECS、VPC服務中的資源。
授權碼:
{ "name": "AliyunCSManagedNetworkRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的網路組件使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedNetworkRolePolicy" }
AliyunCSDefaultRole
說明:ACK在叢集管控操作中使用該角色訪問您在ECS、VPC、SLB、ROS、ESS等服務中的資源。
授權碼:
{ "name": "AliyunCSDefaultRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集在叢集操作時預設使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSDefaultRolePolicy" }
AliyunCSManagedKubernetesRole
說明:ACK託管叢集使用該角色訪問您在ECS、VPC、SLB、ACR等服務中的資源。
授權碼:
{ "name": "AliyunCSManagedKubernetesRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集預設使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedKubernetesRolePolicy" }
AliyunCSManagedArmsRole
說明:ACK託管叢集和ACK Serverless叢集叢集的應用即時監控組件使用該角色訪問您在ARMS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedArmsRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的Arms外掛程式使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedArmsRolePolicy" }
AliyunCSClusterRole
說明:Container Service(CS)在應用運行期使用此角色來訪問您在其他雲產品中的資源許可權描述:用於Container Service(CS) Cluster 角色的權限原則。
授權碼:
{ "name": "AliyunCSClusterRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集在應用運行期使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSClusterRolePolicy" }
可選角色
AliyunCISDefaultRole
說明:ACK容器智能營運將使用該角色訪問您在ECS、VPC、SLB等服務中的資源,為您提供診斷和巡檢等服務。
授權碼:
{ "name": "AliyunCISDefaultRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "ACK智能營運使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCISDefaultRolePolicy" }
AliyunCSManagedAcrRole
說明:ACK託管叢集和ACK Serverless叢集的鏡像拉取免密外掛程式使用該角色訪問您在ACRContainer Registry中的資源。
授權碼:
{ "name": "AliyunCSManagedAcrRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的鏡像拉取免密外掛程式使用該角色訪問您在ACRContainer Registry中的資源。", "policy_name": "AliyunCSManagedAcrRolePolicy" }
AliyunCSManagedNlcRole
說明:ACK託管叢集託管節點池控制組件使用該角色訪問您的ECS和ACK節點池資源。
授權碼:
{ "name": "AliyunCSManagedNlcRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集託管節點池控制組件使用該角色訪問您的ECS和ACK節點池資源。", "policy_name": "AliyunCSManagedNlcRolePolicy" }
AliyunCSManagedAutoScalerRole
說明:ACK託管叢集和ACK Serverless叢集的Auto Scaling組件使用該角色來訪問您在ESS和ECS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedAutoScalerRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的Auto Scaling組件使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunCSManagedAutoScalerRolePolicy" }
AliyunCSManagedSecurityRole
說明:ACK託管叢集和ACK Serverless叢集的落盤加密外掛程式使用該角色訪問您在KMS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedSecurityRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的落盤加密外掛程式使用該角色訪問您在KMS服務中的資源。", "policy_name": "AliyunCSManagedSecurityRolePolicy" }
AliyunCSManagedCostRole
說明:ACK託管叢集和ACK Serverless叢集的成本分析組件使用該角色訪問您在賬單管理API、ECS和ECI服務中的資源。
授權碼:
{ "name": "AliyunCSManagedCostRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的成本分析組件使用該角色訪問您在賬單管理API、ECS和ECI服務中的資源。", "policy_name": "AliyunCSManagedCostRolePolicy" }
AliyunCSManagedNimitzRole
說明:ACK靈駿叢集的網路組件使用該角色訪問您在智能計算靈駿服務中的資源。
授權碼:
{ "name": "AliyunCSManagedNimitzRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "ACK靈駿叢集的網路組件使用該角色訪問您在智能計算靈駿服務中的資源。", "policy_name": "AliyunCSManagedNimitzRolePolicy" }
AliyunCSManagedBackupRestoreRole
說明:ACK託管叢集的備份中心組件使用該角色訪問您在雲備份(Cloud Backup)服務和OSS服務中的資源。
授權碼:
{ "name": "AliyunCSManagedBackupRestoreRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集的備份中心組件使用該角色訪問您在雲備份(Cloud Backup)服務和OSS服務中的資源。", "policy_name": "AliyunCSManagedBackupRestoreRolePolicy" }
AliyunCSManagedEdgeRole
說明:ACK Edge叢集的管控組件使用該角色訪問您在Smart Access Gateway、VPC和雲企業網CEN服務中的資源。
授權碼:
{ "name": "AliyunCSManagedEdgeRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["cs.aliyuncs.com"] } } ], "Version": "1" }, "description": "ACK Edge叢集的管控組件使用該角色訪問您在Smart Access Gateway、VPC和雲企業網CEN服務中的資源。", "policy_name": "AliyunCSManagedEdgeRolePolicy" }
AliyunOOSLifecycleHook4CSRole
說明:Container Service Kubernetes 版擴縮容節點池依賴OOS服務,OOS使用此角色來訪問您在其他雲產品中的資源。
授權碼:
{ "name": "AliyunOOSLifecycleHook4CSRole", "policy_document": { "Statement": [ { "Action": "sts:AssumeRole", "Effect": "Allow", "Principal": { "Service": ["oos.aliyuncs.com"] } } ], "Version": "1" }, "description": "叢集擴縮容節點池依賴OOS服務,OOS使用此角色來訪問您在其他雲產品中的資源。", "policy_name": "AliyunOOSLifecycleHook4CSRolePolicy" }