我最新最全的文章都在 南瓜慢說 www.pkslow.com ,歡迎大家來喝茶!
簡介
最近工作中用到了Terraform,權當學習記錄一下,希望能幫助到其它人。
Terraform系列文章如下:
Terraform入門教程,示例展示管理Docker和Kubernetes資源
Terraform插件Provider管理,搜索、定義、下載
Terraform模塊Module管理,聚合資源的抽取與復用
State
狀態是Terraform用於管理基礎設施和配置的,它是真實資源的映射,也可以提供大規模基礎設施平台的效率。它的主要功能是綁定遠程資源平台(如AWS)和本地代碼配置的關系。說白了,就是它存儲了在實際平台中各種資源的狀態,現有的樣子。
先通過示例感受一下State
如果概念不好理解,就先通過示例感受一下吧。
關鍵配置如下,具體請去GitHub參考我的代碼:
provider "kubernetes" {
config_path = "~/.kube/config"
}
module "pkslow-nginx" {
source = "./nginx"
namespace = "pkslow"
applicationName = "pkslow-nginx"
image = "nginx:1.19.5"
replicas = 3
nodePort = 30201
}
先執行apply操作:
$ terraform apply
module.pkslow-nginx.kubernetes_deployment.test: Creating...
module.pkslow-nginx.kubernetes_deployment.test: Creation complete after 4s [id=pkslow/pkslow-nginx]
module.pkslow-nginx.kubernetes_service.test: Creating...
module.pkslow-nginx.kubernetes_service.test: Creation complete after 0s [id=pkslow/pkslow-nginx]
它創建了兩個資源,這里在項目的當前目錄就會新生成一個terraform.tfstate
,它是默認的狀態文件。它是一個Json格式的文件,存儲了apply新建的資源的狀態,如叫什么名字、是什么屬性、IP等。
這時,如果我們再次apply
,它會什么都不生成,因為狀態文件與實際基礎設施一樣,而配置又沒有改動,所以可以認為配置與實際一樣,不需要變更:
$ terraform apply
No changes. Your infrastructure matches the configuration.
我把NodePort改為30301
,再重新apply
:
$ terraform apply
Plan: 0 to add, 1 to change, 0 to destroy.
module.pkslow-nginx.kubernetes_service.test: Modifying... [id=pkslow/pkslow-nginx]
module.pkslow-nginx.kubernetes_service.test: Modifications complete after 0s [id=pkslow/pkslow-nginx]
可以看到它只變更了兩個資源中的其中一個。
通過destroy操作刪除資源時,也是要讀狀態文件的,如果狀態文件丟失了,它就無法正常刪除了。
$ mv terraform.tfstate terraform.tfstate.bak
$ terraform destroy
No changes. No objects need to be destroyed.
Either you have not created any objects yet or the existing objects were already deleted outside of Terraform.
Destroy complete! Resources: 0 destroyed.
有對應的狀態文件,就會根據狀態文件刪除:
$ terraform destroy
Plan: 0 to add, 0 to change, 2 to destroy.
module.pkslow-nginx.kubernetes_service.test: Destroying... [id=pkslow/pkslow-nginx]
module.pkslow-nginx.kubernetes_service.test: Destruction complete after 0s
module.pkslow-nginx.kubernetes_deployment.test: Destroying... [id=pkslow/pkslow-nginx]
module.pkslow-nginx.kubernetes_deployment.test: Destruction complete after 0s
查看狀態
可以通過命令terraform state
查看狀態,主要命令有:
$ terraform state
Subcommands:
list List resources in the state
mv Move an item in the state
pull Pull current state and output to stdout
push Update remote state from a local state file
replace-provider Replace provider in the state
rm Remove instances from the state
show Show a resource in the state
操作如下:
$ terraform state list
module.pkslow-nginx.kubernetes_deployment.test
module.pkslow-nginx.kubernetes_service.test
$ terraform state show module.pkslow-nginx.kubernetes_deployment.test
# module.pkslow-nginx.kubernetes_deployment.test:
......
生產實踐
在生產中,狀態文件一般不會保存在本地,通常會保存在雲存儲中,如etcd、gcp、oss等。
如gcs的配置:
terraform {
backend "gcs" {
bucket = "tf-state-prod"
prefix = "terraform/state"
}
}
阿里雲oss的配置:
terraform {
backend "oss" {
bucket = "bucket-for-terraform-state"
prefix = "path/mystate"
key = "version-1.tfstate"
region = "cn-beijing"
tablestore_endpoint = "https://terraform-remote.cn-hangzhou.ots.aliyuncs.com"
tablestore_table = "statelock"
}
}
歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...
多讀書,多分享;多寫作,多整理。