Terraform

Terraform (一):基础介绍

2020-03-09  本文已影响0人  WanjinYoung

参考文档:阿里云栖社区
官方文档:Terraform官网

一、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 就是运行在客户端的一个开源的,用于资源编排的自动化运维工具。以代码的形式将所要管理的资源定义在模板中,通过解析并执行模板来自动化完成所定义资源的创建,变更和管理,进而达到自动化运维的目标。

二、Terraform 特点

1.基础设施即代码(IaC, Infrastructure as Code)

Terraform 基于一种特定的配置语言(HCL, Hashicorp Configuration Language)来描述基础设施资源。由此,可以像对待任何其他代码一样,实现对所描述的解决方案或者基础架构的版本控制和管理。同时,通用的解决方案和基础架构可以以模板的形式进行便捷的共享和重用。

resource "resourcecenter_fw_policy_v1" "policy" {
  name = "polices-ltl"
  description = "test polices create"
  firewall_rules = ["ade65679-7d15-4d35-90b1-d9f21a6e8efd"]
  vpc_id = "fd93d0e0-dfb3-4d8b-bc5a-0777779736e1"
  tags {
    key = "k-1"
    value = "v-1"
  }
  tags {
    key = "k-2"
    value = "v-2"
  }
}
2.执行计划(Execution Plans)

Terraform 在执行模板前,运行 terraform plan 命令会先通过解析模板生成一个可执行的计划,这个计划展示了当前模板所要创建或变更的资源及其属性。操作人员可以预览这个计划,在确认无误后执行 terraform apply 命令,即可完成对所定义资源的快速创建和变更,以免发生一些超预期的问题。

------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.subnet.resourcecenter_networking_subnet_v1.subnet_provider_test_1[0] will be created
  + resource "resourcecenter_networking_subnet_v1" "subnet_provider_test_1" {
      + alias             = "provide test"
      + cidr              = "192.168.102.0/24"
      + description       = "create des 1"
      + gateway           = (known after apply)
      + id                = (known after apply)
      + ip_subnet_pool_id = "1fb148e2-083e-43ef-b61f-25ee91744c26"
      + name              = "provider-subnet-test-1"
      + resource_zone_id  = "cc5dcd1c-0db4-46a0-bca3-4fd7118abeb4"
      + type              = "OPS"
      + vpc_id            = "d4d61bbf-2304-40e2-8e5e-5e0a3c0f07fa"

      + allocation_pools {
          + end   = (known after apply)
          + start = (known after apply)
        }

      + tags {
          + key   = "key-1"
          + value = "value-1"
        }
    }

Plan: 1 to add, 0 to change, 0 to destroy.

------------------------------------------------------------------------

3.资源拓扑图(Resource Graph)

Terraform 会根据模板中的定义,构建所有资源的图形,并且以并行的方式创建和修改那些没有任何依赖资源的资源,以保证执行的高效性。对于有依赖资源的资源,被依赖的资源优先执行。

4.自动化变更(Change Automation)

不论多复杂的资源,当模板中定义的资源内容发生变更时,Terraform 都会基于新的资源拓扑图将变更的内容plan 出来,在确认无误后,只需一个命令即可完成数个变更操作,避免了人为操作带来的错误。

三、基本实现

每个Resource需要实现Create,Read,Update,Delete,Import(optional)五个功能:

调用资源创建API,实现对某个资源的创建,并将resource id 写入到state文件中
如果资源没有ID,比如安全组规则,那么resource要自己按照一定的规则自行实现一个ID,对terraform而言,该ID是resource的唯一标识,后续对resource的所有操作都需要依赖于该ID进行
Create 的实现逻辑要尽可能简单,以成功创建资源为目标,太多的复杂逻辑会降低资源创建的成功率
复杂的功能实现可交由 Update 来完成
Create之后通常调用Update方法来完成更多功能的实现

调用资源的Update(更新)或Modify(修改) API,实现对resource更多功能的支持以及对已有属性的修改。
Update之前要打开Partial d.Partial(true)(可选),并在每步修改后进行时时更新,如d.SetPartial("bandwidth"),以保证后面的每一步成功的更改都能被及时的写入到state 文件中。
Update 之后,调用 Read 方法,实现对resource 所有属性的展示,同步到state文件。

调用 Describe 或者 List API,实现对已有资源的查询和并将结果写入到state
如果找不到指定的资源,要将resource ID标记为“”,以告诉terraform,当前这个资源已经不存在了。

调用资源Delete API实现对指定资源的删除和释放
为了保证resource成功释放,有时需要加入retry策略来避免因资源依赖或者异步操作而引起的删除失败问题
通常,resource释放后要再次调用查询API来确保完成删除操作的验证

调用查询API实现对已有资源的导入
通常只需要方法申明,无需多余的实现逻辑,它会借助Read方法来完成对资源的查询和导入


Ref :
reference1
reference2

上一篇 下一篇

猜你喜欢

热点阅读