Dry goods | transport Weifu sound --Terraform automated cloud management Jingdong
Original: Zhang Hongwei Jingdong cloud developer community yesterday
Terraform is a highly scalable IT infrastructure orchestration tool automation, advocate infrastructure, namely the code through the code can be centrally managed cloud resources and infrastructure, which means that users can easily use a simple template language to define cloud in Jingdong, and preview deploying cloud infrastructure to quickly deploy cloud environment to Jingdong or local data center, to achieve cloudy management and cross-cloud migration. Jingdong cloud has become one of the few products Terraform Provider cloud vendors. Scenario: Infrastructure as a code cloudy environment for rapid deployment, automate management and reduce costs. Official website link:
https://www.terraform.io/docs/providers/jdcloud/index.html
Terraform Hashicorp company is an open-source resource scheduling tools, on behalf of the industry's cutting-edge technologies and standards. Relative to other cloud resource management, infrastructure has to quickly create, deploy and efficient cloudy environment and significantly reduce management costs of the three features.
Terraform maintenance code management through cloud resources, we can save the state of infrastructure resources, quickly create and maintain cloud management host, network, load balancing cloud resources and cloud resources sharing arrangement configured via code with other people.
Terraform supports more than 200 infrastructure provider for cloudy solutions can be deployed quickly to the user's environment Jingdong cloud, cloud vendors or other local data center. Developers can manage resources of different cloud vendors can also be quickly and easily migrate to another cloud vendor. Terraform through code batch management of resources according to plan, the arrangement can be repeatedly automated management of cloud resources, reduce uncertainty management errors caused by human factors, and can quickly create the same development, testing, pre-development and build environment, reduce developers' management costs.
This article demonstrates a technique to do to get started with a simple demo, designed to help people understand how to use the resources on the Terraform to automate management Jingdong cloud.
Terraform installation
Terraform is an automated IT infrastructure orchestration tool, its slogan is "Write, Plan, and create Infrastructure as Code", which is installed on the customer's terminal PC, can run on multiple operating system platforms. Examples herein are employed CentOS operating system.
After logging in to the host installation package to download it
1[jdc@mysandbox ~]$ mkdir tf 2[jdc@mysandbox ~]$ cd tf 3[jdc@mysandbox tf]$ wget https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip 4--2019-05-16 14:41:57-- https://releases.hashicorp.com/terraform/0.11.13/terraform_0.11.13_linux_amd64.zip 5Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.109.183, 2a04:4e42:1a::439 6Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.109.183|:443... connected. 7HTTP request sent, awaiting response... 200 OK 8Length: 21128942 (20M) [application/zip] 9Saving to: ‘terraform_0.11.13_linux_amd64.zip’1011100%[============================================================================================================================================================>] 21,128,942 4.30MB/s in 66s12132019-05-16 14:43:05 (312 KB/s) - ‘terraform_0.11.13_linux_amd64.zip’ saved [21128942/21128942]
unzip
1[jdc@mysandbox tf]$ ls2terraform_0.11.13_linux_amd64.zip[jdc@mysandbox tf]$ unzip terraform_0.11.13_linux_amd64.zip3Archive: terraform_0.11.13_linux_amd64.zip4inflating: terraform
Direct help run the program you can see the following command line:
1$ terraform 2Usage: terraform [--version] [--help] <command> [args] 3 4The available commands for execution are listed below. 5The most common, useful commands are shown first, followed byless common or more advanced commands. If you're just gettingstarted with Terraform, stick with the common commands. For theother commands, please read the help and docs before usage. 6 7Common commands: 8apply Builds or changes infrastructure 9console Interactive console for Terraform interpolations destroy Destroy Terraform-managed infrastructure10fmt Rewrites config files to canonical format11get Download and install modules for the configuration12graph Create a visual graph of Terraform resources import Import existing infrastructure into Terraform init Initialize a new or existing Terraform configuration13output Read an output from a state file14plan Generate and show an execution plan15providers Prints a tree of the providers used in the configuration16push Upload this Terraform module to Terraform Enterprise to run17refresh Update local state file against real resources18show Inspect Terraform state or plan19taint Manually mark a resource for recreation20untaint Manually unmark a resource as tainted21validate Validates the Terraform files22version Prints the Terraform version23workspace Workspace management2425All other commands:26debug Debug output management (experimental)27force-unlock Manually unlock the terraform state28state Advanced state management
Example: View Terraform version
1[jdc@mysandbox tf]$ ./terraform version2Terraform v0.11.13
Initialize the environment
Terraform Jingdong access cloud-based services, you first need to identity authentication. Certification using Access Key and Secret key to complete. AK made from the console, SK identity authentication information are two ways to save:
Method 1: AK, SK added to the operating environment
1[jdc@mysandbox tf]$ cat >> ~/.bash_profile <<EOF2> #### add Hongwei 201905163> export access_key="D4xxxxxxxxxxxxxxxxxxxxxxxxxxxx8D"4> export secret_key="7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxE"5> export region="cn-north-1"> EOF6[jdc@mysandbox tf]$ . ~/.bash_profile
Method 2: AK, SK into json file
1cat >> jdcloud.tf <<EOF2provider "jdcloud" {3 access_key = "D4xxxxxxxxxxxxxxxxxxxxxxxxxxxx8D"4 secret_key = "7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxE "5 region = "cn-north-1"}6EOF
Initialize the environment
1[jdc@mysandbox tf]$ ./terraform init 2 3Initializing provider plugins... 4- Checking for available provider plugins on https://releases.hashicorp.com... 5- Downloading plugin for provider "jdcloud" (0.0.1)... 6 7The following providers do not have any version constraints in configuration,so the latest version was installed. 8 9To prevent automatic upgrades to new major versions that may contain breakingchanges, it is recommended to add version = "..." constraints to thecorresponding provider blocks in configuration, with the constraint stringssuggested below.1011* provider.jdcloud: version = "~> 0.0"1213Terraform has been successfully initialized!1415You may now begin working with Terraform. Try running "terraform plan" to seeany changes that are required for your infrastructure. All Terraform commandsshould now work.1617If you ever set or change modules or backend configuration for Terraform,rerun this command to reinitialize your working directory. If you forget, othercommands will detect it and remind you to do so if necessary.
Demo: Create an instance of Cloud Hosting
Reference Terraform online documentation ( https://www.terraform.io/docs/providers/jdcloud/jdcloud_instance.html ), tf creates the following files: jdcloud_ instance.tf
1resource "jdcloud_instance" "vm-1" { 2 az = "cn-north-1a" 3 instance_name = "vm-1" 4 instance_type = "g.n2.medium" 5 image_id = "bba85cab-dfdc-4359-9218-7a2de429dd80" 6 password = "cNXOxJywMU6IY7c0CgIj" 7 subnet_id = "subnet-35h6keqh4m" 8 network_interface_name = "example_ni_name" 9 primary_ip = "10.0.0.27"10 secondary_ip_count = 011 security_group_ids = ["sg-chx9tv75xa"]1213 system_disk = {14 disk_category = "local"15 device_name = "vda"16 disk_type="ssd"17 disk_size_gb = 4018}1920data_disk = {21 disk_category = "cloud"22 device_name = "vdc"23 disk_type = "ssd"24 disk_name = "exampleDisk"25 disk_size_gb = 5026 az = "cn-north-1a"2728 auto_delete = true29 disk_name = "vm1-datadisk-1"30 description = "test"31 }32}
plan command displays the execution plan:
1[jdc@mysandbox tf]$ ./terraform plan 2Refreshing Terraform state in-memory prior to plan... 3The refreshed state will be used to calculate this plan, but will not bepersisted to local or remote state storage. 4 5jdcloud_instance.vm-1: Refreshing state... (ID: i-y8ye9jd6ny) 6 7------------------------------------------------------------------------ 8 9An execution plan has been generated and is shown below.10Resource actions are indicated with the following symbols:-/+ destroy and then create replacement1112Terraform will perform the following actions:1314-/+ jdcloud_instance.vm-1 (new resource required)15id: "i-y8ye9jd6ny" => <computed> (forces new resource)16az: "cn-north-1a" => "cn-north-1a"17data_disk.#: "1" => "1"18data_disk.0.auto_delete: "true" => "true"19data_disk.0.az: "cn-north-1a" => "cn-north-1a"20data_disk.0.description: "test" => "test"21data_disk.0.device_name: "vdc" => "vdc"22data_disk.0.disk_category: "cloud" => "cloud"23data_disk.0.disk_id: "vol-fhvqnjyxw7" => <computed>24data_disk.0.disk_name: "vm1-datadisk-1" => "vm1-datadisk-1"25data_disk.0.disk_size_gb: "50" => "50"26data_disk.0.disk_type: "ssd" => "ssd" image_id: "bba85cab-dfdc-4359-9218-7a2de429dd80" => "bba85cab-dfdc-4359-9218-7a2de429dd80"27instance_name: "vm-1" => "vm-1"28instance_type: "g.n2.medium" => "g.n2.medium"29ip_addresses.#: "0" => <computed>30network_interface_name: "example_ni_name" => "example_ni_name"31password: <sensitive> => <sensitive> (attribute changed)32primary_ip: "10.0.0.27" => "10.0.0.27"33secondary_ip_count: <sensitive> => <sensitive> (attribute changed)34security_group_ids.#: "1" => "1"35security_group_ids.4008937636: "sg-chx9tv75xa" => "sg-chx9tv75xa"36subnet_id: "subnet-35h6keqh4m" => "subnet-35h6keqh4m"37system_disk.#: "1" => "1"38system_disk.0.auto_delete: "true" => <computed>39system_disk.0.az: "" => <computed>40system_disk.0.device_name: "vda" => "vda"41system_disk.0.disk_category: "local" => "local"42system_disk.0.disk_id: "" => <computed>43system_disk.0.disk_name: "" => <computed>44system_disk.0.disk_size_gb: "40" => "40"45system_disk.0.disk_type: "" => "ssd" (forces new resource)
Submitted to the Executive:
1[jdc@mysandbox tf]$ ./terraform apply -auto-approve
2jdcloud_instance.vm-1: Creating...
3az: "" => "cn-north-1a"
4data_disk.#: "" => "1"
5data_disk.0.auto_delete: "" => "true"
6data_disk.0.az: "" => "cn-north-1a"
7data_disk.0.description: "" => "test" 8data_disk.0.device_name: "" => "vdc" 9data_disk.0.disk_category: "" => "cloud"10data_disk.0.disk_id: "" => "<computed>"11data_disk.0.disk_name: "" => "vm1-datadisk-1"12data_disk.0.disk_size_gb: "" => "50"13data_disk.0.disk_type: "" => "ssd"14image_id: "" => "bba85cab-dfdc-4359-9218-7a2de429dd80"15instance_name: "" => "vm-1"16instance_type: "" => "g.n2.medium"17ip_addresses.#: "" => "<computed>"18network_interface_name: "" => "example_ni_name"19password: "<sensitive>" => "<sensitive>"20primary_ip: "" => "10.0.0.27"21secondary_ip_count: "<sensitive>" => "<sensitive>"22security_group_ids.#: "" => "1"23security_group_ids.4008937636: "" => "sg-chx9tv75xa"24subnet_id: "" => "subnet-35h6keqh4m"25system_disk.#: "" => "1"26system_disk.0.auto_delete: "" => "<computed>"27system_disk.0.az: "" => "<computed>"28system_disk.0.device_name: "" => "vda"29system_disk.0.disk_category: "" => "local"30system_disk.0.disk_id: "" => "<computed>"31system_disk.0.disk_name: "" => "<computed>"32system_disk.0.disk_size_gb: "" => "40" system_disk.0.33disk_type: "" => "ssd"jdcloud_instance.vm-1: Still creating... (10s elapsed)34jdcloud_instance.vm-1: Still creating... (20s elapsed)35jdcloud_instance.vm-1: Still creating... (30s elapsed)36jdcloud_instance.vm-1: Still creating... (40s elapsed)37jdcloud_instance.vm-1: Still creating... (50s elapsed)38jdcloud_instance.vm-1: Still creating... (1m0s elapsed)39jdcloud_instance.vm-1: Creation complete after 1m1s (ID: i-y8ye9jd6ny)40Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
After successful submission, we can see that the instance is running in the console creation process:
After you create a complete login to check whether the host files that meet the definition:
- See if the same disk partitioning:
- See if the same IP address:
Demo: Destruction examples
Examples can be easily deleted by destroy command.
1[jdc@mysandbox tf]$ ./terraform destroy 2jdcloud_instance.vm-1: Refreshing state... (ID: i-y8ye9jd6ny) 3 4An execution plan has been generated and is shown below. 5Resource actions are indicated with the following symbols: - destroy 6 7Terraform will perform the following actions: 8 - jdcloud_instance.vm-1
910Plan: 0 to add, 0 to change, 1 to destroy.1112Do you really want to destroy all resources?13 Terraform will destroy all your managed infrastructure, as shown above.14 There is no undo. Only 'yes' will be accepted to confirm.1516 Enter a value: yesjdcloud_instance.vm-1: Destroying... (ID: i-y8ye9jd6ny)1718jdcloud_instance.vm-1: Still destroying... (ID: i-y8ye9jd6ny, 10s elapsed)19jdcloud_instance.vm-1: Still destroying... (ID: i-y8ye9jd6ny, 20s elapsed)20jdcloud_instance.vm-1: Still destroying... (ID: i-y8ye9jd6ny, 30s elapsed)21jdcloud_instance.vm-1: Still destroying... (ID: i-y8ye9jd6ny, 40s elapsed)22jdcloud_instance.vm-1: Destruction complete after 41s2324Destroy complete! Resources: 1 destroyed.
On the console to view the progress of deletion:
Terraform automatic scheduling process
These are just demonstrates Terraform cloud management Jingdong most simple process. In fact by Terraform complete complex choreography is entirely possible to complete the deployment and management of a large and complex environment. The following are Terraform process:
This, our demo ended.
You can try this yourself Jingdong cloud simple and efficient automated management tool.
Click on "read the original" Jingdong cloud for more details
Jingdong big promotion cloud 618, is in progress!
Minimum 90% off
recommend
read
RECOMMEND