You need to enable JavaScript to run this app.
导航

使用 Terraform 给集群添加已有节点

最近更新时间2024.03.27 11:42:38

首次发布时间2022.11.28 14:31:31

本文主要介绍使用 Terraform 为通过 Terraform 创建的集群,添加已有节点的操作。

前提条件

已通过 Terraform 创建集群。详细操作,请参见 管理使用 Terraform 创建的集群

创建默认节点池并添加已有节点

使用 Terraform 创建默认节点池,同时在默认节点池中添加已有节点时,请按照如下步骤操作。

  1. 创建一个文件夹,并在该文件夹中创建名为main.tf的配置文件。
    terraform {
      required_providers {
        volcengine = {
          source = "volcengine/volcengine"
          version = "0.0.140"  # version 信息请从 Terraform 官网(https://registry.terraform.io/providers/volcengine/volcengine/latest)获取。
        }
      }
    }
    
    provider "volcengine" {
      access_key = "**********"  # 火山引擎账号的 Access Key ID。
      secret_key = "**********"  # 火山引擎账号的 Secret Access Key。
      region = "cn-beijing"  # 容器服务业务所在的地域。
    }
    
    data "volcengine_vke_clusters" "default" {
      name_regex = "tf-created-vke"  # 通过 Terraform 创建的节点所属集群名称。
    }
    
    #创建默认节点池并添加已有节点
    resource "volcengine_vke_default_node_pool" "vke_test" {
      cluster_id = data.volcengine_vke_clusters.default.clusters[0].id
      node_config {
        security {
          login {
            password = "*****"  # 节点的访问方式,Root 用户登录密码。使用 Base64 编码格式。
          }
        }
      }
      instances {
        instance_id = "i-ybwgrtud6d********"  # 已有节点(云服务器实例)的 ID。
        keep_instance_name = true  # 是否保留云服务器实例名称。true:保留,false:云服务器实例被添加到集群后重新命名。
        additional_container_storage_enabled = true  # 是否将数据盘格式化并挂载到容器、镜像存储目录。true:是,false:否。
        container_storage_path = "/dev/vdb"  # 指定数据盘挂载目录。
      }
      instances {
        instance_id = "i-ybwgqqpjav********"
        keep_instance_name = true
        additional_container_storage_enabled = true
        container_storage_path = "/dev/vdb"
      }
    }
    
  2. 初始化 Terraform 运行环境。
    terraform init
    
    预期执行结果如下所示。
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding volcengine/volcengine versions matching "0.0.140"...
    - Installing volcengine/volcengine v0.0.140...
    
    ...
    
    Terraform has been successfully initialized!
    
    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
    
    预期执行结果如下所示。
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    ...
    Plan: 1 to add, 0 to change, 0 to destroy.
    
  4. 创建默认节点池并添加已有节点。
    terraform apply
    
    预期执行结果如下所示。
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    ...
    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
    
    volcengine_vke_default_node_pool.vke_test: Creating...
    ...
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    
    您也可以登录 容器服务控制台,查看名为vke-default-nodepool的默认节点池中已添加的节点。

从默认节点池中移除节点

使用 Terraform 从默认节点池中移除之前添加的已有节点时,请按照如下步骤操作。

注意

使用 Terraform 从默认节点池中移除已有节点时,不管节点的计费方式是包年包月还是按量计费,都不会释放该节点。如需释放节点,可通过云服务器控制台操作,操作指导,请参见 删除实例

  1. main.tf文件中删除 instances 字段内容。
    ...
      #instances {
      #  instance_id = "i-ybwgqqpjav********"
      #  keep_instance_name = true
      #  additional_container_storage_enabled = true
      #  container_storage_path = "/dev/vdb"
      #}
      ...
    
  2. 执行移除节点的命令。
    terraform apply
    
    预期执行结果如下所示。
    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:
    ...
    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
    ...
    Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
    
    您也可以登录 容器服务控制台,在目标集群的默认节点池中查看节点是否被移除。