Terraform

Terraform 是 Hashicorp 公司开源的一种多云资源编排工具。使用者通过一种特定的配置语言(HCL, Hashicorp Configuration Language)来描述基础设施,由 Terraform 工具统一解析,构建资源之间的关系,生成执行计划,并通过调用各家云厂商的具体实现来完成整个基础设施生命周期的管理。

本章将主要介绍Terraform的安装及初始环境配置。

 Terraform安装

可以到如下网站进行下载,选择所需要的二进制版本,支持 Linux、Windows、macOS 及 BSD,目前最新版本为 v0.10.8。
https://www.terraform.io/downloads.html
需要基于源码编译安装的可以到 github下载。
https://github.com/hashicorp/terraform
将安装包解压后,会得到名为terraform的可执行文件,直接更新PATH环境变量即可运行。

安装 Terraform,找到与你系统 匹配的软件包 然后下载。Terraform 被打包为一个 zip 归档文件。

下载完 zip 文件以后,解压这个包。Terraform 是一个名为 terraform 的独立文件。包里其他所有的文件都可以安全删掉,Terraform 依然可以正常工作。

最后一步确保 terraform 二进制文件在 PATH 上可用。 

# copy binary file to path
mv ~/Downloads/terraform /usr/local/bin/

# macOS can use Homebrew to install terraform
brew install hashicorp/tap/terraform

# terraform
Usage: terraform [-version] [-help] <command> [args]

The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.

Common commands:
    apply              Builds or changes infrastructure
    console            Interactive console for Terraform interpolations
    destroy            Destroy Terraform-managed infrastructure
    env                Workspace management
    fmt                Rewrites config files to canonical format
    get                Download and install modules for the configuration
    graph              Create a visual graph of Terraform resources
    import             Import existing infrastructure into Terraform
    init               Initialize a Terraform working directory
    login              Obtain and save credentials for a remote host
    logout             Remove locally-stored credentials for a remote host
    output             Read an output from a state file
    plan               Generate and show an execution plan
    providers          Prints a tree of the providers used in the configuration
    refresh            Update local state file against real resources
    show               Inspect Terraform state or plan
    taint              Manually mark a resource for recreation
    untaint            Manually unmark a resource as tainted
    validate           Validates the Terraform files
    version            Prints the Terraform version
    workspace          Workspace management

All other commands:
    0.12upgrade        Rewrites pre-0.12 module source code for v0.12
    0.13upgrade        Rewrites pre-0.13 module source code for v0.13
    debug              Debug output management (experimental)
    force-unlock       Manually unlock the terraform state
    push               Obsolete command for Terraform Enterprise legacy (v1)
    state              Advanced state management

配置介绍

Terraform使用文本文件来描述基础设施和设置变量。这些文件称为 Terraform 配置,并以 .tf 结尾。本节介绍这些文件的格式以及它们的加载方式。

配置文件的格式可以有两种格式:Terraform 格式和JSON。Terraform 格式更加人性化,支持注释,并且是大多数 Terraform 文件通常推荐的格式。JSON格式适用于机器创建,修改和更新,也可以由Terraform操作员完成。Terraform格式后缀名以.tf结尾,JSON格式后缀名以.tf.json结尾。详细说明请参考如下链接:

https://www.terraform.io/docs/configuration/index.html

加载顺序与语义

在调用加载Terraform配置的任何命令时,Terraform将按字母顺序加载指定目录中的所有配置文件。

加载文件的后缀名必须是.tf或.tf.json。否则,文件将被忽略。多个文件可位于同一目录中。Terraform配置文件可以一个是Terraform 语法,另一个是JSON格式。

覆盖文件是一个例外,因为它们在所有非覆盖文件之后按字母顺序加载。加载文件中的配置将相互附加。这具有相同名称的两个资源不会合并,而是会导致验证错误。

配置中定义的变量,资源等的顺序并不重要。Terraform配置是声明式的,因此对其他资源和变量的引用不依赖于它们定义的顺序。

为什么选择 Terraform 而不选择 ansible 呢?

程序性(Procedural)与声明性(Declarative)

Ansible采用程序性代码,在此过程中,您可以编写代码逐步指定如何实现最终状态。

Terraform,CloudFormation,SaltStack和Puppet都采用声明性代码,您可以直接编写代码指定所需的最终状态,而IAC工具本身负责确定如何实现该状态。

​ 例如,假设您要部署10台服务器来运行应用程序的v1。这是 Ansible模板的简化示例,可通过程序方法来完成此任务:

- ecs:
count: 10
image: ami-v1 
instance_type: t2.micro

以下是Terraform模板的简化示例,该模板使用声明性方法执行相同的操作:

resource "esc_instance" "example" {
count = 10
ami = "ami-v1"
instance_type = "t2.micro"
}

​ 现在看来这两种方法很相似,并且当您最初使用 Ansible 或 Terraform 执行它们时,它们将产生相似的结果。但是我们需要注意的是,当您要进行更改时会发生什么。

假设流量增加了,您想将服务器数量增加到15个。使用 Ansible,您先前编写的过程代码将不再有用。如果您仅将服务器数量更新为15,然后重新运行该代码,它将部署15台新服务器,总共25台!因此,您必须知道以前已经部署了多少服务器,并编写新的脚本来添加 5个新服务器:

- ecs:
count: 5
image: ami-v1 
instance_type: t2.micro

​ 使用声明性代码,由于您要做的就是声明所需的结束状态,并且Terraform 会弄清楚如何达到该结束状态,Terraform还知道它过去创建的任何资源。因此,如果需要将服务器数量增加到15个,您要做的就是回到Terraform脚本并将服务器数量从10更新为15:

resource "ecs_instance" "example" {
count = 15
ami = "ami-v1"
instance_type = "t2.micro"
}

如果执行此模板,Terraform将意识到它已经创建了10台服务器,因此只需要做的就是创建5台新服务器。

思考:在这个问题中展现了 IAC 工具的两个主要问题

  1. 在处理 Ansible 这些程序性代码时,基础架构的状态没有完全展示在代码中,我们需要阅读所有的 Ansible 脚本而且知道这些脚本的执行顺序,才有可能知道基础架构的最终状态。
  2. 程序性代码的可重用性非常低,由于状态不断变化,先前的代码可能不再可用。结果随着时间的推移,过程代码库往往非常庞大而复杂。

参考:https://wsgzao.github.io/post/terraform/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值