同时用腾讯云和阿里云,资源管理一团糟?3 个主流多云编排方案深度对比,附 Terraform 多云实战代码。
多云管理的痛点:为什么不用一个云就够了?
现实中,大多数企业都不是”一朵云走天下”:
- 合规要求:金融行业要求主备双云
- 性价比:不同云的不同产品各有优势(腾讯云 CDN 便宜、阿里云数据库强)
- 历史包袱:早期业务在 A 云,新业务想用 B 云
- 防绑定:不被单一厂商锁死
结果就是:一个人管 3 个云控制台,每天切换账号配资源,效率极低。
多云编排工具就是解决这个问题的。今天对比 3 个主流方案:
一、三大方案快速对比
| 维度 | Terraform + Cloud | 腾讯云编排(TAT) | 阿里云 ROS |
|---|---|---|---|
| 支持云厂商 | 3000+ Provider(全覆盖) | 仅腾讯云 | 仅阿里云 |
| 声明式语法 | HCL(HashiCorp 配置语言) | YAML | ROS 模板(JSON/YAML) |
| 状态管理 | Terraform State(远程锁定) | 无状态,直接执行 | ROS Stack 管理 |
| 版本控制 | Git + Terraform Cloud 原生支持 | 需自行集成 | 支持仓库关联 |
| 学习曲线 | 中(HCL 需要学习) | 低(YAML 语法简单) | 低 |
| 免费额度 | Terraform CLI 免费,Cloud 付费 | 免费 | 基础免费 |
| 团队协作 | Sentinel 策略 + RBAC | CAM 权限控制 | RAM 权限控制 |
| 社区生态 | 极强(HashiCorp 生态) | 弱(仅腾讯云) | 弱(仅阿里云) |
结论:真正做多云管理,Terraform 是唯一成熟的选择。单云场景可以用自家编排服务。
二、Terraform 多云实战:一套代码管腾讯云 + 阿里云
2.1 项目结构
multi-cloud-infra/
├── main.tf # 入口文件
├── providers.tf # 云厂商 Provider 配置
├── variables.tf # 变量定义
├── terraform.tfvars # 变量值(敏感信息,不提交 Git)
├── modules/
│ ├── tencent-cvm/ # 腾讯云 CVM 模块
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ └── aliyun-ecs/ # 阿里云 ECS 模块
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
└── environments/
├── dev.tfvars # 开发环境
└── prod.tfvars # 生产环境
2.2 Provider 配置
# providers.tf - 多云 Provider 配置
terraform {
required_version = ">= 1.5.0"
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
version = "~> 1.81"
}
alicloud = {
source = "aliyun/alicloud"
version = "~> 1.215"
}
}
# 远程状态管理(推荐用腾讯云 COS 或阿里云 OSS)
backend "cos" {
region = "ap-guangzhou"
bucket = "tf-state-1234567890"
key = "multi-cloud/terraform.tfstate"
secret_id = "YOUR_SECRET_ID"
secret_key = "YOUR_SECRET_KEY"
}
}
# 腾讯云 Provider
provider "tencentcloud" {
region = var.tencent_region
secret_id = var.tencent_secret_id
secret_key = var.tencent_secret_key
}
# 阿里云 Provider
provider "alicloud" {
region = var.aliyun_region
access_key = var.aliyun_access_key
secret_key = var.aliyun_secret_key
}
2.3 变量定义
# variables.tf
variable "tencent_region" {
description = "腾讯云区域"
default = "ap-guangzhou"
}
variable "aliyun_region" {
description = "阿里云区域"
default = "cn-hangzhou"
}
variable "tencent_secret_id" {
description = "腾讯云 SecretId"
sensitive = true
}
variable "tencent_secret_key" {
description = "腾讯云 SecretKey"
sensitive = true
}
variable "aliyun_access_key" {
description = "阿里云 AccessKey"
sensitive = true
}
variable "aliyun_secret_key" {
description = "阿里云 SecretKey"
sensitive = true
}
variable "env" {
description = "环境标识"
default = "dev"
}
variable "instance_type" {
description = "实例规格"
default = "2核4G"
}
2.4 腾讯云 CVM 模块
# modules/tencent-cvm/main.tf
resource "tencentcloud_vpc" "main" {
name = "${var.env}-vpc"
cidr_block = "10.0.0.0/16"
}
resource "tencentcloud_subnet" "web" {
name = "${var.env}-subnet-web"
vpc_id = tencentcloud_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = var.availability_zone
}
resource "tencentcloud_security_group" "web" {
name = "${var.env}-sg-web"
}
resource "tencentcloud_security_group_rule" "allow_ssh" {
security_group_id = tencentcloud_security_group.web.id
type = "ingress"
cidr_ip = "0.0.0.0/0"
ip_protocol = "tcp"
port_range = "22"
policy = "accept"
}
resource "tencentcloud_security_group_rule" "allow_http" {
security_group_id = tencentcloud_security_group.web.id
type = "ingress"
cidr_ip = "0.0.0.0/0"
ip_protocol = "tcp"
port_range = "80,443"
policy = "accept"
}
resource "tencentcloud_instance" "web" {
instance_name = "${var.env}-web-server"
availability_zone = var.availability_zone
image_id = "img-8toqc6s3" # Ubuntu 22.04 公共镜像
instance_type = var.instance_type
system_disk_type = "CLOUD_PREMIUM"
system_disk_size = 50
vpc_id = tencentcloud_vpc.main.id
subnet_id = tencentcloud_subnet.web.id
security_groups = [tencentcloud_security_group.web.id]
tags = {
Env = var.env
Project = "multi-cloud-demo"
Managed = "terraform"
}
}
2.5 阿里云 ECS 模块
# modules/aliyun-ecs/main.tf
resource "alicloud_vpc" "main" {
name = "${var.env}-vpc"
cidr_block = "172.16.0.0/16"
}
resource "alicloud_vswitch" "web" {
name = "${var.env}-vswitch-web"
vpc_id = alicloud_vpc.main.id
cidr_block = "172.16.1.0/24"
zone_id = var.zone_id
}
resource "alicloud_security_group" "web" {
name = "${var.env}-sg-web"
vpc_id = alicloud_vpc.main.id
}
resource "alicloud_security_group_rule" "allow_ssh" {
type = "ingress"
ip_protocol = "tcp"
port_range = "22/22"
source_cidr_ip = "0.0.0.0/0"
security_group_id = alicloud_security_group.web.id
}
resource "alicloud_security_group_rule" "allow_http" {
type = "ingress"
ip_protocol = "tcp"
port_range = "80/80,443/443"
source_cidr_ip = "0.0.0.0/0"
security_group_id = alicloud_security_group.web.id
}
resource "alicloud_instance" "web" {
instance_name = "${var.env}-web-server"
image_id = "ubuntu_22_04_x64_20G_alibase_20240624.vhd"
instance_type = var.instance_type
system_disk_category = "cloud_essd"
system_disk_size = 50
vswitch_id = alicloud_vswitch.web.id
security_groups = [alicloud_security_group.web.id]
tags = {
Env = var.env
Project = "multi-cloud-demo"
Managed = "terraform"
}
}
2.6 入口文件
# main.tf
module "tencent_web" {
source = "./modules/tencent-cvm"
env = var.env
availability_zone = "ap-guangzhou-3"
instance_type = "SA2.MEDIUM4" # 2核4G
}
module "aliyun_web" {
source = "./modules/aliyun-ecs"
env = var.env
zone_id = "cn-hangzhou-i"
instance_type = "ecs.c6.large" # 2核4G
}
# 输出资源信息
output "tencent_public_ip" {
value = module.tencent_web.public_ip
}
output "aliyun_public_ip" {
value = module.aliyun_web.public_ip
}
三、日常运维:5 个常用 Terraform 命令
# 1. 初始化(下载 Provider)
terraform init
# 2. 预览变更(只看不执行,务必先跑这个!)
terraform plan -var-file="environments/dev.tfvars"
# 3. 执行变更
terraform apply -var-file="environments/dev.tfvars"
# 4. 查看当前资源状态
terraform show
# 5. 销毁所有资源(慎用!)
terraform destroy -var-file="environments/dev.tfvars"
四、多云管理最佳实践
4.1 成本对比
| 云厂商 | 2核4G(通用型)月费 | 对象存储 | CDN |
|---|---|---|---|
| 腾讯云 | ¥56/月 | ¥0.099/GB/月 | ¥0.15/万次 |
| 阿里云 | ¥68/月 | ¥0.12/GB/月 | ¥0.24/万次 |
| 华为云 | ¥58/月 | ¥0.099/GB/月 | ¥0.20/万次 |
以上为 2026 年 5 月华南区参考价,实际以官网为准。
4.2 5 条避坑建议
- State 文件必须远程存储。用 COS 或 OSS,千万别放本地,丢了就全完了
- 敏感信息用 tfvars + .gitignore。SecretId/SecretKey 绝不能提交到 Git
- 先用 plan 再 apply。养成习惯,避免误删生产资源
- 模块化拆分。不同云的模块独立维护,方便团队协作
- 打标签 Tag。所有资源统一打标签,方便账单分析和资源盘点
👤 作者简介
一枚在大中原腹地(河南)卖公有云的从业者,主营腾讯云/阿里云/火山云,曾踩坑无数,现专注AI大模型应用落地。关注公众号「公有云cloud」,围观AI前沿动态~
博客:y1cloud.cn
