Terraformは、安全で効率的な方法でクラウドインフラストラクチャを事前設定および管理できるオープンソースツールです。 Terraformを使用してAlibaba Cloudリソースを管理できます。
前提条件
Terraformがインストールされています。
説明Terraform 0.12.28以降をインストールする必要があります。 terraform -- versionコマンドを実行して、Terraformバージョンを照会できます。
デフォルトでは、Cloud ShellにTerraformがプリインストールされ、アカウント情報が設定されています。 設定を変更する必要はありません。
Cloud Shell以外の方法でTerraformをインストールする方法の詳細については、「ローカルPCでのTerraformのインストールと構成」をご参照ください。
アカウント情報が設定されています。
次のコマンドを実行して、ID認証情報を保存する環境変数を作成します。
Linux環境
export ALICLOUD_ACCESS_KEY="************" # Replace the value with the AccessKey ID of your Alibaba Cloud account. export ALICLOUD_SECRET_KEY="************" # Replace the value with the AccessKey secret of your Alibaba Cloud account. export ALICLOUD_REGION="cn-beijing" # Replace the value with the ID of the region in which your cluster resides.
Windows環境
set ALICLOUD_ACCESS_KEY="************" # Replace the value with the AccessKey ID of your Alibaba Cloud account. set ALICLOUD_SECRET_KEY="************" # Replace the value with the AccessKey secret of your Alibaba Cloud account. set ALICLOUD_REGION="cn-beijing" # Replace the value with the ID of the region in which your cluster resides.
説明権限管理の柔軟性とセキュリティを向上させるために、Terraformという名前のResource Access management (RAM) ユーザーを作成することをお勧めします。 次に、RAMユーザーのAccessKeyペアを作成し、RAMユーザーに権限を付与します。 詳細については、「RAMユーザーの作成」および「RAMユーザーへの権限付与」をご参照ください。
Terraformを使用したACKサーバーレスクラスターの作成
という名前の作業ディレクトリとファイルを作成します。main.tfディレクトリにあります。
main.tfファイルには、次のTerraform設定が含まれています。
仮想プライベートクラウド (VPC) を作成し、VPCにvSwitchを作成します。
ACKサーバーレスクラスターを作成します。
provider "alicloud" { } variable "k8s_name_prefix" { description = "The name prefix used to create ASK cluster." default = "ask-example" } # The default resource names. locals { k8s_name_ask = substr(join("-", [var.k8s_name_prefix, "ask"]), 0, 63) new_vpc_name = "tf-vpc-172-16" new_vsw_name = "tf-vswitch-172-16-0" } data "alicloud_eci_zones" "default" {} 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_eci_zones.default.zones.0.zone_ids.1 } resource "alicloud_cs_serverless_kubernetes" "serverless" { name = local.k8s_name_ask version = "1.28.3-aliyun.1" # Replace the value with the version of the cluster that you want to create. cluster_spec = "ack.pro.small" vpc_id = alicloud_vpc.vpc.id vswitch_ids = split(",", join(",", alicloud_vswitch.vsw.*.id)) new_nat_gateway = true endpoint_public_access_enabled = true deletion_protection = false # Enable the RAM Roles for Service Accounts (RRSA) feature to configure service accounts. enable_rrsa = true load_balancer_spec = "slb.s2.small" time_zone = "Asia/Shanghai" service_cidr = "10.13.0.0/16" service_discovery_types = ["CoreDNS"] # tags tags = { "cluster" = "ack-serverless" } # addons addons { name = "nginx-ingress-controller" # Expose the cluster to the Internet config = "{\"IngressSlbNetworkType\":\"internet\",\"IngressSlbSpec\":\"slb.s2.small\"}" # To expose the cluster to the Internet, specify the following configuration: # config = "{\"IngressSlbNetworkType\":\"intranet\",\"IngressSlbSpec\":\"slb.s2.small\"}" } addons { name = "metrics-server" } addons { name = "knative" } addons { name = "managed-arms-prometheus" } addons { name = "logtail-ds" # Specify the name of a Simple Log Service project. # config = "{\"sls_project_name\":\"<YOUR-SLS-PROJECT-NAME>}\"}" } }
次のコマンドを実行して、Terraformランタイム環境を初期化します。
terraform init
次の情報が返されると、Terraformは初期化されます。
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.
次のコマンドを実行して、実行計画を作成します。
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: 3 to add, 0 to change, 0 to destroy. ...
次のコマンドを実行して、クラスターを作成します。
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_serverless_kubernetes.serverless: Creation complete after 8m26s [id=************] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Terraformを使用したACKサーバーレスクラスターの削除
次のコマンドを実行して、Terraformを使用して作成されたACKサーバーレスクラスターを削除します。
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: 3 destroyed.