创建一个高可用架构
更新时间 2025-12-19 14:25:32
最近更新时间: 2025-12-19 14:25:32
本文为您介绍如何使用模板创建一个高可用架构的操作场景及操作步骤。
操作场景
本示例以创建一个弹性负载均衡为例,介绍如何使用模板创建一个高可用架构。
不同架构内设云资源不同,本文及供参考。
操作步骤
登录控制中心。
在控制台首页搜索“资源编排ROS”,或在左侧产品导航栏选择“迁移与管理 > 管理工具 > 资源编排ROS”,进入资源编排控制台。
在左侧导航栏选择 模板管理。
在模板管理页面,单击创建模板。
创建高可用架构的.tf模板示例如下,请参考文档创建模板完成模板配置。
terraform {
required_providers {
ctyun = {
source = "ctyun-it/ctyun"
version = "1.2.0"
}
}
}
provider "ctyun" {
region_id = "bb9fdb42056f11eda1610242ac110002" //选定资源池
az_name = "cn-huadong1-jsnj1A-public-ctcloud" //选定可用区
}
variable "instance_name" { //定义变量:云主机名称
type = string
default = "tf-ecs-ha"
description = "Name of EC instances to create"
}
variable "vpc_name" { //定义变量:虚拟私有云名称
type = string
default = "tf-vpc-ha"
description = "Name of vpc to create"
}
variable "eip_name" { //定义变量:弹性IP名称
type = string
default = "tf-eip-ha"
description = "Name of eip to create"
}
variable "elb_name" { //定义变量:弹性负载均衡名称
type = string
default = "tf-elb-ha"
description = "Name of elb to create"
}
variable "instance_count" { //定义变量:创建弹性云主机数量,默认为1个
type = number
default = 1
description = "Number of EC instances to create"
}
variable "password" { //定义变量:云主机密码
type = string
sensitive = true
}
resource "ctyun_vpc" "vpc_test" { //定义vpc资源:vpc_test
name = var.vpc_name
cidr = "192.168.0.0/16"
description = "terraform测试使用"
enable_ipv6 = true
}
resource "ctyun_subnet" "subnet_test" { //定义子网资源:subnet_test
vpc_id = ctyun_vpc.vpc_test.id
name = "tf-vpc-ha"
cidr = "192.168.1.0/24"
description = "terraform测试使用"
dns = [
"114.114.114.114",
"8.8.8.8",
"8.8.4.4"
]
enable_ipv6 = true
}
resource "ctyun_eip" "eip_test" { //定义弹性IP资源:eip_test
name = var.eip_name
bandwidth = 1
cycle_type = "on_demand"
demand_billing_type = "bandwidth"
}
data "ctyun_zones" "test" {
}
locals {
az1 = data.ctyun_zones.test.zones[0]
az2 = data.ctyun_zones.test.zones[1]
}
output "az1" {
value = local.az1
}
data "ctyun_images" "image_test" { //确定云主机镜像
az_name = local.az1
name = "CTyunOS 22.06 64 位"
visibility = "public"
page_no = 1
page_size = 10
}
data "ctyun_ecs_flavors" "ecs_flavor_test" { //确定云主机参数
az_name = local.az1
cpu = 2
ram = 4
arch = "x86"
series = "C"
type = "CPU_C7"
}
resource "ctyun_ecs" "ecs_test" { //定义云主机资源:ecs_test
count = var.instance_count
instance_name = "${var.instance_name}-${count.index + 1}"
display_name = "${var.instance_name}-${count.index + 1}"
flavor_id = data.ctyun_ecs_flavors.ecs_flavor_test.flavors[0].id
image_id = data.ctyun_images.image_test.images[0].id
is_destroy_instance = true
system_disk_type = "sata"
system_disk_size = 40
vpc_id = ctyun_vpc.vpc_test.id
password = var.password
az_name = local.az1
cycle_type = "year"
cycle_count = 1
subnet_id = ctyun_subnet.subnet_test.id
}
# az2
data "ctyun_images" "image_test2" {
az_name = local.az2
name = "CentOS Linux 8.4"
visibility = "public"
page_no = 1
page_size = 10
}
data "ctyun_ecs_flavors" "ecs_flavor_test2" {
az_name = local.az2
cpu = 2
ram = 4
arch = "x86"
series = "C"
type = "CPU_C7"
}
resource "ctyun_ecs" "ecs_test2" { //定义云主机资源:ecs_test2
instance_name = "${var.instance_name}-2"
display_name = "${var.instance_name}-2"
flavor_id = data.ctyun_ecs_flavors.ecs_flavor_test2.flavors[0].id
image_id = data.ctyun_images.image_test2.images[0].id
is_destroy_instance = true
system_disk_type = "sata"
system_disk_size = 40
vpc_id = ctyun_vpc.vpc_test.id
password = var.password
az_name = local.az2
cycle_type = "year"
cycle_count = 1
subnet_id = ctyun_subnet.subnet_test.id
}
resource "ctyun_elb_loadbalancer" "test" { //定义弹性负载均衡资源:test
az_name = local.az1
subnet_id = ctyun_subnet.subnet_test.id
name = var.elb_name
sla_name = "elb.s2.small"
resource_type = "external"
vpc_id = ctyun_vpc.vpc_test.id
eip_id = ctyun_eip.eip_test.id
cycle_type = "on_demand"
}
resource "ctyun_elb_listener" "elb_listener_test" { //定义弹性负载均衡监听器资源:elb_listener_test
loadbalancer_id = ctyun_elb_loadbalancer.test.id
name = "${var.elb_name}-listerer"
protocol = "TCP"
protocol_port = 456
default_action_type = "forward"
target_groups = [{ target_group_id = ctyun_elb_target_group.target_group_test.id }]
}
resource "ctyun_elb_target_group" "target_group_test" { //定义负载均衡后端主机组
name = "${var.elb_name}-targer-group"
vpc_id = ctyun_vpc.vpc_test.id
algorithm = "wrr"
}在刚刚创建好的模板详情中,单击创建资源栈。
参考创建资源栈,使用当前模板完成资源栈创建。
需要注意的是,您需要在模板参数配置中,对模板中的变量进行赋值,变量值的校验规则您可以在模板中提前配置。
参考部署资源栈,完成资源栈部署。
部署完成后,您可前往对应资源控制台查看资源的创建结果。