一 背景
越来越多的公司将整个IT系统部署到各个云上,一个完整的IT 服务包含但不限于如下部分:
在公有云的环境中,我们一般如何快速交付 公司的 IT 基础设施?在云厂商提供的前端管理页面上手动操作?当IT基础设施越来越多、越来越复杂、以及公司为了满足多云架构,跨多个云厂商环境的时候,这些基础设施的配置和管理便会碰到一个巨大的挑战。
提前说一句,云厂商提供的 openapi 或者provider 接口不一致 也是灾难。。
为了解决上述问题,Terrafrom 应运而生。Terraform 官方的定义如下:
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.
The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.
翻译过来大致如下:
使用高级配置语法来描述基础架构,这样就可以对数据中心的蓝图进行版本控制,就像对待其他代码一样对待它。
Terraform 有一个 plan 步骤,它生成一个执行计划。执行计划显示了当执行 apply 命令时 Terraform 将做什么。通过 plan 进行提前检查,可以使 Terraform 操作真正的基础结构时避免意外。
Terraform 构建的所有资源的图表,它能够并行地创建和修改任何没有相互依赖的资源。因此,Terraform 可以高效地构建基础设施,操作人员也可以通过图表深入地解其基础设施中的依赖关系。
把复杂的变更集应用到基础设施中,而无需人工交互。通过前面提到的执行计划和资源图,我们可以确切地知道 Terraform 将会改变什么,以什么顺序改变,从而避免许多可能的人为错误。
我们仅仅只需编写简单的声明式代码,然后执行 Terraform 命令便可以轻松创建一个阿里云的数据库实例。
## 引用 provider
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.161.0"
}
}
}
provider "alicloud" {
# Configuration options
}
### 申请数据库实例资源 RDS MySQL 5.7 版本,2c4g ,50G存储
resource "alicloud_db_instance" "instance" {
engine = "MySQL"
engine_version = "5.7"
instance_type = "rds.mysql.s1.small"
instance_storage = "50"
...
}
基本逻辑是每个基础设施平台都会把对自身资源的操作统一封装打包成一个 provider, Terraform 在执行的过程中通过调用基础设施平台(各种云厂商)提供的 API 来实现各种云服务的申请,维护 等操作。
Terraform是 HashiCorp 旗下的一款开源的 DevOps 基础架构资源管理运维工具。从云的使用者的角度,它能非常方便快捷的操作 "云服务产品", 进一步提升用户使用云服务的效率。