1. 程式人生 > 其它 >Terraform狀態State管理,讓變更有記錄

Terraform狀態State管理,讓變更有記錄

我最新最全的文章都在 南瓜慢說 www.pkslow.com ,歡迎大家來喝茶!

簡介

最近工作中用到了Terraform,權當學習記錄一下,希望能幫助到其它人。

Terraform系列文章如下:

Terraform入門教程,示例展示管理Docker和Kubernetes資源

Terraform外掛Provider管理,搜尋、定義、下載

Terraform狀態State管理,讓變更有記錄

Terraform模組Module管理,聚合資源的抽取與複用

Terraform常用命令

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"
  }
}

歡迎關注微信公眾號<南瓜慢說>,將持續為你更新...

多讀書,多分享;多寫作,多整理。