当前位置:网站首页>Summary of terraform learning (1) -- Introduction to terraform

Summary of terraform learning (1) -- Introduction to terraform

2020-12-07 00:57:39 Technology d life

Preface

stay DevOps In practice , Infrastructure, that is, how code is delivered, is a hot topic . image Chef,Puppet And mature configuration management tools , Can meet a certain degree of demand , But are there any more friendly tools that can meet most of our needs ? The author thinks Terraform It's a potential tool , At present, the major cloud platforms also support well , It's especially easy to use . This article will briefly introduce Terraform Related concepts , And then through a small demo Let's take you into Terraform The world of . explain : The demo environment for this article is ubuntu 16.04.

Terraform What is it? ?

Terraform It's a safe and effective way to build 、 Tools for change and version control infrastructure ( Orchestration tools for infrastructure Automation ). Its goal is to "Write, Plan, and create Infrastructure as Code", Infrastructure is code .Terraform It can support almost all the cloud services available on the market . Specifically, code can be used to manage maintenance IT resources , Before the need to manually operate part of the task through the program to automatically complete , The result of this is very obvious : Efficient 、 It's not easy to make mistakes. .

Terraform Provides a flexible abstraction of resources and providers . The model allows representation from physical hardware 、 Virtual machines and containers to email and DNS All content of the provider . Because of this flexibility ,Terraform It can be used to solve many different problems . This means that there are many tools available with Terraform The functions overlap . But here's the thing ,Terraform It's not mutually exclusive with other systems . It can be used to manage different objects as small as a single application or to the entire data center .

Terraform Use configuration files to describe the managed components ( Small to a single application , To the entire data center ).Terraform Generate an execution plan , Describe what it will do to achieve the desired state , It is then executed to build the described infrastructure . As the configuration changes ,Terraform Be able to determine what has changed , And create an applicable incremental execution plan .

Terraform Yes, it is Go Open source project of language development , You can  github  Access its source code on .

Terraform Core functions

  • Infrastructure is code (Infrastructure as Code)
  • Implementation plan (Execution Plans)
  • Resource map (Resource Graph)
  • Automated change (Change Automation)

Infrastructure is code (Infrastructure as Code)
Use advanced configuration syntax to describe infrastructure , This allows version control of the blueprint for the data center , Treat it like any other code .

Implementation plan (Execution Plans)
Terraform There is one plan step , It generates an execution plan . The execution plan shows when executing apply On command Terraform What will be done . adopt plan Check in advance , You can make Terraform Avoid surprises when working with the real infrastructure .

Resource map (Resource Graph)
Terraform A diagram of all the resources built , It can create and modify any resource that is not interdependent in parallel . therefore ,Terraform Infrastructure can be built efficiently , Operators can also use diagrams to deeply understand the dependencies in their infrastructure .

Automated change (Change Automation)
Applying complex change sets to infrastructure , Without human interaction . Through the implementation plan and resource map mentioned above , We can know for sure that Terraform What will change , In what order , So as to avoid many possible human errors .

install Terraform

Terraform The installation of is very simple , Put... Directly Official binary executable file Save it locally . For example, the author habitually saves it to /usr/local/bin/ Under the table of contents , Of course, this directory will be added to PATH In the environment variables . Check the version number when you're done :

adopt -h Options we can see terraform All commands supported :

stay Azure Create a Resource Group

Must let Terraform visit Azure Resources in subscription , You need to create Azure service principal,Azure service principa Allow your Terraform Script in Azure Configure resources in subscription . Please refer to here establish Azure service principal.

To configure Terraform environment variable
To configure Terraform Use Azure service principal, You need to set the following environment variables :

  • ARM_SUBSCRIPTION_ID
  • ARM_CLIENT_ID
  • ARM_CLIENT_SECRET
  • ARM_TENANT_ID
  • ARM_ENVIRONMENT

The values of these environment variables can be created from the front Azure service principal In the process of getting . Convenience , We can write the steps of setting these environment variables to script files azureEnv.sh in :

#!/bin/sh
echo "Setting environment variables for Terraform"
export ARM_SUBSCRIPTION_ID=your_subscription_id
export ARM_CLIENT_ID=your_appId
export ARM_CLIENT_SECRET=your_password
export ARM_TENANT_ID=your_tenant_id
# Not needed for public, required for usgovernment, german, china
export ARM_ENVIRONMENT=public

It's executing Terraform Pass before order source Command to execute the script !

establish Terraform The configuration file
In order to be in Azure Create a Resource Group, We created the name createrg.tf Configuration file for , And edit the following :

provider "azurerm" {
}
resource "azurerm_resource_group" "rg" {
        name = "NickResourceGroup"
        location = "eastasia"
}

use init The command is used to initialize the working directory
Switch the current directory to createrg.tf Directory of files , And then execute init command :

$ terraform init 

It's just a way of createrg.tf The driver specified in the file is installed in the current directory .terraform Directory :

adopt plan Command to check the configuration file
plan The command checks the configuration file and generates an execution plan , If an error is found in the configuration file, an error will be reported directly :

$ . azureEnv.sh
$ terraform plan

adopt plan Output of command , We can clearly see the tasks to be performed in the target environment .

Use graph Command to generate visual charts
Actually graph The command can only generate data for the relevant chart (dot Formatted data ), We go through dot Command to generate a visual chart , First install through the following command dot Program :

$ sudo apt install graphviz

And then generate a graph :

$ terraform graph | dot -Tsvg > graph.svg

The picture above shows us through azurerm The driver creates a Resource Group.

Use apply Command to complete the deployment operation
In the use of apply When the command performs the actual deployment , By default, it will execute first plan Command and enter interactive mode to wait for user confirmation , We've implemented plan The command , So you can use -auto-approve Option to skip these steps and execute the deployment operation directly :

$ terraform apply -auto-approve

To Azure Check it out on the site , The name of the discovery is NickResourceGroup Of Resource Group It has been created successfully .

summary

Terraform There are many platforms supported , image AWS,Azure It's needless to say when we wait for big factories , Some small manufacturers can also provide provider Support Terraform, So that the whole ecology becomes very active . If you want to DevOps In practice, the introduction of infrastructure as code , Whether it's a public cloud or a private cloud , Believe in Terraform You won't be disappointed .

版权声明
本文为[Technology d life]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201207005524690i.html