Terraform使用
Terraform
默认下载插件目录
当前目录下.terraform
:
terraform配置文件
bash
#vim /root/terraformrc
terraformrc
文件是 Terraform 的配置文件,通常命名为 .terraformrc
或 terraform.rc
,它位于用户的主目录下。在 Windows 系统上,该文件被命名为 terraform.rc
并且位于用户的“应用数据”文件夹中。这个文件用于配置一些 Terraform 的全局设置,如插件的安装位置和访问 Terraform Registry 的凭证。
以下是 .terraformrc
中可以设置的一些常用参数:
plugin_cache_dir - 指定插件缓存的目录位置,用于加速插件下载和安装。
yamlplugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
provider_installation - 用于配置自定义的提供程序安装选项,例如直接从文件系统中安装,或者指定镜像地址。
yamlprovider_installation { filesystem_mirror { path = "/mnt/terraform-plugins" include = ["registry.terraform.io/*/*"] } network_mirror { url = "https://mirrors.example.com/terraform/" include = ["registry.terraform.io/*/*"] } direct { exclude = ["registry.terraform.io/hashicorp/*"] } }
credentials - 用来设置访问私有 Terraform Registry 的凭证。
yamlcredentials "app.terraform.io" { token = "xxxxxx.atlasv1.zzzzzzzzzzzzz" }
disable_checkpoint - 可以禁用 Terraform 的检查点功能,这通常用于避免自动更新和检查 Terraform 版本。
hcldisable_checkpoint = true
disable_checkpoint_signature - 关闭检查点签名验证,增加安全风险。
hcldisable_checkpoint_signature = true
请注意,对于大多数用户来说,.terraformrc
文件是不必要的,因为 Terraform 默认情况下会采用合理的设置。.terraformrc
文件通常用于高级用户或在企业环境中,需要细致地控制 Terraform 的行为时使用。如果你选择使用 .terraformrc
文件,请确保它不被错误地提交到版本控制系统中,因为它可能包含敏感信息。
命令
bash
terraform init
#install
terraform apply --auto-approve
# uninstall
terraform destroy --auto-approve
注意:当使用terraform重新部署一个应用时,之前生成的缓存文件一定要清理干净,否则会各种报错
bash
[root@devops8 k8s]#ls -al
total 48
drwxr-xr-x 3 root root 128 Jun 19 09:06 .
drwxr-xr-x 6 root root 115 May 30 21:13 ..
-rw-r--r-- 1 root root 15343 May 30 17:37 ingress.yaml
-rw-r--r-- 1 root root 2778 May 30 17:37 kind.tf
-rw-r--r-- 1 root root 1010 May 30 17:37 README.md
drwxr-xr-x 3 root root 23 Jun 19 09:04 .terraform
-rw-r--r-- 1 root root 452 Jun 19 09:04 .terraform.lock.hcl
-rw-r--r-- 1 root root 19207 Jun 19 09:06 terraform.tfstate
[root@devops8 k8s]#rm -rf .terraform .terraform.lock.hcl terraform.tfstate