这几天看了一些铁人赛的文章,看到一些很不错的文章,当然也看到很多不知所云的内容
观察整体趋势是劣币驱逐良币,心中五味杂陈
大家尽量维持品质,内容有所依据,不要错得离谱就好
不要害读者不看还好,看了文章反而被雷,技术开倒车
本章会特别短,将介绍 state 最後常用的功能 taint / untaint / apply -replace="resource address"
课程内容与代码会放在 Github 上: https://github.com/chechiachang/terraform-30-days
赛後文章会整理放到个人的部落格上 http://chechia.net/
Terraform 官方文件 已经依照 state manipulation 的使用情境做了分类,有以下几个类别
- Inspecting State
- state list
- state show
- refresh
- Forcing Re-creation (Tainting)
- taint
- untaint
- Moving Resources
- state mv
- state rm
- state replace-provider
- Disaster Recovery
- state pull
- state push
- force-unlock
Terraform taint
在 terraform 正常工作流程中,在不需要 destroy + create 的情形下,terraform 会尽量 update 计有的 resource。
然而,有时我们管理 infrastructrue 的时候,就是会需要强制 recreate 的动作,terraform 提供了 state taint / state untaint 来满足需求
使用 taint 可以标记一个已经存在的 state address,在下次 plan / apply 时,terraform 会强制 replace 这个 state 相关的 resource
- .tf resource 不变
- 强制 replace remote resource
- 也同时 replace state
相同的 resource address 下,会删除原先的 remote resource,并 create 一个全新的 remote resource,并把 state 放在原本的 resource address 下
example
一样使用用到烂的 azure/foundation/compute_network
示范
cd azure/foundation/compute_network
terragrunt state list
module.network.data.azurerm_resource_group.network
module.network.azurerm_subnet.subnet[0]
module.network.azurerm_subnet.subnet[1]
module.network.azurerm_subnet.subnet[2]
module.network.azurerm_virtual_network.vnet
terragrunt taint "module.network.azurerm_subnet.subnet[0]"
Resource instance module.network.azurerm_subnet.subnet[0] has been marked as tainted.
terragrunt state list
module.network.data.azurerm_resource_group.network
module.network.azurerm_subnet.subnet[0]
module.network.azurerm_subnet.subnet[1]
module.network.azurerm_subnet.subnet[2]
module.network.azurerm_virtual_network.vnet
terragrunt plan
-/+ destroy and then create replacement
Terraform will perform the following actions:
# module.network.azurerm_subnet.subnet[0] is tainted, so must be replaced
-/+ resource "azurerm_subnet" "subnet" {
~ address_prefix = "10.2.1.0/24" -> (known after apply)
~ id = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1" -> (known after apply)
name = "dev-1"
- service_endpoint_policy_ids = [] -> null
# (6 unchanged attributes hidden)
- timeouts {}
}
Plan: 1 to add, 0 to change, 1 to destroy.
Changes to Outputs:
~ vnet_subnets = [
- "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1",
+ (known after apply),
"/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-2",
# (1 unchanged element hidden)
]
─────────────────────────────────────────────────────────────────────────────
- taint 掉
module.network.azurerm_subnet.subnet[0]
- state list 仍然存在
- plan 的时候显示 destroy and then create replacement
如果 apply,terraform 就会执行 destroy + create
- 过程中会影响依赖 subnet 的公有云 resource
- 如果 subnet 上面没有什麽东西,可以尝试 apply 下去
untaint
- terraform 内部有 mark
tained
的机制 - 如果一个 apply 途中,有多步骤的 create 中途出错,terraform 会自动把产生到一半的 resource 加上
tainted
标记 - 因为 terraform 不能保证 create 到一半的 resource 功能是正常的
- 下次 apply 就会 destroy + create 全新的 resource,确保 resource 完整
如果此时想要人为介入,便可以使用 untaint 来移除 tainted 标记
- 移除後,下次 plan / apply 就不会 destroy + create resource
scenario of taint
- 上面提到的,apply 途中出错,卡在中间,可以用 taint / untaint 控制下次 plan / apply
- provider 觉得不用 recreate,想要人为强制 recreate 的时候
- 如果 resource / module 中有使用 lifecycle {
ignore_change
},使用 taint 可以强制 resource update
taint deprecation
在 Terraform v0.15.2 之後,terraform 提供了新的方法来操作 taint
- 何必 taint 之後再 apply
- 直接 apply 时,指定 -replace="resource address" 就行了
terraform apply -replace="aws_instance.example[0]"