本主题介绍了 Network Infrastructure Automation (NIA) 的池模块。

注:

Terraform 模块设计为仅用于 consul-terraform-sync

此模块支持根据 Consul 目录中的服务配置扩展和缩减池和池成员(服务器)。

Consul-Terraform-Sync

consul-terraform-sync 作为守护进程运行,使 Consul 和控制器之间的发布者-订阅者范式支持 Network Infrastructure Automation (NIA)。



consul-terraform-sync 订阅 Consul 目录的更新,并根据这些更新使用相应的服务变量值来执行一个或多个自动化任务。consul-terraform-sync 利用 Terraform 作为底层自动化工具,并利用 Terraform 提供程序生态系统来推动网络基础架构的相关更改。每个任务都包含一个操作手册自动化,该自动化是使用底层网络基础架构提供程序的资源和数据源以兼容的 Terraform 模块形式编写的。有关安装 consul-terraform-sync 的信息,请参阅安装 Consul-Terraform-Sync

必备条件

  • 要求

    名称

    版本

    Terraform

    >= 0.13

    consul-terraform-sync

    >= 0.1.0

    consul

    >= 1.7

  • 提供商

    名称

    版本

    vmware/avi

    >= 20.1.4

  • 用法

    要使用此模块,您需要安装 consul-terraform-sync,创建一个将此 Terraform 模块作为源的任务,然后运行 consul-terraform-sync

    您可以订阅 consul 目录中的服务,并定义 Terraform 模块,在使用任务对订阅的服务进行任何更新时将执行该模块。

    注:

    建议参考 Consul-Terraform-Sync 配置指南

    • 在高可用性节点上下载 consul-terraform-sync(最好是运行 consul 客户端的节点)。

    • consul-terraform-sync 添加到该节点上的路径。

    • 使用以下 CLI 代码检查安装:

       wget https://releases.hashicorp.com/consul-terraform-  sync//consul-terraform-sync__linux_amd64.zip
       unzip consul-terraform-sync__linux_amd64.zip
       sudo mv consul-terraform-sync /usr/local/bin
       consul-terraform-sync --version
       </code></pre> 
    • consul-terraform-sync 创建一个配置文件 tasks.hcl。例如,

       log_level = <log_level> # eg. "info"
          
       consul {
         address = "<consul agent address>" # eg. "1.1.1.1:8500"
       }
          
       driver "terraform" {
         log = true
         required_providers {
           avi = {
           source =  "vmware/avi"
           version = "20.1.4"
           }
         }
       }
          
       terraform_provider "avi" {
         avi_username    = "admin"
         avi_tenant      = "admin"
         avi_password    = ""
         avi_controller  = "<avi_controller_address>"
         avi_version     = "<api_version>"
         avi_api_timeout = 50
       }
          
       task {
         name = <name of the task (has to be unique)> # eg. "avi-pool-counting"
         description = <description of the task> # eg. "Automatically Scale/Configure AVI Pool Servers"
         source = "vmware/modules/nia/pool" # to be updated
         providers = ["avi"]
         services = ["<list of services you want to subscribe to>"] # eg. ["web", "counting"]
         variable_files = ["<list of files that have user variables for this module (please input full path)>"] # eg. ["/home/user/cts-poc/input_counting.tf"]
         enabled = true
       }
    • 使用以下 CLI 代码启动 consul-terraform-sync

      $ consul-terraform-sync -config-file=tasks.hcl

      consul-terraform-sync 将为每个任务创建池,来自 consul 目录的已订阅服务将成为池的服务器。

      consul-terraform-sync 现已订阅 Consul 目录。对任务中标识的服务进行的任何更新都将导致更新控制器上池的服务器配置。

为 Consul Terraform Sync 配置任务参数

有关 task.hcl 中 task {} 块的变量的更多详细信息,请参阅自述文件指南中的“输入”部分。consul-terraform-sync 有两个方面,即从 Consul 目录更新和管理整个 Terraform 工作流。

从 Consul 目录更新

在后端,consul-terraform-sync 使用配置中标识的 Consul 代理创建一个阻止 API 查询会话,以从名为 consul-terraform-sync 的 Consul 目录中获取更新。

创建、更新或删除以下任何服务属性后,consul-terraform-sync 将在 consul 目录中获取服务的更新。这些更新还包括创建和删除服务。

  • 服务 ID

  • 服务名称

  • 服务地址

  • 服务端口

  • 服务元

  • 服务标记

  • 服务名称空间

  • 服务运行状况

  • 节点 ID

  • 节点地址

  • 节点数据中心

  • 带有节点标记的地址

  • 节点元

管理整个 Terraform 工作流

如果定义了任务,则可将一个或多个服务与该任务相关联,在该任务中声明提供程序,并使用任务的源字段指定 Terraform 模块,将发生以下事件序列:

  • consul-terraform-sync 将安装所需的 Terraform 版本。

  • consul-terraform-sync 将安装在配置文件中定义并在任务中声明的所需 Terraform 提供程序版本。

  • 将创建一个新目录 nia-tasks,其中子目录与每个任务对应。这就是对命名实施严格准则的原因。每个子目录对应于一个单独的 Terraform 工作区。在与任务对应的每个子目录中,consul-terraform-sync 将生成模板 main.tfvariables.tfterraform.tfvars,terraform.tfvars.tmpl

main.tf

此文件包含基于任务定义的所需 Terraform 和提供程序版本的声明。此外,该文件还包含模块(由任务中的源字段标识)声明,并将输入变量 Consul K/V 用作此 Terraform 工作区的后端状态。由 consul-terraform-sync 生成的 main.tf 的示例:

# This file is generated by Consul Terraform Sync.
  #
  # The HCL blocks, arguments, variables, and values are derived from the
  # operator configuration for Sync. Any manual changes to this file
  # may not be preserved and could be overwritten by a subsequent update.
  #
  # Task: avi-svc-web
  # Description: Automatically Scale AVI Service Redirection Destinations
  
  terraform {
    required_version = ">= 0.13.0, < 0.15"
    required_providers {
      avi = {
        source  = "vmware/avi"
        version = "20.1.4"
      }
    }
    backend "consul" {
      address = "x.x.x.x:8500"
      gzip    = true
      path    = "consul-terraform-sync/terraform"
    }
  }
  provider "avi" {
    avi_api_timeout = var.avi.avi_api_timeout
    avi_controller  = var.avi.avi_controller
    avi_password    = var.avi.avi_password
    avi_tenant      = var.avi.avi_tenant
    avi_username    = var.avi.avi_username
    avi_version     = var.avi.avi_version
  }
  
  module "avi-svc-web" {
    source   = "vmware/modules/nia/pool"
    services = var.services
  
    avi_controller = var.avi_controller
    avi_password   = var.avi_password
    avi_username   = var.avi_username
    avi_version    = var.avi_version
    lb_algorithm   = var.lb_algorithm
    pool_name      = var.pool_name
  }

variables.tf

这是在模块中定义的 variables.tf 文件。由 consul-terraform-sync 生成的 variables.tf 的示例:

# This file is generated by Consul Terraform Sync.
#
# The HCL blocks, arguments, variables, and values are derived from the
# operator configuration for Sync. Any manual changes to this file
# may not be preserved and could be overwritten by a subsequent update.
#
# Task: avi-svc-web
# Description: Automatically Scale AVI Service Redirection Destinations

# Service definition protocol v0
variable "services" {
 description = "Consul services monitored by Consul Terraform Sync"
 type = map(
   object({
     id        = string
     name      = string
     kind      = string
     address   = string
     port      = number
     meta      = map(string)
     tags      = list(string)
     namespace = string
     status    = string

     node                  = string
     node_id               = string
     node_address          = string
     node_datacenter       = string
     node_tagged_addresses = map(string)
     node_meta             = map(string)

     cts_user_defined_meta = map(string)
   })
 )
}
variable "avi" {
 default     = null
 description = "Configuration object for avi"
 sensitive   = true
 type = object({
   alias           = string
   avi_api_timeout = number
   avi_controller  = string
   avi_password    = string
   avi_tenant      = string
   avi_username    = string
   avi_version     = string
 })
}

consul-terraform-sync 生成的 variables.module.tf 的示例:

# This file is generated by Consul Terraform Sync.
#
# The HCL blocks, arguments, variables, and values are derived from the
# operator configuration for Sync. Any manual changes to this file
# may not be preserved and could be overwritten by a subsequent update.
#
# Task: avi-svc-web
# Description: Automatically Scale AVI Service Redirection Destinations

variable "avi_controller" {
  default = null
  type    = string
}

variable "avi_password" {
  default = null
  type    = string
}

variable "avi_username" {
  default = null
  type    = string
}

variable "avi_version" {
  default = null
  type    = string
}

variable "lb_algorithm" {
  default = null
  type    = string
}

variable "pool_name" {
  default = null
  type    = string
}

terraform.tfvars

这是由 consul-terraform-sync 生成的最重要的文件。此变量文件是从 Consul 目录中使用最新值为任务中标识的所有服务生成的。

在 Consul 目录中更新相应服务时,consul-terraform-sync 将使用最新值更新此文件。由 consul-terraform-sync 生成的 terraform.tfvars 的示例:

# This file is generated by Consul Terraform Sync.
#
# The HCL blocks, arguments, variables, and values are derived from the
# operator configuration for Sync. Any manual changes to this file
# may not be preserved and could be overwritten by a subsequent update.
#
# Task: avi-svc-web
# Description: Automatically Scale AVI Service Redirection Destinations

services = {
 "web.avi-dev.dc1" : {
   id      = "web"
   name    = "web"
   kind    = ""
   address = "x.x.x.x"
   port    = 80
   meta = {
     enabled = "false"
     ratio   = "5"
   }
   tags            = ["rails"]
   namespace       = null
   status          = "passing"
   node            = "ys-dev"
   node_id         = "<id>"
   node_address    = "x.x.x.x"
   node_datacenter = "dc1"
   node_tagged_addresses = {
     lan      = "x.x.x.x"
     lan_ipv4 = "x.x.x.x"
     wan      = "x.x.x.x"
     wan_ipv4 = "x.x.x.x"
   }
   node_meta = {
     consul-network-segment = ""
   }
   cts_user_defined_meta = {}
 },
 "web.mayank-dev.dc1" : {
   id              = "web"
   name            = "web"
   kind            = ""
   address         = "x.x.x.x"
   port            = 80
   meta            = {}
   tags            = ["rails"]
   namespace       = null
   status          = "passing"
   node            = "mayank-dev"
   node_id         = "<id>"
   node_address    = "x.x.x.x"
   node_datacenter = "dc1"
   node_tagged_addresses = {
     lan      = "x.x.x.x"
     lan_ipv4 = "x.x.x.x"
     wan      = "x.x.x.x"
     wan_ipv4 = "x.x.x.x"
   }
   node_meta = {
     consul-network-segment = ""
   }
   cts_user_defined_meta = {}
 },
 "web.ys-avi-dev-blr.dc1" : {
   id              = "web"
   name            = "web"
   kind            = ""
   address         = "x.x.x.x"
   port            = 89
   meta            = {}
   tags            = ["rails"]
   namespace       = null
   status          = "passing"
   node            = "ys-dev"
   node_id         = "<id>"
   node_address    = "x.x.x.x"
   node_datacenter = "dc1"
   node_tagged_addresses = {
     lan      = "x.x.x.x"
     lan_ipv4 = "x.x.x.x"
     wan      = "x.x.x.x"
     wan_ipv4 = "x.x.x.x"
   }
   node_meta = {
     consul-network-segment = ""
   }
   cts_user_defined_meta = {}
 }
}
注:

Network Infrastructure Automation (NIA) 兼容模块旨在使用上述服务变量。

consul-terraform-sync 根据这些任务中所声明服务的更新,来管理与已定义任务对应的所有单个工作区的整个 Terraform 工作流。

注:

总之,consul-terraform-sync 会根据从 Consul 目录检测到的更新来触发 Terraform 工作流。