set { name = "clusterName" value = module.eks-6992-hk-uat.cluster_name } # --- 新增以下两项以解决 VPC ID 获取失败的问题 --- set { name = "vpcId" value = module.eks-6992-hk-uat-vpc.vpc_id }
set { name = "region" value = var.aws_region } # ----------------------------------------------
set { name = "serviceAccount.create" value = "true" }
set { name = "serviceAccount.name" value = "aws-load-balancer-controller" }
set { name = "serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn" value = module.alb_controller_irsa_role.iam_role_arn } }
set { name = "controller.serviceAccount.create" value = "true" }
set { name = "controller.serviceAccount.name" value = "efs-csi-controller-sa" }
set { name = "controller.serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
value = module.efs_csi_irsa_role.iam_role_arn } } # 创建 StorageClass resource "kubernetes_storage_class" "efs" { metadata { name = "efs-sc" }
storage_provisioner = "efs.csi.aws.com"
parameters = { provisioningMode = "efs-ap"
fileSystemId = aws_efs_file_system.eks_efs.id
directoryPerms = "700"
gidRangeStart = "1000" gidRangeEnd = "2000"
basePath = "/dynamic_provisioning" }
reclaim_policy = "Retain"
volume_binding_mode = "Immediate" }
初始化并应用 Terraform 项目
执行以下命令初始化 Terraform 项目
$ terraform init Initializing modules... Downloading registry.terraform.io/terraform-aws-modules/vpc/aws 5.8.1 for eks-6992-hk-uat-vpc... - eks-6992-hk-uat-vpc in .terraform/modules/eks-6992-hk-uat-vpc ... Initializing the backend...
Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future.
Terraform has been successfully initialized!
使用命令 terraform plan 查看 Terraform 计划,可以检查是否有报错,并确认是否符合预期。
$ terraform plan ╷ │ Error: Reference to undeclared module │ │ on eks.tf line 8, in module "eks-6992-hk-uat": │ 8: vpc_id = module.vpc.vpc_id # VPC ID,用于创建 EKS 集群的 VPC ID │ │ No module call named "vpc" is declared in the root module. ╵ ╷ │ Error: Reference to undeclared module │ │ on eks.tf line 9, in module "eks-6992-hk-uat": │ 9: subnet_ids = module.vpc.private_subnets # Private Subnets,节点实例和工作节点实例位于 Private Subnets 中的可用区 │ │ No module call named "vpc" is declared in the root module. ╵
语法无误的情况下,可以查看 Terraform 计划,确认是否符合预期。
$ terraform plan # 读取已有数据,包括 AWS 身份标识符(Caller Identity)以及 Terraform 模块的内容 module.eks-6992-hk-uat.module.kms.data.aws_caller_identity.current[0]: Reading... module.eks-6992-hk-uat.data.aws_caller_identity.current[0]: Reading... module.eks-6992-hk-uat.module.eks_managed_node_group["default"].data.aws_partition.current: Reading... module.eks-6992-hk-uat.module.kms.data.aws_partition.current[0]: Reading... module.eks-6992-hk-uat.data.aws_partition.current[0]: Reading... module.eks-6992-hk-uat.module.eks_managed_node_group["default"].data.aws_caller_identity.current: Reading... module.eks-6992-hk-uat.module.eks_managed_node_group["default"].data.aws_partition.current: Read complete after 0s [id=aws] module.eks-6992-hk-uat.data.aws_caller_identity.current[0]: Read complete after 0s [id=<AWS_ID>] module.eks-6992-hk-uat.module.eks_managed_node_group["default"].data.aws_iam_policy_document.assume_role_policy[0]: Reading... module.eks-6992-hk-uat.data.aws_partition.current[0]: Read complete after 0s [id=aws] module.eks-6992-hk-uat.data.aws_iam_policy_document.assume_role_policy[0]: Reading... module.eks-6992-hk-uat.module.kms.data.aws_partition.current[0]: Read complete after 0s [id=aws] module.eks-6992-hk-uat.module.kms.data.aws_caller_identity.current[0]: Read complete after 0s [id=<AWS_ID>] module.eks-6992-hk-uat.module.eks_managed_node_group["default"].data.aws_iam_policy_document.assume_role_policy[0]: Read complete after 0s [id=2560088296] module.eks-6992-hk-uat.module.eks_managed_node_group["default"].data.aws_caller_identity.current: Read complete after 0s [id=<AWS_ID>] module.eks-6992-hk-uat.data.aws_iam_session_context.current[0]: Reading... module.eks-6992-hk-uat.data.aws_iam_policy_document.assume_role_policy[0]: Read complete after 0s [id=2830595799] module.eks-6992-hk-uat.data.aws_[0]: Reading[0]: Reading... module.eks-6992-hk-uat.data.aws_iam_session_context.current[0]: Read complete after 0s [id=arn:aws:iam::<AWS_ID>:userops] module.eks-6992-hk-uat.data.aws_iam_policy_document.custom[0]: Read complete after 0s [id=513122117]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create <= read (data resources) # 真正要执行的操作计划 Terraform will perform the following actions: # module.eks-6992-hk-uat.data.tls_certificate.this[0] will be read during apply # (config refers to values not yet known) <= data "tls_certificate" "this" { + certificates = (known after apply) + id = (known after apply) + url = (known after apply) } # module.eks-6992-hk-uat.aws_eks_cluster.this[0] will be created + resource "aws_eks_cluster" "this" { + arn = (known after apply) + bootstrap_self_managed_addons = true + certificate_authority = (known after apply) + cluster_id = (known after apply) ... + tags = { + "terraform-aws-modules" = "eks" } + tags_all = { + "terraform-aws-modules" = "eks" } + version = "1.35"
+ encryption_config { + resources = [ + "secrets", ... } } # module.eks-6992-hk-uat.aws_iam_openid_connect_provider.oidc_provider[0] will be created + resource "aws_iam_openid_connect_provider" "oidc_provider" { + arn = (known after apply) ... } # module.eks-6992-hk-uat.aws_iam_policy.cluster_encryption[0] will be created + resource "aws_iam_policy" "cluster_encryption" { ... } # module.eks-6992-hk-uat.aws_iam_policy.custom[0] will be created + resource "aws_iam_policy" "custom" { + arn = (known after apply) + attachment_count = (known after apply) + id = (known after apply) + name = (known after apply) + name_prefix = "eks-6992-hk-uat-cluster-" + path = "/" + policy = jsonencode( { ... }
# module.eks-6992-hk-uat.aws_iam_role_policy_attachment.this["AmazonEKSVPCResourceController"] will be created + resource "aws_iam_role_policy_attachment" "this" { ... } # module.eks-6992-hk-uat.aws_security_group.cluster[0] will be created + resource "aws_security_group" "cluster" { ... + revoke_rules_on_delete = false + tags = { + "Name" = "eks-6992-hk-uat-cluster" } + tags_all = { + "Name" = "eks-6992-hk-uat-cluster" } + vpc_id = (known after apply) } # module.eks-6992-hk-uat.aws_security_group.node[0] will be created + resource "aws_security_group" "node" { ... + revoke_rules_on_delete = false + tags = { + "Name" = "eks-6992-hk-uat-node" + "kubernetes.io/cluster/eks-6992-hk-uat" = "owned" } + tags_all = { + "Name" = "eks-6992-hk-uat-node" + "kubernetes.io/cluster/eks-6992-hk-uat" = "owned" } + vpc_id = (known after apply) }
# module.eks-6992-hk-uat-vpc.aws_eip.nat[0] will be created + resource "aws_eip" "nat" { ... } + tags_all = { + "Name" = "eks-6992-hk-uat-vpc-ap-east-1-1a" } + vpc = (known after apply) }
# module.eks-6992-hk-uat.module.kms.data.aws_iam_policy_document.this[0] will be read during apply # (config refers to values not yet known) <= data "aws_iam_policy_document" "this" { ...
$ kubectl get pod NAME READY STATUS RESTARTS AGE efs-test 1/1 Running 0 113s $ kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE efs-pvc Bound pvc-dc224e47-0365-46d9-9c5b-06e5cb1fb95b 5Gi RWX efs-sc <unset> 2m8s