开发者社区> CodeSample> 正文

一键创建分布式集群并部署文件

简介: 本教程介绍如何通过Terraform创建一个基于Master/Slave主从模式的分布式集群,并向集群中部署更新文件。

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 编写Terraform脚本代码。本教程以Cloud Shell中运行terraform为例。
    1. 登录阿里云Cloud Shell
    2. main.tf文件中声明Module。
      vim main.tf

      按下i键进入vim的编辑模式,新增以下内容:

      module "app-deployer" {
        source  = "Starnop/app-deployer/alicloud"
        version = "0.0.1"
      
        region            = var.region != "" ? var.region : null
        availability_zone = module.vpc.this_availability_zones[0]
        vpc_id            = module.vpc.this_vpc_id
      
        app_name = "example"
        instance_settings = [
          {
            identifier        = "master"
            description       = "master node"
            hostnamePrefix    = "master"
            ecs_password      = "Example123"
            image_id          = null
            image_owners      = null
            image_name_regex  = null
            instance_type     = null
            cpu               = 2
            memory            = 4
            system_disk_size  = 100
            data_disks        = null
            max_bandwidth_out = 100
            security_groups   = null
            vswitch_id        = module.vpc.this_vswitch_ids[0]
            private_ip        = null
            user_data         = null
            temp_files        = ["temp_files/master_template.file"]
            static_files      = ["static_files/master_static.file"]
            entrypoint        = "echo SUCCESS"
            size              = 2
          },
          {
            identifier        = "worker"
            description       = "worker node"
            hostnamePrefix    = "worker"
            ecs_password      = "Example123"
            image_id          = null
            image_owners      = null
            image_name_regex  = null
            instance_type     = null
            cpu               = 4
            memory            = 8
            system_disk_size  = 100
            data_disks        = null
            max_bandwidth_out = 100
            security_groups   = null
            vswitch_id        = module.vpc.this_vswitch_ids[0]
            private_ip        = null
            temp_files        = ["temp_files/worker_template.file"]
            static_files      = ["static_files/worker_static.file"]
            user_data         = null
            entrypoint        = "echo SUCCESS"
            size              = 3
          }
        ]
      }
      
      module "vpc" {
        source = "alibaba/vpc/alicloud"
      
        region       = var.region != "" ? var.region : null
        vpc_name     = "my_vpc"
        vswitch_name = "my_vswitch"
      
        vswitch_cidrs = [
          "172.16.1.0/24",
        ]
      }
      							

      按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

    3. 在文件variables.tf中定义常量参数。
      vim variables.tf

      按下i键进入vim的编辑模式,新增以下内容:

      variable "region" {
        description = "The region ID used to launch this module resources. If not set, it will be sourced from followed by ALICLOUD_REGION environment variable and profile."
        default     = ""
      }

      按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

    4. 在文件outputs.tf中定义输出参数。
      vim outputs.tf

      按下i键进入vim的编辑模式,新增以下内容:

      output "instances" {
        value = {
          for identifier, instance in module.app-deployer.this_instanceList : identifier => instance
        }
      }

      按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

    5. 新增部署文件。
      • static_files/master_static.file
        mkdir static_files && vim static_files/master_static.file

        按下i键进入vim的编辑模式,新增以下内容:

        master_static

        按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

      • static_files/worker_static.file
        mkdir static_files && vim static_files/worker_static.file

        按下i键进入vim的编辑模式,新增以下内容:

        worker_static

        按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

      • temp_files/master_template.file
        mkdir temp_files && vim temp_files/master_template.file

        按下i键进入vim的编辑模式,新增以下内容:

        Instances: "${Instances.master_0.private_ip}"

        按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

      • temp_files/worker_template.file
        mkdir temp_files && vim temp_files/worker_template.file

        按下i键进入vim的编辑模式,新增以下内容:

        Instances: "${Instances.master_0.public_ip}"

        按下Ecs键退出编辑模式,进入命令模式输入命令:wq,保存并退出vim。

  2. 运行terraform init初始化。
    terraform init

    命令输出结果类似如下:

    Initializing modules...
    Downloading Starnop/app-deployer/alicloud 0.0.1 for app-deployer...
    - app-deployer in .terraform/modules/app-deployer/Starnop-terraform-alicloud-app-deployer-8434639
    Downloading alibaba/vpc/alicloud 1.4.2 for vpc...
    - vpc in .terraform/modules/vpc/terraform-alicloud-modules-terraform-alicloud-vpc-7e25cee
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "null" (hashicorp/null) 2.1.2...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.68.0...
    - Downloading plugin for provider "local" (hashicorp/local) 1.4.0...
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    * provider.local: version = "~> 1.4"
    * provider.null: version = "~> 2.1"
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 运行terraform apply开始创建。
    terraform apply

    命令输出结果类似如下:

    module.app-deployer.data.alicloud_zones.zones_ds: Refreshing state...
    module.vpc.data.alicloud_zones.default: Refreshing state...
    module.vpc.data.alicloud_vpcs.this: Refreshing state...
    module.vpc.data.alicloud_route_tables.this: Refreshing state...
    module.app-deployer.data.alicloud_images.images["worker"]: Refreshing state...
    module.app-deployer.data.alicloud_instance_types.ecs_type["master"]: Refreshing state...
    module.app-deployer.data.alicloud_instance_types.ecs_type["worker"]: Refreshing state...
    module.app-deployer.data.alicloud_images.images["master"]: Refreshing state...
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
    
    Terraform will perform the following actions:
    
    ...
    
    Plan: 23 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    module.vpc.alicloud_vpc.vpc[0]: Creating...
    ...
    
    Apply complete! Resources: 23 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    instances = {
      "master_0" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nmaster node"
        "dry_run" = false
        "host_name" = "master0"
    ...
      }
      "master_1" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nmaster node"
        "dry_run" = false
        "host_name" = "master1"
    ...
      }
      "worker_0" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nworker node"
        "dry_run" = false
        "host_name" = "worker0"
    ...
      }
      "worker_1" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nworker node"
        "dry_run" = false
        "host_name" = "worker1"
    ...
      }
      "worker_2" = {
        "availability_zone" = "cn-chengdu-a"
        "credit_specification" = ""
        "data_disks" = []
        "deletion_protection" = false
        "description" = "OWNER: default\nworker node"
        "dry_run" = false
        "host_name" = "worker2"
    ...
      }
    }

    脚本执行完后,可登录服务器查看,部署的文件在服务器/tmp/example路径下。

分享:
若有任何问题或疑惑,请反馈给我们 立即提问
相关文档
弹性计算
使用钉钉扫一扫加入圈子
+ 订阅

做技术领先、性能优异、稳如磐石的弹性计算!

官方博客
官网链接