干货 | 运维福音——Terraform自动化管理京东云

原创: 张宏伟 京东云开发者社区  昨天

Terraform是一个高度可扩展的IT基础架构自动化编排工具,主张基础设施即代码,可通过代码集中管理云资源和基础架构,这意味着用户能够在京东云上轻松使用简单模板语言来定义、预览和部署云基础架构,能够快速将环境部署到京东云或本地数据中心,实现多云管理和跨云迁移。京东云成为国内少数拥有Terraform Provider产品的云厂商之一。应用场景:基础设施即代码、快速部署多云环境、自动化管理降低成本。官网链接:

https://www.terraform.io/docs/providers/jdcloud/index.html

Terraform 是 Hashicorp 公司一款开源的资源编排工具,代表了业界前沿的技术和标准。相对于其他云上资源管理方式,具有快速创建基础设施、高效部署多云环境和大幅降低管理成本三大功能特性。

Terraform 通过代码管理维护云资源,可保存基础设施资源的状态,快速创建和维护管理云主机、网络、负载均衡等云资源,并通过代码与其他人共享云资源的编排配置。

Terraform支持200多个基础设施提供商,适用于多云方案,可快速将用户的环境部署到京东云、其他云厂商或者本地的数据中心。开发者可同时管理不同云厂商的资源,也可快速方便地迁移到另外一个云厂商。Terraform通过代码批量按计划地管理资源,可编排、重复地自动化管理云资源,减少人为因素造成的不确定管理错误,同时能快速创建相同的开发、测试、预发和生成环境,降低开发者的管理成本。

本文通过简单demo做一个技术入门的演示,目的是帮助大家了解如何采用Terraform来自动化管理京东云上的资源。

Terraform安装

Terraform 是一个 IT 基础架构自动化编排工具,它的口号是 “Write, Plan, and create Infrastructure as Code”, 其程序安装在客户的终端PC上,可以运行于多种操作系统平台。本文实例采用的是CentOS操作系统。

登录到主机后先下载一下安装包
 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]
解压缩
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

直接运行程序可以看到以下命令行的帮助信息:

 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
举例:查看Terraform版本
1[jdc@mysandbox tf]$ ./terraform version2Terraform v0.11.13

初始化环境

Terraform访问京东云的服务,首先需要身份认证鉴权。认证采用Access Key与Secret key来完成。从控制台取得AK、SK身份鉴权信息两种方法保存:

方法1:将AK,SK加入运行环境

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

方法2:将AK,SK放入json文件

1cat >> jdcloud.tf <<EOF2provider "jdcloud" {3 access_key = "D4xxxxxxxxxxxxxxxxxxxxxxxxxxxx8D"4 secret_key = "7xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxE "5 region     = "cn-north-1"}6EOF

初始化环境

 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.

演示:创建一个云主机实例

参考Terraform的联机文档https://www.terraform.io/docs/providers/jdcloud/jdcloud_instance.html),创建以下tf文件: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命令可以显示执行计划:
 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)
提交执行:
 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.

成功提交后,我们可以在控制台看到正在运行的实例创建过程:

创建完成后登录主机查看是否与定义文件符合:

  • 查看磁盘划分是否一致:

  • 查看IP地址是否一致:

演示:销毁实例

通过destroy命令可以方便的删除实例。

 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.

在控制台上查看删除进度:

Terraform自动编排的流程

以上只是演示了Terraform管理京东云最简单的流程。实际上通过Terraform完成复杂的编排,完全可以完成一个复杂的大型环境的部署与管理。以下是Terraform的流程:

到此,我们的演示就结束了。

大家可以自己动手试一下这种简洁高效的京东云自动化管理工具了。

点击“阅读原文”了解更多京东云详情

京东云618大促,正在进行时!

最低1折


推荐

阅读

RECOMMEND

Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云!

阅读原文

干货 | 运维福音——Terraform自动化管理京东云的更多相关文章

  1. 不看好运维竖井产品模式,优云打造融合化运维PaaS平台

    2018年1月13号中国双态运维用户大会上,优云软件总裁刘东海接受了36Kr记者的专访,期间谈到了新时代下的企业运维模式,新兴技术和传统运维的融合以及优云未来的发展方向等问题.以下为访谈实录: 优云软 ...

  2. Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云!

    Developer Friendly | 基础设施即代码的事实标准Terraform已支持京东云! Chef.Puppet.Ansible.SaltStack 都可以称为配置管理工具,这些工具的主要目 ...

  3. 比Ansible更吊的自动化运维工具,自动化统一安装部署_自动化部署udeploy 1.0

    新增功能: 2015-03-11 除pass(备份与更新)与start(启动服务)外,实现一切自动化. 注:pass与start设为业务类,由于各类业务不同,所以无法实现自动化.同类业务除外,如更新的 ...

  4. 比Ansible更吊的自动化运维工具,自动化统一安装部署自动化部署udeploy 1.0 版本发布

    新增功能: 逻辑与业务分离,完美实现逻辑与业务分离,业务实现统一shell脚本开发,由框架统一调用. 并发多线程部署,不管多少台服务器,多少个服务,同时发起线程进行更新.部署.启动. 提高list规则 ...

  5. 从运维的角度分析使用阿里云数据库RDS的必要性--你不应该在阿里云上使用自建的MySQL/SQL Server/Oracle/PostgreSQL数据库

    开宗明义,你不应该在阿里云上使用自建的MySQL or SQL Server数据库,对了,还有Oracle or PostgreSQL数据库. 云数据库 RDS(Relational Database ...

  6. 自动化运维工具Fabric - 密码管理(env.password and ssh key)

    在使用 Fabric 的过程中,如果大批量服务器处理的话,我们就需要针对配置主机的密码,每台主机的密码相同还好,不同的话,就需要针对不同的主机做配置了,以下有两种配置方式 注:本文主要参考官方文档 P ...

  7. Linux运维之Ansible自动化运维管理工具

    Ansible简介:Ansible是一个简单高效的自动化运维管理工具,用Python开发,能大批量管理N多台机器,可以并发的在多台机器上部署应用.安装软件.执行命令.配置和编排任务.后面会提到批量安装 ...

  8. 自动化运维之Cobbler自动化部署安装操作系统

    Cobbler概述: Cobbler可以用来快速建立 Linux 网络安装环境,它已将 Linux 网络安装的技术门槛,从大专以上文化水平,成功降低到初中以下,连补鞋匠都能学会. 在生产环境中,经常批 ...

  9. Liunx运维(九)-Liunx进程管理命令

    文档目录: 一.ps:查看进程 二.pstree:查看进程状态树 三.pgrep:查找匹配条件的进程 四.kill:终止进程 五.killall:通过进程名终止进程 六.pkill:通过进程名种植进程 ...

随机推荐

  1. 解决datagridview 横向的scrollbar不显示

    下午遇到这个问题.看到了网上各种解决办法.都没搞定. 新建了一个datagridview.发现是没问题了.仔细对比了一下它们的属性. 在Columns的属性中,有一项:Frozen, 把这个值改顺默认 ...

  2. 清除windows激活信息

    1.管理员运行命令提示符 在命令提示符中输入 slmgr /upk---删除当前KMS密匙 出现"成功地卸载了产品密匙"后,继续依次执行下面两个命令 slmgr /ckms---此 ...

  3. 初玩PLSQL连接 Oracle

    1. 官网下载合适的[Instant Client] https://www.oracle.com/database/technologies/instant-client/winx64-64-dow ...

  4. Python基础笔记:使用dict和set

    dict 就和 C语言中的 map 的作用一样.查找非常快,以空间换时间! dict的使用: >>> d={'Mike':66,'Bob':77,'John':88} #定义一个di ...

  5. 119-PHP调用private成员的方法

    <?php class ren{ //定义人类 private $birthday='1990-12-20'; //定义private修饰的成员属性 public function say_bi ...

  6. poi 导出Excel java代码

    js: function initBatchExport(url,sub_key,current_sub_num){ var btn_id="#btn_char"+current_ ...

  7. 如何通过模仿提升Paper写作能力?

    对于大部分初到国外留学的中国留学生们来说要想自己独立完成一篇Paper可能难度会很大,从Paper字体字号要求.Paper写作格式.Paper写作结构等等诸多因素都会影响留学生们写Paper的效率.对 ...

  8. NumPy 数组迭代

    章节 Numpy 介绍 Numpy 安装 NumPy ndarray NumPy 数据类型 NumPy 数组创建 NumPy 基于已有数据创建数组 NumPy 基于数值区间创建数组 NumPy 数组切 ...

  9. springboot关闭whitelabel Error page

    当访问不存在的页面时报错: 如何关闭它?有2种方法, 方法1: application.properties中加入server.error.whitelabel.enabled=false 方法2:在 ...

  10. python---函数定义、调用、参数

    1.函数定义和调用 下面def test部分的代码块是函数定义:test(2,3)则是函数调用 def test(a,b): print(a) print(b) test(,) 2.必填参数,即函数调 ...