如何利用Terraform工具编排管理TcaplusDB

时间:2022-07-22
本文章向大家介绍如何利用Terraform工具编排管理TcaplusDB,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.前言

Terraform是国外很流行的资源编排工具,具体介绍可查看Hashicorp官网。TIC是腾讯云基于Terraform打造一款免费服务,可为不同云平台的基础架构资源提供易用、高效、安全的统一资源编排管理平台,通过使用声明型语言,兼容众多优秀的开源社区工具,同时提供代码编辑和视图编辑两种模式,有效降低学习成本和使用难度。TIC 使用代码版本管理的方式管理基础架构,保障基础设施的构建、管理和迁移的可靠性和安全性。具体产品介绍可参考TIC官网。TcalplusDB是腾讯去推出的一款全托管NoSQL数据库服务,可为用户提供高性能、低成本、易扩展、稳定、安全的存储服务,目前广泛应用于王者荣耀、刺激战场、穿越火线、火影忍者等数百款流行游戏,具体产品能力介绍可参考另一篇文章《腾讯云TcaplusDB基础能力介绍》

本文主要介绍如何利用TIC进行TcaplusDB资源的编排调度。

2.环境准备

2.1 腾讯云环境

所有操作依赖于用户需要提前申请腾讯云账号,并创建申请一个API密钥。

2.2 TIC环境

腾讯云TIC入门可参考官方文档。腾讯云TIC控制台包括几个部分:

  • 资源栈: 同一基础架构的所有云资源的有机组合称之为资源栈,腾讯云 TIC 基于资源栈的维度全面管理基础架构,提供了资源栈的版本、资源和历史事件管理
  • 模板管理: 腾讯云 TIC 产品一共提供两种类型的模板管理,用户私有的模板和公共模板。用户可以创建自己的私有模板用以重复使用,也可以将公共模板保存为私有模板。公共模板为腾讯云认证的最佳实践,用户只需要修改简单的参数即可使用。
  • 资源管理: 资源列表罗列出当前腾讯云 TIC 支持的资源信息,包括参数使用限制、应用案例和对应的产品官网参考。

3. TIC实操

从TIC控制台创建TcaplusDB资源编排过程有几个步骤: 模板创建、资源栈创建、参数调整、编排前计划、编排执行等几个步骤。

3.1 模板准备

具体TIC中模板文件语法请参考Terraform官网下Providers中关于TencentCloud的TcpalusDB相关资源操作示例。这里以准备私有模板为例介绍下模板创建过程。

tic_temp.png

TcaplusDB模板文件分成三块:

  • main.tf: 管理所有的Resource资源,TcaplusDB的资源包括几块: VPC资源、子网资源、集群资源、表格组资源、IDL资源及表资源;
    • VPC资源: TcaplusDB部署在腾讯云VPC环境,需要在创建表之前创建好相应的VPC
    • 子网资源: 子网资源与VPC资源关联,用于表示TcaplusDB资源所属哪个子网
    • 集群资源: TcaplusDB表所在集群,类似于数据库概念
    • 表格组资源: 逻辑分组,类似游戏分区概念,如微信区、QQ区
    • IDL资源: 用于定义表结构
    • 表资源: 用于创建TcaplusDB具体的表
  • variables.tf: 管理所有变量,用于main.tf引用
  • game_players.proto: 默认的表结构定义文件,Google Protobuf3协议格式定义

具体模板文件内容如下:

  • main.tf
resource "tencentcloud_vpc" "tw_vpc" {
  name       = var.instance_name
  cidr_block = var.vpc_cidr
  description  = "TencentCloud VPC resoource"
}

resource "tencentcloud_subnet" "tw_sub" {
  name              = var.instance_name
  vpc_id            = tencentcloud_vpc.tw_vpc.id
  availability_zone = var.availability_zone
  cidr_block        = var.subnet_cidr
  is_multicast      = false
  description = "Subnet resource of VPC"
}


resource "tencentcloud_tcaplus_cluster" "test_cluster" {
  idl_type                 = var.cluster_idl
  cluster_name                 = var.cluster_name
  vpc_id                   = tencentcloud_vpc.tw_vpc.id
  subnet_id                = tencentcloud_subnet.tw_sub.id

  password                 = var.cluster_password
  old_password_expire_last = 3600
  description  = "TcaplusDB Cluster Resource"
}


resource "tencentcloud_tcaplus_tablegroup" "tablegroup" {
  cluster_id      = tencentcloud_tcaplus_cluster.test_cluster.id
  tablegroup_name = var.tablegroup_name
  description = "TcaplusDB TableGroup resource"
}

resource "tencentcloud_tcaplus_idl" "test_idl" {
  cluster_id        = tencentcloud_tcaplus_cluster.test_cluster.id
  tablegroup_id = tencentcloud_tcaplus_tablegroup.tablegroup.id

  file_name     = var.idl_file_name
  file_type     = var.idl_file_type
  file_ext_type = var.idl_file_ext_type
  file_content  = file(var.idl_temp_file)
  description = "TcaplusDB IDL resource"
}
resource "tencentcloud_tcaplus_table" "test_table" {
  cluster_id  = tencentcloud_tcaplus_cluster.test_cluster.id
  tablegroup_id = tencentcloud_tcaplus_tablegroup.tablegroup.id
  table_name         = var.table_name
  table_type         = var.table_type
  description        = var.table_desc
  idl_id             = tencentcloud_tcaplus_idl.test_idl.id
  table_idl_type     = var.table_idl
  reserved_read_cu  = var.reserved_read_cu
  reserved_write_cu = var.reserved_write_cu
  reserved_volume    = var.reserved_volume
  description  = "TcaplusDB table resource"
}
  • variables.tf
variable "region" {
  default = "ap-shanghai"
}
variable "availability_zone" {
  default = "ap-shanghai-1"
}
variable "instance_name" {
  default = "vpc_test"
}
variable "vpc_cidr" {
  default = "172.18.0.0/16"
}
variable "subnet_cidr" {
  default = "172.18.0.0/24"
}

variable "cluster_idl" {
  default = "PROTO"
}
variable "cluster_name" {
  default = "tw_tcaplus_cluster"
}
variable "cluster_password" {
  default = "TcaplusDB2020"
}
variable "tablegroup_name" {
  default = "tw_tcaplus_tb_1"
}

variable "idl_file_name" {
  default = "game_players"
}

variable "idl_file_type" {
  default = "PROTO"
}
variable "idl_file_ext_type" {
  default = "proto"
}

variable "idl_temp_file" {
  default = "game_players.proto"
}
variable "table_name" {
  default = "game_players"
}
variable "table_type" {
  default = "GENERIC"
}
variable "table_desc" {
  default = "tic_test"
}

variable "table_idl" {
  default = "PROTO"
}

variable "reserved_read_cu" {
  default = "80"
}
variable "reserved_write_cu" {
  default = "20"
}
variable "reserved_volume" {
  default = "1"
}
  • game_players.proto
syntax = "proto3";                      // Specify the version of the protocol buffers language


import "tcaplusservice.optionv1.proto"; // Use the public definitions of TcaplusDB by importing them.

message game_players {  // Define a TcaplusDB table with message

	// Specify the primary keys with the option tcaplusservice.tcaplus_primary_key
	// The primary key of a TcaplusDB table has a limit of 4 fields
    option(tcaplusservice.tcaplus_primary_key) = "player_id, player_name, player_email";

    // Specify the primary key indexes with the option tcaplusservice.tcaplus_index
    option(tcaplusservice.tcaplus_index) = "index_1(player_id, player_name)";
    option(tcaplusservice.tcaplus_index) = "index_2(player_id, player_email)";


    // Value Types supported by TcaplusDB
    // int32, int64, uint32, uint64, sint32, sint64, bool, fixed64, sfixed64, double, fixed32, sfixed32, float, string, bytes
    // Nested Types Message

    // primary key fields
    int64 player_id = 1;
    string player_name = 2;
    string player_email = 3;


    // Ordinary fields
    int32 game_server_id = 4;
    repeated string login_timestamp = 5;
    repeated string logout_timestamp = 6;
    bool is_online = 7;

    payment pay = 8;
}


message payment {

	int64 pay_id = 1;
	uint64 amount = 2;
    int64 method = 3;

}

3.2 资源栈准备

在创建好模板好,开始进行实际的资源创建动作。

3.2.1 创建资源栈

tic_resource_stack.png

注意:

  • 地域选择: 这个和模板中定义的地域变量所填的值要保持一致
  • 指定模板: 这里选择刚创建好的私有模板

3.2.2 参数调整

选择模板后点击下一步会显示模板,此步需要调整下模板参数,如地域、可用区、VPC和子网CIDR。

tic_param_config.png

3.2.3 编排计划

调整完参数后,点击下一步,会自动进入编排计划,此步会检查所创建资源的语法是否OK,如果没问题, 会显示模板需要增加的资源数,并显示Plan为Finish状态。

tic_plan.png

3.2.4 编排应用

Plan执行完后,点击下一步会进入Apply步骤,此步会要求输入资源栈名称:

tic_apply.png

点击确认后,会生成Apply事件,正在执行中的事件状态为APPLY_IN_PROGRESS,如下图所示:

tic_event.png

执行完后,只要整个过程没有异常,事件状态会变为APPLY_COMPLETED,点击事件详情可以看到具体的执行过程,如下图所示:

tic_apply_result.png

整个Apply过程会根据Plan中编排的资源列表按顺序进行一一创建,如果这中间有任何异常,都会在执行结果中显示具体的异常错误码,可根据具体描述来相应处理。

3.2.5 查看TcaplusDB创建结果

根据Apply的结果,接着打开TcaplusDB控制台,看看模板中所定义的上海地域(ap-shanghai)创建的名称为tw_tcaplus_cluster的集群,如下图所示:

tic_tcaplus_result.png

在表格组中查看表格详情,如下所示,创建了一个模板所定义的名为game_players的TcaplusDB表,表的初始参数和模板保持一致(读写CU、容量)。

tic_tcaplus_table.png

3.3 资源栈销毁

针对所创建的资源进行销毁,TIC支持一键销毁,方便用户统一进行资源的管理。在资源栈列表页面找到对应需要销毁的资源栈,选中后点击销毁即可。

tic_stack_destroy.png

对于已经销毁的资源栈,还支持在原有版本上创建新的版本便于用户以同一个资源栈创建同样的资源。如下所示:

tic_stack_version.png

4. 总结

本文介绍了如何利用TIC工具来进行TcaplusDB资源的创建与销毁,基于Terraform便利的编排调度机制方便用户快速创建或销毁业务所需资源,避免人工进行资源管理。