使用Terraform在Elastic Cloud上构建Elasticsearch的方法
本文将逐步说明使用Terraform在Elastic Cloud上构建Elasticsearch的步骤。即使是从未使用过Elastic Cloud的人,也可以通过本文来构建Elasticsearch。
验证环境
-
- M2 MacBook Air
-
- Terraform version: v1.1.7 on darwin_arm64
terraform-provider-ec: v0.4.1
在Elastic Cloud上注册。
首先,我们将解释如何注册Elastic Cloud。如果您已经对Elastic Cloud有所了解,可以继续使用Terraform创建新的部署,不需要重复注册。
只要拥有Google账号或Microsoft账号,您就可以立即注册Elastic Cloud。此外,Elastic Cloud提供14天的免费试用期,我们可以利用这个试用期。
用Terraform创建一个新的部署。
下一步我们将使用Terraform而不是从Elastic Cloud界面开始创建部署。
创建API密钥
首先,我们要创建一个API密钥。点击Elastic Cloud主界面上的”专用部署”。
设置API密钥
有两种选择来设置API密钥,一种是将其作为环境变量设置,另一种是在Terraform的代码中进行描述。
export EC_API_KEY="<apikey>"
provider "ec" {
apikey = "<apikey>"
}
本次我们将在Terraform的代码中写入API密钥。
写代码
然后,我们来编写Terraform的代码。创建main.tf文件并填写以下代码。
provider "ec" {
apikey = "xxx"
}
terraform {
required_providers {
ec = {
source = "elastic/ec"
version = "0.4.1"
}
}
}
resource "ec_deployment" "test_cluster" {
region = "ap-southeast-1"
version = "8.9.1"
deployment_template_id = "aws-cpu-optimized-arm"
name = "test cluster"
elasticsearch {
autoscale = "false"
topology {
id="hot_content"
size="1g"
size_resource="memory"
zone_count=1
}
}
kibana {
topology {
size="1g"
size_resource="memory"
zone_count=1
}
}
}
填写完上述内容后,执行terraform init。后续我们将简写terraform为tf。
$ tf init ─╯
Initializing the backend...
Initializing provider plugins...
- Finding elastic/ec versions matching "0.4.1"...
- Installing elastic/ec v0.4.1...
- Installed elastic/ec v0.4.1 (signed by a HashiCorp partner, key ID 7FE579EDEC6DAA7B)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
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.
接下来,我们使用tf plan来确认将在Elasti Cloud上创建的资源。
$ tf 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:
# ec_deployment.test_cluster will be created
+ resource "ec_deployment" "test_cluster" {
+ alias = (known after apply)
+ apm_secret_token = (sensitive value)
+ deployment_template_id = "aws-cpu-optimized-arm"
+ elasticsearch_password = (sensitive value)
+ elasticsearch_username = (known after apply)
+ id = (known after apply)
+ name = "test cluster"
+ region = "ap-southeast-1"
+ version = "8.9.1"
+ elasticsearch {
+ autoscale = "false"
+ cloud_id = (known after apply)
+ http_endpoint = (known after apply)
+ https_endpoint = (known after apply)
+ ref_id = "main-elasticsearch"
+ region = (known after apply)
+ resource_id = (known after apply)
+ topology {
+ config = (known after apply)
+ id = "hot_content"
+ instance_configuration_id = (known after apply)
+ node_roles = (known after apply)
+ node_type_data = (known after apply)
+ node_type_ingest = (known after apply)
+ node_type_master = (known after apply)
+ node_type_ml = (known after apply)
+ size = "1g"
+ size_resource = "memory"
+ zone_count = 1
+ autoscaling {
+ max_size = (known after apply)
+ max_size_resource = (known after apply)
+ min_size = (known after apply)
+ min_size_resource = (known after apply)
+ policy_override_json = (known after apply)
}
}
+ trust_account {
+ account_id = (known after apply)
+ trust_all = (known after apply)
+ trust_allowlist = (known after apply)
}
+ trust_external {
+ relationship_id = (known after apply)
+ trust_all = (known after apply)
+ trust_allowlist = (known after apply)
}
}
+ kibana {
+ elasticsearch_cluster_ref_id = "main-elasticsearch"
+ http_endpoint = (known after apply)
+ https_endpoint = (known after apply)
+ ref_id = "main-kibana"
+ region = (known after apply)
+ resource_id = (known after apply)
+ topology {
+ instance_configuration_id = (known after apply)
+ size = "1g"
+ size_resource = "memory"
+ zone_count = 1
}
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
如果输出结果没有特别的问题,可以使用tf apply创建部署。
$ tf apply
...(terraform plan実行時の差分が出る)
Plan: 1 to add, 0 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。
Plan: 1 to add, 0 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
ec_deployment.test_cluster: Creating...
ec_deployment.test_cluster: Still creating... [10s elapsed]
ec_deployment.test_cluster: Still creating... [20s elapsed]
ec_deployment.test_cluster: Still creating... [30s elapsed]
ec_deployment.test_cluster: Still creating... [40s elapsed]
ec_deployment.test_cluster: Still creating... [50s elapsed]
最后
本文介绍了使用Terraform在Elasti Cloud上构建Elasticsearch的方法。由于仍处于构建阶段,我希望能更多地了解Elasticsearch并在文章中能够描述出来。
文献引用