上云无忧 > 文档中心 > 腾讯云弹性微服务 TEM Terraform 管理 - 应用资源管理
弹性微服务 TEM
腾讯云弹性微服务 TEM Terraform 管理 - 应用资源管理

文档简介:
操作场景: 在弹性微服务中,为了帮助您聚焦设计与部署业务应用程序,省去对集群与服务器的考虑,抽象出了应用的概念。 本文介绍如何使用 Terraform 创建应用、部署和删除应用。
*此产品及展示信息均由腾讯云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

操作场景

在弹性微服务中,为了帮助您聚焦设计与部署业务应用程序,省去对集群与服务器的考虑,抽象出了应用的概念。 本文介绍如何使用 Terraform 创建应用、部署和删除应用。

前提条件

请参见 Terraform 快速开始 安装 Terraform 并配置腾讯云鉴权。

TEM应用资源

Terraform的tencentcloud_tem_application 资源提供了以下参数:
必填:application_name 应用名称。
必填:coding_language 应用的开发语言。
可选:repo_type 镜像仓库类型:“0”为个人版镜像仓库,“1”为企业版镜像仓库,“2”为公共镜像仓库,“4”为演示镜像。
可选:repo_name 镜像仓库名称。
可选:repo_server 镜像仓库地址。
可选:use_default_image_service 是否新建镜像仓库。 详细信息请参见 tencentcloud_tem_application

创建应用

此处以创建一个应用,并在广州地域下的 test_tf 环境中部署该应用为示例。
1. 创建 provider.tf 文件,指定 provider 配置信息。文件内容如下:
		
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
# 通过version指定版本
# version = ">=1.60.18"
}
}
}
provider "tencentcloud" {
region = "ap-guangzhou"
# secret_id = "my-secret-id"
# secret_key = "my-secret-key"
}
region:填写地域 ID。
获取凭证:在 API 密钥管理 面中创建并复制 SecretId 和 SecretKey。
2. 创建 main.tf 文件,配置腾讯云 Provider 并创建弹性微服务应用。文件内容如下:
		
resource "tencentcloud_tem_application" "application" {
application_name = "demo"
description = "demo for terraform"
coding_language = "JAVA"
repo_type = 2
repo_name = "qcloud/nginx"
repo_server = "ccr.ccs.tencentyun.com"
}
repo_type 选择为公共镜像仓库。
repo_namerepo_server 填写相关镜像仓库名称和地址。
3. 执行以下命令,初始化工作目录并下载插件。
		
terraform init
返回信息如下:
		
Initializing the backend...
Initializing provider plugins...
- Finding latest version of tencentcloudstack/tencentcloud...
- Installing tencentcloudstack/tencentcloud v1.77.4...
- Installed tencentcloudstack/tencentcloud v1.77.4 (signed by a HashiCorp partner, key ID 84F69E1C1BECF459)
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.
4. 执行以下命令,查看更新计划。

		

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:
# tencentcloud_tem_application.application will be created
+ resource "tencentcloud_tem_application" "application" {
+ application_name = "demo_tf"
+ coding_language = "JAVA"
+ description = "demo for terraform"
+ id = (known after apply)
+ repo_name = "qcloud/nginx"
+ repo_server = "ccr.ccs.tencentyun.com"
+ repo_type = 2
+ use_default_image_service = 0
}
Plan: 1 to add, 0 to change, 0 to destroy.

5. 执行以下命令,创建资源。
		
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:
# tencentcloud_tem_application.application will be created
+ resource "tencentcloud_tem_application" "application" {
+ application_name = "demo"
+ coding_language = "JAVA"
+ description = "demo for terraform"
+ id = (known after apply)
+ repo_name = "qcloud/nginx"
+ repo_server = "ccr.ccs.tencentyun.com"
+ repo_type = 2
+ use_default_image_service = 0
}
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
tencentcloud_tem_application.application: Creating...
tencentcloud_tem_application.application: Creation complete after 1s [id=app-vpzq42ep]

6. 执行完毕后,您可以在 弹性微服务控制台 应用列表中查看创建的应用。

部署应用

此步骤演示以拉取镜像的方式将上面创建的应用部署至 test-tf 环境中。
1. main.tf 文件,添加弹性微服务工作负载资源。内容如下:
		
resource "tencentcloud_tem_workload" "workload" {
application_id = "my-app-id"
environment_id = "my-env-id"
deploy_version = "1.9"
deploy_mode = "IMAGE"
img_repo = "qcloud/nginx"
init_pod_num = 1
cpu_spec = 1
memory_spec = 1
}
2. 执行 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:
# tencentcloud_tem_workload.workload will be created
+ resource "tencentcloud_tem_workload" "workload" {
+ application_id = "my-app-id"
+ cpu_spec = 1
+ deploy_mode = "IMAGE"
+ deploy_version = "1.9"
+ environment_id = "my-evn-id"
+ id = (known after apply)
+ img_repo = "qcloud/nginx"
+ init_pod_num = 1
+ memory_spec = 1
+ env_conf {
+ config = (known after apply)
+ key = (known after apply)
+ secret = (known after apply)
+ type = (known after apply)
+ value = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.

3. 执行 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:
# tencentcloud_tem_workload.workload will be created
+ resource "tencentcloud_tem_workload" "workload" {
+ application_id = "my-app-id"
+ cpu_spec = 1
+ deploy_mode = "IMAGE"
+ deploy_version = "1.9"
+ environment_id = "my-env-id"
+ id = (known after apply)
+ img_repo = "qcloud/nginx"
+ init_pod_num = 1
+ memory_spec = 1
+ env_conf {
+ config = (known after apply)
+ key = (known after apply)
+ secret = (known after apply)
+ type = (known after apply)
+ value = (known after apply)
}
}
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
tencentcloud_tem_workload.workload: Creating...
tencentcloud_tem_workload.workload: Creation complete after 6s [id=en-ejoqxm65#app-vpzq42ep]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

4. 执行完毕后,您可以在 弹性微服务控制台 应用列表中查看部署的应用。

销毁应用

您可以根据需求,执行以下命令销毁所创建和部署的资源。
		
terraform destroy
返回信息如下:
		
Terraform will perform the following actions:
# tencentcloud_tem_application.application will be destroyed
- resource "tencentcloud_tem_application" "application" {
- application_name = "demo" -> null
- coding_language = "JAVA" -> null
- description = "demo for terraform" -> null
- id = "my-app-id" -> null
- repo_name = "qcloud/nginx" -> null
- repo_server = "ccr.ccs.tencentyun.com" -> null
- repo_type = 2 -> null
- use_default_image_service = 0 -> null
}
# tencentcloud_tem_environment.environment will be destroyed
- resource "tencentcloud_tem_environment" "environment" {
- description = "demo for terraform" -> null
- environment_name = "test_tf" -> null
- id = "my-env-id" -> null
- subnet_ids = [
- "subnet-id1",
- "subnet-id2",
] -> null
- vpc = "vpc-id" -> null
}
# tencentcloud_tem_workload.workload will be destroyed
- resource "tencentcloud_tem_workload" "workload" {
- application_id = "my-app-id" -> null
- cpu_spec = 1 -> null
- deploy_mode = "IMAGE" -> null
- deploy_version = "1.9" -> null
- environment_id = "my-env-id" -> null
- id = "my-env-id#my-app-id" -> null
- img_repo = "qcloud/nginx" -> null
- init_pod_num = 1 -> null
- memory_spec = 1 -> null
- security_group_ids = [] -> null
相似文档
  • 操作场景: 若您在使用 Terraform 管理云资源之前,已经通过腾讯云控制台创建了资源,则可参考本文进行操作,将现存资源使用 Terraform 管理。
  • 弹性微服务支持哪些语言? 弹性微服务目前支持 Java 语言的微服务应用,针对非微服务应用,您可以通过制作 Docker 镜像并上传至弹性微服务,助您部署任何语言的应用。
  • 日志中出现乱码,如何解决? 问题描述: 部署至弹性微服务中的业务,如果在日志中显示乱码,一般是由于 locale 的错误配置导致。 遇到这种情况时,可以通过以下方案解决,设置与您应用部署地域相应的编码配置。
  • 为使用腾讯云微服务引擎-弹性微服务(以下简称“本服务”),您应当阅读并遵守《腾讯云微服务引擎-弹性微服务服务等级协议》(以下简称“本协议”或“SLA”),以及《腾讯云服务协议》。本协议包含本服务的术语和定义、服务可用性/服务成功率等级指标、赔偿方案、免责条款等相关内容。除非另有约定,本协议不适用于本服务正在内测的功能,腾讯云对本服务的内测功能不作可用性、服务质量等任何承诺。请您务必审慎阅读、充分理解各条款内容,限制、免责条款或者其他涉及您重大权益的条款可能会以加粗、加下划线等形式提示您重点注意。
  • API 网关(API Gateway)是 API 托管服务,提供 API 的完整生命周期管理,包括创建、维护、发布、运行、下线等。您可使用 API Gateway 封装自身业务,将您的数据、业务逻辑或功能安全可靠的开放出来,用以实现自身系统集成、以及与合作伙伴的业务连接。
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部