背景:
通过terraform resource "alicloud_cs_managed_kubernetes" "k8s" {...}创建集群时,会产生一个默认的节点池default-nodepool,但是如何去修改这个默认节点池的信息呢?
解决思路:
因为Terraform的state文件中并没有default-nodepool,因此如果你直接去main.tf中创建节点池default-nodepool时,会导致你现有的阿里云上的节点池被覆盖,最好的方式是通过以下命令去将阿里云上的default-nodepool资源导入到Terraform的state中,然后在main.tf中在去新建节点池default-nodepool并修改你想修改的内容,就可以不覆盖即有内容了。
$ terraform import alicloud_cs_kubernetes_node_pool.default-nodepool cluster_id:nodepool_id
通过下面命令查看目前terraform state正在管理的内容:
terraform state list
输出,
data.alicloud_zones.availability_zones
data.kubernetes_secret.helm_secret
data.local_file.kube_config
alicloud_cs_kubernetes_node_pool.k8s_pool
alicloud_cs_managed_kubernetes.k8s
alicloud_key_pair.deployer
alicloud_security_group.default
alicloud_vswitch.vswitches[0]
alicloud_vswitch.vswitches[1]
alicloud_vswitch.vswitches[2]
kubernetes_cluster_role_binding.cluster_role_for_helm
kubernetes_namespace.external_secrets
kubernetes_secret.onepassword_connect_secret
kubernetes_secret.onepassword_connect_token_infrastructure
kubernetes_service_account.sa_for_helm
null_resource.aliyun
null_resource.kube_config
可以看出目前没有默认的节点池default-nodepool,
在使用terraform import命令时,要先在main.tf中创建如下对应的节点池但不需要运行,
resource "alicloud_cs_kubernetes_node_pool" "default-nodepool" {name = "default-nodepool"cluster_id = alicloud_cs_managed_kubernetes.k8s.idvswitch_ids = alicloud_vswitch.vswitches.*.idinstance_types = [var.csk_worker_instance_type]key_name = alicloud_key_pair.deployer.key_nameinstall_cloud_monitor = trueplatform = "AliyunLinux"image_id = "aliyun_2_1903_x64_20G_alibase_20230704.vhd"
}
然后运行
$ terraform import alicloud_cs_kubernetes_node_pool.default-nodepool cluster_id:nodepool_id
导入成功后,通过如下命令可以再次查看输出内容
terraform state list
data.alicloud_zones.availability_zones
data.kubernetes_secret.helm_secret
data.local_file.kube_config
alicloud_cs_kubernetes_node_pool.default-nodepool
alicloud_cs_kubernetes_node_pool.k8s_pool
alicloud_cs_managed_kubernetes.k8s
alicloud_key_pair.deployer
alicloud_security_group.default
alicloud_vpc.vpc
alicloud_vswitch.vswitches[0]
alicloud_vswitch.vswitches[1]
alicloud_vswitch.vswitches[2]
kubernetes_cluster_role_binding.cluster_role_for_helm
kubernetes_namespace.external_secrets
kubernetes_secret.onepassword_connect_secret
kubernetes_secret.onepassword_connect_token_infrastructure
kubernetes_service_account.sa_for_helm
null_resource.aliyun
null_resource.default_storage
null_resource.kube_config
可以看出alicloud_cs_kubernetes_node_pool.default-nodepool 这个节点池被从阿里云导入到terraform了,此时去修改platform和image_id,通过terraform run后,我们可以看出,不会去修改其它的节点内容了。
======================以下是引用=================================
使用Terraform管理已创建的集群 - 容器服务 Kubernetes 版 ACK - 阿里云
Terraform支持导入和管理ACK的存量资源,例如集群、节点池等。本文介绍如何通过Terraform管理存量ACK托管版集群。
前提条件
- 已创建ACK托管版集群,其中包含一个有两个节点的节点池。具体操作,请参见使用Terraform创建ACK托管版集群。
-
已安装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用户授权。
操作步骤
- 创建一个工作目录,并在工作目录中创建名为main.tf的配置文件。
provider "alicloud" { }
- 执行以下命令初始化Terraform运行环境。
terraform init
预期输出:
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.
- 导入集群。
- 将集群的资源添加到main.tf文件中。
# Kubernetes托管版。 resource "alicloud_cs_managed_kubernetes" "default" { }
- 执行以下命令,导入集群。其中,
<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": [........]}]}
- 根据terraform.tfstate文件的内容,补充main.tf的必填字段。
provider "alicloud" { }# 专有网络。 resource "alicloud_cs_managed_kubernetes" "default" {# 补充必填字段。worker_vswitch_ids = [.....]...... }
- 执行以下命令查看本地资源和远端的差异。
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.alicloud_cs_managed_kubernetes.default: Refreshing state... [id=c338cf0f4496a4dc1936a9e314162****]------------------------------------------------------------------------No changes. Infrastructure is up-to-date.This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions need to be performed.
- 将集群的资源添加到main.tf文件中。
- 导入节点池。
- 执行以下命令,导入节点池。其中,
<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": [.....]}]
- 根据terraform.tfstate文件的内容,补充main.tf的必填字段。
provider "alicloud" { }# 节点池。 resource "alicloud_cs_kubernetes_node_pool" "default" {# 节点池的名称。name = ....# 节点池的实例类型。instance_types = ......... }
- 执行以下命令查看本地资源和远端的差异。
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.alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****] alicloud_cs_managed_kubernetes.default: Refreshing state... [id=c338cf0f4496a4dc1936a9e314162****]------------------------------------------------------------------------No changes. Infrastructure is up-to-date.This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions need to be performed.
集群导入完成后,您就可以通过main.tf文件对集群或节点池进行操作。
- 执行以下命令,导入节点池。其中,
- 验证节点池的扩容操作。
- 通过main.tf验证节点池的扩容操作。比如给刚导入的节点池扩容1个节点,需要将main.tf文件修改为:
......# Kubernetes托管版。 resource "alicloud_cs_kubernetes_node_pool" "default" {.....# 节点池期望节点数为3。desired_size = 3 }.....
- 执行以下命令,完成创建操作。
terraform apply
预期输出:
alicloud_cs_kubernetes_node_pool.default: Refreshing state... [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****]An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols:~ update in-placeTerraform 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.....}Plan: 0 to add, 1 to change, 0 to destroy.
提示是否执行变更,输入yes,等待变更结束
-
..... alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****, 2m30s elapsed] alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****, 2m40s elapsed] alicloud_cs_kubernetes_node_pool.default: Still modifying... [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****, 2m50s elapsed] alicloud_cs_kubernetes_node_pool.default: Modifications complete after 2m53s [id=c338cf0f4496a4dc1936a9e314162****:np0f8f2193384045d4aa503c3d24ca****]Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
- 通过main.tf验证节点池的扩容操作。比如给刚导入的节点池扩容1个节点,需要将main.tf文件修改为: