基础架构自动化编排工具-terraform

基础架构自动化编排工具-terraform

Terraform 是什么?

Terraform是用于安全高效地构建,更改和版本控制基础结构的工具。Terraform可以管理现有和流行的服务提供商以及定制的内部解决方案。配置文件向Terraform描述了运行单个应用程序或整个数据中心所需的组件。Terraform生成执行计划,以描述达到预期状态所需执行的操作,然后执行该计划以构建所描述的基础结构。随着配置的更改,Terraform能够确定更改的内容并创建可以应用的增量执行计划。Terraform可以管理的基础结构包括底层组件(例如计算实例,存储和网络)以及高层组件(例如DNS条目,SaaS功能等)。官方说明

基础架构自动化编排工具-terraform

为什么需要它?

通常,我们在开发产品时,大量运用像是阿里云、腾讯云、AWS、GCP…等云平台,而随着产品的增多,我们的机器规格可能会提升,或是架构需要调整,像新增 Cache 的机器…等,又或是我们有许多需求像是数据库、负载均衡器…等诸多云资源,这时Terraform 便是很好的工具去管理这些云基础架构。

经过 Terraform,我们不再需要手动到 Web 界面开启一台台机器,不再需要手动配置 VPC,只需编写好 Terraform 配置文件,一个键就能够到达你想要做的事情,简单来说就是你经过模板配置去管理这些云资源,同时具有以下好处:

  • 能够版本化去管理你的云资源
  • 团队成员都能够经过 Terraform 配置理解目前所使用的云资源和配置情况
  • 能够自动化测试云架构
  • 确保不同的环境 (eg Dev, Test, Prod) 都是相同的配置

Terraform 与其他的工具有什么不同呢?

其他工具如 Chef, Ansible 他们最主要的功能是“配置管理”,即是要配置什么环境变量…等之类的,而相较之下 Terraform 则是决定要什么样的机器。

跟 Terraform 最接近的工具则是 AWS 自行推出的 CloudFormation,但是固然 CloudFormation 也是经过文件去维护云基础架构,但是 Terraform 除了 AWS 能够同时扩大到各家云服务商,同时 Terraform 所制定的编写方式相关于 CloudFormation 是十分的明晰易懂,容易上手维护,就我个人的经历上也是较引荐使用 Terraform的。

下载安装 Terraform(官方推荐)

www.terraform.io/downloads.html

安装后直接在终端机执行 Terraform 指令

➜ ~ terraform -vTerraform v0.11.14
Your version of Terraform is out of date!
The latest versionis 0.12.12.
You can update by downloading from www.terraform.io/downloads.html
基础架构自动化编排工具-terraform

如何编写 Terraform 配置文件?

Terraform 的文件名是 *.tf,而根本的一个 Terraform 配置是经过叫做 HCL 言语撰写,长得像是这样

# Create a new instance of the latest Ubuntu 14.04 on an# t2.micro node with an AWS Tag naming it "HelloWorld"provider "aws" { region = "us-west-2"}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = [
"ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"
]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical

}
resource "aws_instance" "web" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
tags = { Name = "HelloWorld" }
}

上述表示是需求运用 AWS 的云平台,然后需求开启一台 EC2 Instance 的机器,其规格类型是 t2.micro 。

provider 是决定要对哪一个平台操作, region 是 aws 需求的属性。

而这里这里resource 的栏位意义是如下面的表示:

resource 云资源名称 自定义的名称 {  

属性 = 值
}

自定义的名称能够用来被其他资源引用。

除了这个最根本的定义资源语法之外,Terraform 还提供了像是:

Input Variables 让你能够像写程式一样运用变量,在反复用到的地方,或是一些需求动态给予的值。

variable "image_id" { 
type = string
}
variable "availability_zone_names" {
type = list(string)
default = ["us-west-1a"]
}

你能够经过 var.* 引用变量

resource "aws_instance" "this" { 
instance_type = "t2.micro"
ami = var.image_id
}

Output Values 输出一些资源创立后你需要的值,最常见的像是 IP

output "instance_ip_addr" { 
value = aws_instance.this.private_ip
}

你经过 “<provider>__<name>_<attribute>” 去取值。/<attribute>/<name>/<provider>

Modules 当你有多个环境像是 Dev, Test, Prod,经过 Modules 能够让你不用在这些地方都写一份相似的 Terraform,能够集中写一些 Modules 然后在用到的地方引入。

module "servers" { 
source = "./app-cluster"
servers =5
}

lifecycle 指定资源的生命周期,像是先创建新的,再移除旧的资源

resource "azurerm_resource_group" "this" { 
# ...
lifecycle {
create_before_destroy = true
}
}

如何执行 Terraform?

在运用 Terraform 前你需要先有云帐号,像是阿里云、腾讯云、 AWS 帐号、GCP 帐号,而以 AWS 为例,你需求先配置好 AWS 的 credentials。

再来你的目录下,创建一个 main.tf 的文件,并编写你的配置,然后下 terraform init 指令

➜ ~ terraform init
Initializing the backend...
Successfully configured the backend "s3"!
Terraform will automaticallyuse this backend unless the backend configuration changes.
Initializing provider plugins...
- Checking for available provider plugins on releases.hashicorp.com...
- Downloading plugin for provider "null" (2.1.2)...
- Downloading plugin for provider "aws" (2.33.0)...
- Downloading plugin for provider "template" (2.1.2)...
The following providers do not have any version constraints in configuration,so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breakingchanges,
It is recommended to add version = "..." constraints to thecorresponding provider blocks in configuration, with the constraint stringssuggested below.
* provider.aws: version = "~> 2.33"

* provider.null: version = "~> 2.1"
* provider.template: version = "~> 2.1"
Terraform has been successfully initialized!
You may now begin working with Terraform.
Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould now work.
If you ever set or change modules or backend configuration for Terraform,rerun this command to reinitialize your working directory. If you forget, othercommands will detect it and remind you to do so if necessary.

在这个指令中,Terraform 会去下载需要的插件,像是 AWS plugin。

接着经过 terraform plan 指令,能够让你在apply执行之前查看 Terraform 将做哪些哪些改动,这是为了避免我们修正到我们不应该修正的东西,或是有不是我们预期的结果

➜ ~ terraform plan
Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not bepersisted to local or remote state storage.
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
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:
# aws_instance.this will be created
+ resource “aws_instance” “this” {
+ ami = “ami-212132212”
+ arn = (known after apply)
.....

你能够看到上面 Terraform 通知我们有哪些东西会被改动,而当假如我们曾经有服务在上面时,也会通知我们有哪些会被删除。这里面的 known after apply 是表示在资源树立后才会晓得的值。

接下来假如 plan 的改动都没问题,就能够执行 terraform apply 指令,去创建或改动云资源

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

最后,假如要删除呢?你只需经过 terraform destroy 就能够清理一切的资源了

➜ ~ terraform destroy
aws_instance.example:
Refreshing state
... [id=i-05f8863ba523a0082]
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# aws_instance.example will be destroyed
- resource "aws_instance" "this" {
...

其他常用的 Terraform CLI 指令

除了 init, plan, apply, destroy 之外

terraform fmt 由于多人编写 Terraform 的 coding style 很大的可能会不同,该指令会统一转成一定格式的内容。

terraform graph 让你能够用图形看到一切资源的差异性。

terraform import 假如你曾经在云上手动创建资源了,能够经过该指令导入 Terraform 中

terraform validate 能否存在语法错误

Terraform Remote State

由于 Terraform 预设将一切创建的云资源状态存到terraform.tfstate文件,当有更改时会去这里读取状态,但是当多人操作时,不同人电脑上的状态是不同的,因而 Terraform 提供了 Remote State 的机制,让 state 档案统一放在一个地方。

Terraform 提供了以下能够寄存的方式

  • AzureRM
  • Consul
  • GCS
  • Local
  • Manta
  • Postgres
  • Remote
  • S3

并提供了 terraform workspace 指令能够切换不同的 state,由于像是 Dev 环境的 state 会跟 Prod 不一样,应该要分开。

当然即便同一存在一个中央,也可能发作 read write 抵触的问题,所以 Terraform 预设会 lock 防止发作抵触。

结论

到这里我们简单地介绍了 Terraform 的用处、如何编写运用 Terraform,实际运用还有根据需求进行配置,但希望经过这样的介绍可以让你简单快速的认识 Terraform。

有更多兴趣推荐查看官方文档

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


分享到:


相關文章: