使用Terraform管理已创建的集群

Terraform支持导入和管理ACK的存量资源,例如集群、节点池等。本文介绍如何通过Terraform管理存量ACK托管版集群。

前提条件

  • 已创建ACK托管版集群,其中包含一个有两个节点的节点池。具体操作,请参见创建ACK托管版集群

  • 已安装Terraform

    说明

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

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

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

  • 配置阿里云账号信息。

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

    • 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"         #替换为您集群所在的地域。
    说明

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

操作步骤

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

    provider "alicloud" {
    }
  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. 导入集群。

    1. 将集群的资源添加到main.tf文件中。

      # Kubernetes托管版。
      resource "alicloud_cs_managed_kubernetes" "default" {
      }
    2. 执行以下命令,导入集群。

      其中,<Cluster-ID>为待导入集群的ID。

      terraform import alicloud_cs_managed_kubernetes.default <Cluster-ID>

      预期输出:

      alicloud_cs_managed_kubernetes.default: Importing from ID "c338cf0f4496a4dc1936a9e314162****"...
      alicloud_cs_managed_kubernetes.default: Import complete!
        Imported alicloud_cs_managed_kubernetes
      alicloud_cs_managed_kubernetes.default: Refreshing state... [id=c338cf0f4496a4dc1936a9e314162****]
      
      Import successful!
      
      The resources that were imported are shown above. These resources are now in
      your Terraform state and will henceforth be managed by Terraform.

      此时,在terraform.tfstate文件中会显示类似如下导入的集群信息:

      {
            "mode": "managed",
            "type": "alicloud_cs_managed_kubernetes",
            "name": "default",
            "provider": "provider.alicloud",
            "instances": [
              {
                "mode": "managed",
                "type": "alicloud_cs_managed_kubernetes",
                "name": "default",
                "provider": "provider.alicloud",
                "instances": [
                   ........
                ]
              }
            ]
          }
    3. 执行如下命令,查看本地资源与集群的差异。

      terraform plan

      返回信息如下,请将如下代码中,状态即将变换的字段补充至main.tf文件中。

      alicloud_cs_managed_kubernetes.default: Refreshing state..
      erraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      -/+ destroy and then create replacement
      
      Terraform will perform the following actions:
      # 请将如下代码中,状态即将变换的字段补充至main.tf文件中。
        # alicloud_cs_managed_kubernetes.default must be replaced
      -/+ resource "alicloud_cs_managed_kubernetes" "default" {
            + availability_zone            = (known after apply)
            ~ certificate_authority        = {.....}
            ~ control_plane_log_ttl        = "30" -> (known after apply)
            ~ deletion_protection          = true -> false
            - enable_rrsa                  = false -> null
            ~ id                           = "cc7c582b0b2b546dcb80ae118eef0cb12" -> (known after apply)
            + install_cloud_monitor        = (known after apply)
            + is_enterprise_security_group = (known after apply)
            ~ name                         = "TFCESHI" -> (known after apply)
            + name_prefix                  = "Terraform-Creation"
            ~ nat_gateway_id               = "ngw-wz9njmq9pf8k9gj042vbi" -> (known after apply)
            + new_nat_gateway              = true
            ~ node_cidr_mask               = 25 -> 24 # forces replacement
            + node_port_range              = (known after apply)
            ~ platform                     = "CentOS" -> (known after apply)
            ~ resource_group_id            = "rg-acfmwqnwhqohesq" -> (known after apply)        }
          }
      
      Plan: 1 to add, 0 to change, 1 to destroy.
    4. 执行如下命令,将上一步中补充的字段导入至本地集群。

      terraform apply

      返回信息如下,字段导入成功。

      alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
      alicloud_cs_managed_kubernetes.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
        ~ update in-place
      
      Terraform will perform the following actions:
      
        ............
      
      
              # (2 unchanged blocks hidden)
          }
      Plan: 0 to add, 1 to change, 0 to destroy.
  4. 导入节点池。

    1. 将集群的节点资源添加到main.tf文件中。

      # Kubernetes托管版。
      resource "alicloud_cs_kubernetes_node_pool" "default" {
      }
    2. 执行以下命令,导入节点池。

      其中,<Cluster-ID>为待导入集群的ID,此处为上一步中导入集群的ID,<Nodepool-ID>为待导入节点池的ID,两者通过英文半角冒号(:)分隔。

      terraform import alicloud_cs_kubernetes_node_pool.default <Cluster-ID>:<Nodepool-ID>

      预期输出:

      alicloud_cs_kubernetes_node_pool.default: Importing from ID "c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****"...
      alicloud_cs_kubernetes_node_pool.default: Import complete!
        Imported alicloud_cs_kubernetes_node_pool
      alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****]
      
      Import successful!
      
      The resources that were imported are shown above. These resources are now in
      your Terraform state and will henceforth be managed by Terraform.

      此时,在terraform.tfstate文件中会显示如下导入的节点池信息:

      .....
      "resources": [
          {
            "mode": "managed",
            "type": "alicloud_cs_kubernetes_node_pool",
            "name": "default",
            "provider": "provider.alicloud",
            "instances": [
              .....
            ]
          }
        ]
    3. 执行如下命令,查看本地资源与集群的差异。

      terraform plan

      返回信息如下,请将如下代码中,状态即将变换的字段补充至main.tf文件中。

      alicloud_cs_managed_kubernetes.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12]
      alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
        ~ update in-place
      
      Terraform will perform the following actions:
      # 请将如下代码中,状态即将变换的字段补充至main.tf文件中。
        # alicloud_cs_kubernetes_node_pool.default will be updated in-place
        ~ resource "alicloud_cs_kubernetes_node_pool" "default" {
            ~ name                       = "default-nodepool" -> "default"
            ~ system_disk_size           = 120 -> 40
              tags                       = {}
              # (27 unchanged attributes hidden)
      
            - management {
                - auto_repair      = true -> null
                - auto_upgrade     = false -> null
                - max_unavailable  = 0 -> null
                - surge            = 0 -> null
                - surge_percentage = 0 -> null
              }
      
              # (1 unchanged block hidden)
          }
      
      Plan: 0 to add, 1 to change, 0 to destroy.
      
    4. 执行如下命令,将上一步中补充的字段导入至本地集群。

      terraform apply

      返回信息如下,字段导入成功。

      alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
      alicloud_cs_managed_kubernetes.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
        ~ update in-place
      
      Terraform will perform the following actions:
      
        ............
      
      
              # (2 unchanged blocks hidden)
          }
      Plan: 0 to add, 1 to change, 0 to destroy.

    集群导入完成后,您就可以通过main.tf文件对集群或节点池进行操作。

  5. 验证节点池的扩容操作。

    1. 通过main.tf验证节点池的扩容操作。

      比如给刚导入的节点池扩容1个节点,需要将main.tf文件修改为:

      ......
      
      # Kubernetes托管版。
      resource "alicloud_cs_kubernetes_node_pool" "default" {
        .....
        # 节点池期望节点数为3。
        desired_size = 3
      }
      
      .....
    2. 执行以下命令,完成变更操作。

      terraform apply

      返回信息如下,输入yes,按Enter键,等待变更结束。

      alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
      alicloud_cs_managed_kubernetes.default: Refreshing state... [id=cc7c582b0b2b546dcb80ae118eef0cb12]
      
      Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
        ~ update in-place
      
      Terraform will perform the following actions:
      
        # alicloud_cs_kubernetes_node_pool.default will be updated in-place
        ~ resource "alicloud_cs_kubernetes_node_pool" "default" {
            ~ desired_size               = 2 -> 3
              # (2 unchanged blocks hidden)
          }
      
      Plan: 0 to add, 1 to change, 0 to destroy.
      
      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_kubernetes_node_pool.default: Modifying... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
      alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009, 10s elapsed]
      alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009, 20s elapsed]
      alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009, 30s elapsed]
      alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009, 40s elapsed]
      alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009, 50s elapsed]
      alicloud_cs_kubernetes_node_pool.default: Modifications complete after 1m0s [id=cc7c582b0b2b546dcb80ae118eef0cb12:np651662dfc3e4440d9979360b24b1a009]
      
      Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

    您可以登录容器服务管理控制台节点池列表页面,查看到节点池中已成功扩容一个节点。