使用Terraform创建ACK Edge集群

Terraform是一种开源工具,用于安全高效地预览、配置和管理云基础架构和资源,帮助开发者自动化地创建、更新阿里云基础设施资源,并进行版本管理。本文介绍如何使用Terraform创建ACK Edge集群

前提条件

  • 已安装Terraform。

    说明

    请确认Terraform版本不低于v0.12.28,可通过terraform --version命令查看Terraform版本。

    • Cloud Shell默认安装配置了Terraform和阿里云账号信息,无需任何额外配置。

    • 如果您不使用Cloud Shell,关于安装Terraform的方式,请参见在本地安装和配置Terraform

  • 配置阿里云账号信息。

    创建环境变量,用于存放身份认证信息。

    export ALICLOUD_ACCESS_KEY="************"   #替换为阿里云账号的AK信息。
    export ALICLOUD_SECRET_KEY="************"   #替换为阿里云账号的SK信息。
    export ALICLOUD_REGION="cn-beijing"         #替换为您集群所在的地域。
    说明

    为提高权限管理的灵活性和安全性,建议您创建名为Terraform的RAM用户,并为该RAM用户创建AccessKey和授权。具体操作,请参见创建RAM用户为RAM用户授权

使用Terraform创建ACK Edge集群

  1. 创建一个工作目录,并且在工作目录中创建以下名为main.tf的配置文件。

    main.tf文件描述了以下的Terraform配置:

    • 创建一个新的VPC,并创建一个该VPC下的vSwitch。

    • 创建一个ACK Edge集群

    • 创建一个包含两个节点的节点池。

    provider "alicloud" {
    }
    
    variable "k8s_name_prefix" {
      description = "The name prefix used to create edge kubernetes cluster."
      default     = "edge-example"
    }
    
    resource "random_uuid" "this" {}
    # 默认资源名称。
    locals {
      k8s_name_edge = substr(join("-", [var.k8s_name_prefix,"edge"]), 0, 63)
      new_vpc_name  = "tf-vpc-172-16"
      new_vsw_name  = "tf-vswitch-172-16-0"
      nodepool_name = "edge-nodepool-1"
    }
    
    data "alicloud_zones" "default" {
        available_resource_creation = "VSwitch"
    }
    
    data "alicloud_instance_types" "default" {
        availability_zone    = data.alicloud_zones.default.zones.0.id
        cpu_core_count       = 4
        memory_size          = 8
        kubernetes_node_role = "Worker"
    }
    
    resource "alicloud_vpc" "vpc" {
      vpc_name   = local.new_vpc_name
      cidr_block = "172.16.0.0/12"
    }
    
    resource "alicloud_vswitch" "vsw" {
      vswitch_name      = local.new_vsw_name
        vpc_id          = alicloud_vpc.vpc.id
        cidr_block      = cidrsubnet(alicloud_vpc.vpc.cidr_block, 8, 8)
        zone_id         = data.alicloud_zones.default.zones.0.id
    }
    
    
    resource "alicloud_cs_edge_kubernetes" "edge" {
      name                  = local.k8s_name_edge
      version               = "1.20.11-aliyunedge.1"  #替换为您所需创建的集群版本。
      cluster_spec          = "ack.pro.small"
      worker_vswitch_ids    = split(",", join(",", alicloud_vswitch.vsw.*.id))
      worker_instance_types = [data.alicloud_instance_types.default.instance_types.0.id]
      new_nat_gateway       = true
      pod_cidr              = "10.10.0.0/16"
      service_cidr          = "10.12.0.0/16"
      load_balancer_spec    = "slb.s2.small"
      time_zone             = "Asia/Shanghai"
      worker_number         = 1
      node_cidr_mask        = 24
    
      # 运行时。
      runtime = {
        name    = "containerd"
        version = "1.5.13"     #替换为您所需创建的运行时版本。
      }
    
      # 节点池。
    resource "alicloud_cs_kubernetes_node_pool" "nodepool" {
      # Kubernetes集群名称。
      cluster_id            = alicloud_cs_edge_kubernetes.edge.id
      # 节点池名称。
      name                  = local.nodepool_name
      # 新的Kubernetes集群将位于的vSwitch。指定一个或多个vSwitch的ID。它必须在availability_zone指定的区域中。
      vswitch_ids           = split(",", join(",", alicloud_vswitch.vsw.*.id))
    
      # ECS实例类型和收费方式。
      instance_types        = [data.alicloud_instance_types.default.instance_types.0.id]
      instance_charge_type  = "PostPaid"
    
      # 可选,自定义实例名称。
      # node_name_mode      = "customized,edge-shenzhen,ip,default"
    
      #容器运行时。
      runtime_name          = "containerd"
      runtime_version       = "1.5.13"
    
      # Kubernetes集群的总工作节点数。默认值为3。最大限制为50。
      desired_size          = 2
      # SSH登录集群节点的密码。
      password              = "******"
    
      # 是否为Kubernetes的节点安装云监控。
      install_cloud_monitor = true
    
      # 节点的系统磁盘类别。其有效值为cloud_ssd和cloud_efficiency。默认为cloud_efficiency。
      system_disk_category  = "cloud_efficiency"
      system_disk_size      = 100
    
      # 操作系统类型。
      image_type            = "AliyunLinux"
    
      # 节点数据盘配置。
      data_disks {
        # 节点数据盘种类。
        category = "cloud_ssd"
        # 节点数据盘大小。
        size     = 120
      }
    }
  2. 执行以下命令初始化Terraform运行环境。

    terraform init

    预期输出:

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.184.0...
    ...
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 执行以下命令生成资源规划。

    terraform plan

    预期输出:

    Refreshing Terraform state in-memory prior to plan...
    The refreshed state will be used to calculate this plan, but will not be
    persisted to local or remote state storage.
    ...
    Plan: 5 to add, 0 to change, 0 to destroy.
    ...
  4. 执行以下命令创建集群。

    terraform apply

    预期输出:

    ...
    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
    ...
    alicloud_cs_edge_kubernetes.edge: Creation complete after 8m26s [id=************]
    
    Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

使用Terraform删除ACK Edge集群

您可以通过执行以下命令,删除通过Terraform创建的ACK Edge集群

terraform destroy

预期输出:

...
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes
...
Destroy complete! Resources: 5 destroyed.