关于后端:Terraform-101-从入门到实践-第二章-Providers插件管理

8次阅读

共计 3822 个字符,预计需要花费 10 分钟才能阅读完成。

《Terraform 101 从入门到实际》这本小册在南瓜慢说官方网站和 GitHub 两个中央同步更新,书中的示例代码也是放在 GitHub 上,不便大家参考查看。


不怕出身低,行行出状元。

插件

Terraform 能够对多种平台的多种资源进行治理,这个是通过插件来实现的。

这里的插件,在 Terraform 的世界也叫 Providers,也是一个个可执行文件。不同的插件实现不同的性能,对接 AWS,就要应用 AWS 的插件;对接 GCP,就要用 GCP 的插件。

当咱们通过 terraform init 初始化一个我的项目时,Terraform 就会依据配置帮咱们下载插件。在咱们执行 apply 的时候,就会调用这些插件实现对应的资源管理。

咱们能够到官网仓库(https://registry.terraform.io…)去搜有什么插件可用,这里有极其丰富的插件,也有具体的应用阐明:

接下来,咱们就插件探讨几个问题:

  • 怎么指定下载哪些插件和版本号?
  • 从哪里下载?
  • 下载到什么中央?
  • 没有对插件库有拜访权限的环境下怎么解决?
  • 是否每个我的项目都要下载雷同的插件?

指定下载哪些插件和版本

Terraform 是通过解析 required_providers 晓得须要哪些插件,个别习惯是定义一个 verion.tf 文件,把相干配置都放在这个文件里,比方:

terraform {
  required_version = "= v1.0.11"

  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "= 2.1.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "3.1.0"
    }
  }
}

这个文件定义了 Terraform 外围组件的版本,还定义了 local 和 random 插件及其版本号。下面指定 Terraform 版本为 1.0.11,local 版本为 2.1.0,random 版本为 3.1.0。

咱们看这里的版本号有两个等于 = 号,会不会感觉奇怪?其实这是 HCL 语言的一个个性,除了 = 号,还能够是 ><= 等,这样能够指定版本范畴,而不只是某个特定版本。

从哪里下载

能够通过命令 terraform providers 查看以后我的项目配置的插件是从哪里下载的。如下:

$ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/random] 3.1.0
└── provider[registry.terraform.io/hashicorp/local] 2.1.0

默认是从官网的公共仓库 registry.terraform.io 下载的。

如果须要指定其它仓库,代码如下:

terraform {
  required_version = "= v1.0.11"

  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "= 2.1.0"
    }
    random = {
      source  = "hashicorp/random"
      version = "3.1.0"
    }
    pkslowcloud = {
      source  = "registry.pkslow.com/examplecorp/pkslowcloud"
      version = "0.1.0"
    }
  }
}

这里 pkslowcloud 就是应用自定义的仓库地址,执行 providers 命令如下:

$ terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/local] 2.1.0
├── provider[registry.terraform.io/hashicorp/random] 3.1.0
└── provider[registry.pkslow.com/examplecorp/pkslowcloud] 0.1.0

留神:pkslowcloud理论不存在,大家不用尝试下载应用。

下载到什么中央

执行 terraform init 进行初始化,就会下载插件:

$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/random versions matching "3.1.0"...
- Finding hashicorp/local versions matching "2.1.0"...
- Installing hashicorp/random v3.1.0...
- Installed hashicorp/random v3.1.0 (signed by HashiCorp)
- Installing hashicorp/local v2.1.0...
- Installed hashicorp/local v2.1.0 (signed by HashiCorp)

执行完 init 命令后,当前工作目录就会有一个 .terraform 文件夹,这里就放了插件的程序。目录构造如下:

$ tree -a
.
├── .terraform
│   └── providers
│       └── registry.terraform.io
│           └── hashicorp
│               ├── local
│               │   └── 2.1.0
│               │       └── darwin_amd64
│               │           └── terraform-provider-local_v2.1.0_x5
│               └── random
│                   └── 3.1.0
│                       └── darwin_amd64
│                           └── terraform-provider-random_v3.1.0_x5

没有网络环境怎么办

在有些状况下,并不能间接拜访 Terraform 的公共仓库去下载插件,如果能够从其它中央复制一份插件,并能够应用,那岂不是美哉?Terraform 曾经思考了这种需要。

首先它反对有网络环境的机器把当前目录的插件复制到特定目录,命令如下:

$ terraform providers mirror /Users/larry/Software/terraform/plugins
- Mirroring hashicorp/local...
  - Selected v2.1.0 to meet constraints 2.1.0
  - Downloading package for darwin_amd64...
  - Package authenticated: signed by HashiCorp
- Mirroring hashicorp/random...
  - Selected v3.1.0 to meet constraints 3.1.0
  - Downloading package for darwin_amd64...
  - Package authenticated: signed by HashiCorp

查看一下目录构造,Terraform 会打包好插件为 zip 文件:

$ tree -a /Users/larry/Software/terraform/plugins
/Users/larry/Software/terraform/plugins-localdisk
└── registry.terraform.io
    └── hashicorp
        ├── local
        │   ├── 2.1.0.json
        │   ├── index.json
        │   └── terraform-provider-local_2.1.0_darwin_amd64.zip
        └── random
            ├── 3.1.0.json
            ├── index.json
            └── terraform-provider-random_3.1.0_darwin_amd64.zip

下次咱们能够指定插件目录实现复用:

$ terraform init -plugin-dir=/Users/larry/Software/terraform/plugins

Initializing the backend...

Initializing provider plugins...
- Reusing previous version of hashicorp/random from the dependency lock file
- Reusing previous version of hashicorp/local from the dependency lock file
- Using previously-installed hashicorp/random v3.1.0
- Using previously-installed hashicorp/local v2.1.0

看日志能够看到,Terraform 不再下载,而是重用插件。

执行完命令 init 后,再查看terraform version,则会显示插件的版本:

$ terraform version
Terraform v1.0.11
on darwin_amd64
+ provider registry.terraform.io/hashicorp/local v2.1.0
+ provider registry.terraform.io/hashicorp/random v3.1.0

Terraform 对于这种插件目录重用的反对,不只是 zip 包,二进制也是反对的,但对应的目录后果有点不一样。这里不开展介绍了。

正文完
 0