Terraform 学习路径

原文转载自 「HelloDog」 ( https://wsgzao.github.io/post/terraform/ ) By wsgzao

预计阅读时间 0 分钟(共 0 个字, 0 张图片, 0 个链接)

前言

相信 Infrastructure as Code (IaC)的 DevOps 理念已经深入人心,如果是 Ansible 是偏向 Infrastructure Configuration tool,那么 Terraform 就是 Infrastructure provision tool,两者本身并不冲突,而 Terraform 使用 declarative 声明式语法结合强大的生态让我们在多云 (AWS,Azure,GCP) 和容器化 (K8s)) 环境中可以更加从容应对新的挑战。

更新历史

2020 年 09 月 14 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/terraform/


Terraform 简介

Terraform 官方文档中 CTO 的介绍视频可以帮助你快速了解

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.

Introduction to Terraform

Introduction to Infrastructure as Code with Terraform

Advantages of Infrastructure as Code

关于 Terraform 开源版你需要知道的缺点,官方的介绍视频主要通过引入 Terraform Enterprise 企业版来解决以下问题

  1. 状态管理非常原始
  2. 缺乏状态可视化
  3. 代码表现力一般

追赶 terraform,让基础设施代码化更加容易,pulumi 都做了些什么?

Terraform 安装

Terraform 的安装和基础命令并不复杂,需要重点学习和熟悉的是各个云厂商开放的 API 语法

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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

Install Terraform

Terraform in 60 Seconds

A Terraform configuration is a series of code blocks that define your intended infrastructure. You’ll run the terraform command against this file to create an Nginx webserver and view the default Nginx web page.

View code

First, open the main.tf file in the text editor by clicking this link.

main.tf

You don’t have to edit or even understand the code. It defines two resources: a Docker disk image that packages the Nginx webserver, and a Docker container that gives it a name and runs it on port 80.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# pwd
/root/terraform-docker-demo

# vim main.tf
terraform {
required_providers {
docker = {
source = "terraform-providers/docker"
}
}
required_version = ">= 0.13"
}

resource "docker_image" "nginx" {
name = "nginx:latest"
}

resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 80
}
}

Init

All Terraform workflows start with the init command. Terraform searches the configuration for both direct and indirect references to providers (such as Docker). Terraform then attempts to load the required plugins.

terraform init

Apply

Now provision the webserver by running apply.

terraform apply

You will be asked to confirm. Type yes and press ENTER. It may take up to 30 seconds. A message will display confirmation that it succeeded.

Verify

Visit this URL to view the default Nginx web page which is now live:

Alternatively, you can examine Docker’s process list. You will see the tutorial container which is running Nginx.

docker ps

Destroy

To remove the Nginx webserver as defined in main.tf, run the destroy command.

terraform destroy

You will be prompted to confirm. Type yes and press ENTER.

Conclusion

You have now created and destroyed your first Terraform resources! Terraform supports hundreds of ecosystem providers, from major cloud resources to content delivery networks and more.

Continue learning at HashiCorp Learn and the Terraform API documentation or discuss with others on the Terraform forum.

Terraform 学习路径

之前也提到了 Terraform 学起来很快,但更重要的是熟悉不同 Providers 之前的调用差异

Learn Terraform - New users can start here. Interactive guides to teach you how to use Terraform’s features. Begin with the Getting Started guide, then continue with task-specific advanced guides or go directly to the Terraform CLI docs.

Providers
Terraform is used to create, manage, and update infrastructure resources such as physical machines, VMs, network switches, containers, and more. Almost any infrastructure type can be represented as a resource in Terraform.

A provider is responsible for understanding API interactions and exposing resources. Most providers configure a specific infrastructure platform (either cloud or self-hosted). Providers can also offer local utilities for tasks like generating random numbers for unique resource names.

Terraform 使用技巧

对方正在输入…

参考文章

Terraform Documentation - The documentation is an in-depth reference guide to all the features of Terraform, including technical details about the internals of how Terraform operates.

more_vert