文档

使用Terraform创建ACK专有集群

更新时间:

本文介绍如何使用Terraform创建ACK专有集群

前提条件

前提条件

  • 已安装Terraform

    说明

    请确保版本不低于v0.12.28。如需检查现有版本,请运行terraform --version命令。

    安装配置方式如下:

  • 配置阿里云账号信息。

    执行如下命令,创建环境变量,用于存放身份认证信息。

    阿里云账号(主账号)对账号中的资源具有完全管理权限,一旦泄露风险极大。推荐您创建一个名为Terraform的RAM用户,并为该RAM用户创建AccessKey和授权。具体操作,请参见创建RAM用户为RAM用户授权

    Linux环境

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

    Windows环境

    set ALICLOUD_ACCESS_KEY="************"   # 替换为阿里云账号的AK信息。
    set ALICLOUD_SECRET_KEY="************"   # 替换为阿里云账号的SK信息。
    set ALICLOUD_REGION="cn-beijing"         # 替换为您集群所在的地域。
  • 已开通容器服务 Kubernetes 版ACK。若需要使用Terraform开通,请参见首次开通ACK并授权默认角色

  • 创建工作目录,并在工作目录中创建variable.tf配置文件。

    在Terraform中,variable.tf可以定义输入变量,用于参数化资源,以便在不同配置中重复使用。这些变量随后将在main.tf文件中用于具体的资源配置。

    下面是一个variable.tf文件的示例:

    • 创建1个新的VPC,并创建3个该VPC下的vSwitch、3个Pod vSwitch。

    • 在创建ACK集群中,默认安装的组件有:Terway(网络组件)、csi-plugin(存储组件)、csi-provisioner(存储组件)、logtail-ds(日志组件)、Nginx Ingress Controller、ack-arms-prometheus(监控组件)。

  • 展开查看示例variable.tf文件

    variable "name" {      # 定义资源的名称或标签。
      default = "tf-example"
    }
    
    # leave it to empty would create a new one
    variable "vpc_id" {    # 指定现有的vpc_id。如果为空,则表示创建一个新的VPC。
      description = "Existing vpc id used to create several vswitches and other resources."
      default     = ""
    }
    
    variable "vpc_cidr" {  # 当没有指定vpc_id时,定义了新VPC的CIDR地址,即IP地址范围。
      description = "The cidr block used to launch a new vpc when 'vpc_id' is not specified."
      default     = "10.0.0.0/8"
    }
    
    # leave it to empty then terraform will create several vswitches
    variable "vswitch_ids" { # 指定现有的vSwitch(虚拟交换机)ID。如果列表为空,默认会根据填写的vswitch_cidrs创建新的vSwitch。
      description = "List of existing vswitch id."
      type        = list(string)
      default     = []
    }
    
    
    variable "vswitch_cidrs" { # 创建新的vSwitch,需要填写三个且不重叠的CIDR地址块。
      description = "List of cidr blocks used to create several new vswitches when 'vswitch_ids' is not specified."
      type        = list(string)
      default     = ["10.1.0.0/16", "10.2.0.0/16", "10.3.0.0/16"]
    }
    
    variable "terway_vswitch_ids" {  # 指定网络组件Terway配置。如果为空,默认会根据terway_vswitch_cidrs的创建新的terway vSwitch。
      description = "List of existing vswitch ids for terway."
      type        = list(string)
      default     = []
    }
    
    variable "terway_vswitch_cidrs" { # 当没有指定terway_vswitch_ids时,用于创建Terway使用的vSwitch的CIDR地址块。
      description = "List of cidr blocks used to create several new vswitches when 'terway_vswitch_cidrs' is not specified."
      type        = list(string)
      default     = ["10.4.0.0/16", "10.5.0.0/16", "10.6.0.0/16"]
    }
    
    variable "cluster_addons" { # 指定ACK集群安装的组件。声明每个组件的名称和对应配置。
      type = list(object({
        name   = string
        config = string
      }))
    
      default = [
        {
          "name"   = "terway-eniip",
          "config" = "",
        },
        {
          "name"   = "csi-plugin",
          "config" = "",
        },
        {
          "name"   = "csi-provisioner",
          "config" = "",
        },
        {
          "name"   = "logtail-ds",
          "config" = "\"IngressDashboardEnabled\":\"true\"",
        },
        {
          "name"   = "nginx-ingress-controller",
          "config" = "\"IngressSlbNetworkType\":\"internet\"",
        },
        {
          "name"   = "arms-prometheus",
          "config" = "",
        },
        {
          "name"   = "ack-node-problem-detector",
          "config" = "\"sls_project_name\":\"\"",
        }
      ]
    }
    说明

    variable.tf配置中的地域请与下文main.tf配置文件保持一致。

使用Terraform创建ACK专有集群

本小节以创建一个使用Terway网络组件的ACK专有集群为例。

  1. 前提条件中创建的工作目录中创建名为main.tf的配置文件。

    说明

    main.tf用来在阿里云上创建和配置ACK集群资源。执行创建过程中,main.tf文件会引用variables.tf文件中声明的变量。

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

    • 创建1个新的VPC,并创建3个该VPC下的vSwitch、3个Pod vSwitch。

    • 创建1个ACK专有集群

    • 创建1个包含2个节点的节点池。

    • 创建1个自动伸缩节点池。

    展开查看main.tf示例文件

    data "alicloud_enhanced_nat_available_zones" "enhanced" {} # 查询用于获取支持增强型网关NAT的区域。
    
    # If there is not specifying vpc_id, the module will launch a new vpc
    resource "alicloud_vpc" "vpc" {  # 当没有提供vpc_id变量时,这个资源将创建一个新的专有网络,其CIDR块由vpc_cidr变量指定。
      count      = var.vpc_id == "" ? 1 : 0
      cidr_block = var.vpc_cidr
    }
    
    # According to the vswitch cidr blocks to launch several vswitches
    resource "alicloud_vswitch" "vswitches" { # 查询VPC vSwitch资源。
      count      = length(var.vswitch_ids) > 0 ? 0 : length(var.vswitch_cidrs)
      vpc_id     = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
      cidr_block = element(var.vswitch_cidrs, count.index)
      zone_id    = data.alicloud_enhanced_nat_available_zones.enhanced.zones[count.index].zone_id
    }
    
    # According to the vswitch cidr blocks to launch several vswitches
    resource "alicloud_vswitch" "terway_vswitches" { # 查询当前阿里云用户的资源组。
      count      = length(var.terway_vswitch_ids) > 0 ? 0 : length(var.terway_vswitch_cidrs)
      vpc_id     = var.vpc_id == "" ? join("", alicloud_vpc.vpc.*.id) : var.vpc_id
      cidr_block = element(var.terway_vswitch_cidrs, count.index)
      zone_id    = data.alicloud_enhanced_nat_available_zones.enhanced.zones[count.index].zone_id
    }
    
    data "alicloud_resource_manager_resource_groups" "default" { 
      status = "OK"
    }
    
    data "alicloud_instance_types" "default" { # 查询阿里云的ECS实例类型。
      count                = 3
      availability_zone    = data.alicloud_enhanced_nat_available_zones.enhanced.zones[0].zone_id
      cpu_core_count       = 4
      memory_size          = 8
    }
    
    resource "alicloud_cs_kubernetes" "default" {  # 创建ACK专有集群,配置包括控制面虚拟交换机、Pod虚拟交换机、实例类型、磁盘、密码、Service网络地址段等。
      master_vswitch_ids    = length(var.vswitch_ids) > 0 ? split(",", join(",", var.vswitch_ids)) : length(var.vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.vswitches.*.id)) # 查询支持增强型NAT的可用区列表。
      pod_vswitch_ids       = length(var.terway_vswitch_ids) > 0 ? split(",", join(",", var.terway_vswitch_ids)) : length(var.terway_vswitch_cidrs) < 1 ? [] : split(",", join(",", alicloud_vswitch.terway_vswitches.*.id)) # 使用Terway时pod网络的vswitch地址段。
      master_instance_types = [data.alicloud_instance_types.default.0.instance_types.0.id, data.alicloud_instance_types.default.1.instance_types.0.id, data.alicloud_instance_types.default.2.instance_types.0.id] # 控制面节点的实例类型。
      master_disk_category  = "cloud_ssd"            # 控制面节点系统盘类型。
      password              = "Yourpassword1234"     # SSH登录密码。
      service_cidr          = "172.18.0.0/16"        # Service网络地址段。
      load_balancer_spec    = "slb.s1.small"         # 负载均衡规格。
      install_cloud_monitor = "true"                 # 安装云监控服务。
      resource_group_id     = data.alicloud_resource_manager_resource_groups.default.groups.0.id # 集群所属资源组ID,实现不同资源的隔离。
      deletion_protection   = "false"                # 集群删除保护,防止通过控制台或API误删除集群。
      timezone              = "Asia/Shanghai"        # 集群使用的时区。
      os_type               = "Linux"                # 操作系统平台类型。
      platform              = "AliyunLinux3"         # 操作系统发行版。
      cluster_domain        = "cluster.local"        # 集群本地域名。
      proxy_mode            = "ipvs"                 # kube-proxy代理模式。
      custom_san            = "www.terraform.io"     # 自定义证书SAN。
      new_nat_gateway       = "true"                 # 创建一个新的NAT网关。
      dynamic "addons" {
        for_each = var.cluster_addons
        content {
          name   = lookup(addons.value, "name", var.cluster_addons)
          config = lookup(addons.value, "config", var.cluster_addons)
        }
      }
    }

    关于更多的创建ACK专有集群配置参数信息,请参见alicloud_cs_kubernetes

  2. 执行以下命令,初始化Terraform运行环境。

    terraform init

    返回信息如下,Terraform初始化成功。

    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1...
    ...
    
    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: 7 to add, 0 to change, 0 to destroy.
    ...
  4. 执行以下命令,创建集群。

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

使用Terraform删除ACK专有集群

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

terraform destroy

返回信息如下,输入yes,按Enter键,集群删除成功。

...
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: 7 destroyed.

相关文档