My latest and most complete articles are in the pumpkin slow say www.pkslow.com, welcome to tea!

Introduction to the

Terraform has been used in my work recently. I hope it can help other people.

The Terraform series is as follows:

Terraform tutorial with examples of managing Docker and Kubernetes resources

Terraform plugin Provider management, search, define, download

Terraform State management allows changes to be recorded

Terraform Module Module management, aggregation resource extraction and reuse

Terraform Common command

The State State, which Terraform uses to manage infrastructure and configuration, is a mapping of real resources and can also provide the efficiency of large-scale infrastructure platforms. Its main function is to bind the relationship between remote resource platforms (such as AWS) and local code configurations. To put it bluntly, it stores the existing state of various resources in the actual platform.

Get a feel for State with an example

If the concept is hard to understand, feel the way through an example first.

The key configuration is as follows, please refer to my code on GitHub for details:

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
}
Copy the code

Apply first:

$ 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]
Copy the code

It creates two resources, where a new terraform.tfState is generated in the project’s current directory, which is the default status file. It is a JSON-formatted file that stores the state of the resource created by Apply, such as its name, properties, IP, and so on.

At this point, if we apply again, it generates nothing, because the status file is the same as the actual infrastructure, and the configuration has not changed, so we can assume that the configuration is the same as the actual configuration and does not need to change:

$ terraform apply
No changes. Your infrastructure matches the configuration.
Copy the code

I changed NodePort to 30301 and re-applied:

$ 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]
Copy the code

You can see that it only changes one of the two resources.

When you destroy a resource, you also read the state file. If the state file is lost, it cannot be deleted properly.

$ 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.Copy the code

If there is a corresponding status file, it will be deleted according to the status file:

$ 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
Copy the code

Check the status

You can run the terraform state command to view the status. The main commands are:

$ 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
Copy the code

The operation is as follows:

$ 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:.Copy the code

The production practice

In production, status files are usually stored in cloud storage devices, such as ETCD, GCP, and OSS, rather than locally.

Such as GCS configuration:

terraform {
  backend "gcs" {
    bucket  = "tf-state-prod"
    prefix  = "terraform/state"}}Copy the code

Ali Cloud OSS configuration:

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"}}Copy the code

Welcome to pay attention to the wechat public number “Pumpkin slow Talk”, will continue to update for you…

Read more and share more; Write more. Organize more.