在 Azure 中使用 Terraform 创建中心虚拟网络设备

使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。

VPN 设备是用于向本地网络提供外部连接的设备。 VPN 设备可以是硬件设备或软件解决方案。 软件解决方案的一个示例为 Windows Server 2012 中的路由和远程访问服务 (RRAS)。 有关 VPN 设备的详细信息,请参阅关于站点到站点 VPN 网关连接的 VPN 设备

Azure 支持各种网络虚拟设备,可供选择。 本文使用 Ubuntu 映像。 若要详细了解 Azure 中支持的多种设备解决方案,请参阅网络设备主页

在本文中,学习如何:

  • 在中心辐射型拓扑中实现中心 VNet
  • 创建充当设备的中心网络虚拟机
  • 启用使用 CustomScript 扩展的路由
  • 创建中心辐射型网关路由表

1.配置环境

  • Azure 订阅:如果没有 Azure 订阅,请在开始之前创建一个免费帐户。

2. 实现 Terraform 代码

  1. 将本系列第一篇文章中创建的示例目录设为当前目录。

  2. 创建名为 hub-nva.tf 的文件并插入下列代码:

    locals {
        prefix-hub-nva         = "hub-nva"
        hub-nva-location       = "eastus"
        hub-nva-resource-group = "hub-nva-rg"
    }
    
    resource "azurerm_resource_group" "hub-nva-rg" {
        name     = "${local.prefix-hub-nva}-rg"
        location = local.hub-nva-location
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_network_interface" "hub-nva-nic" {
        name                 = "${local.prefix-hub-nva}-nic"
        location             = azurerm_resource_group.hub-nva-rg.location
        resource_group_name  = azurerm_resource_group.hub-nva-rg.name
        enable_ip_forwarding = true
    
        ip_configuration {
        name                          = local.prefix-hub-nva
        subnet_id                     = azurerm_subnet.hub-dmz.id
        private_ip_address_allocation = "Static"
        private_ip_address            = "10.0.0.36"
        }
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_virtual_machine" "hub-nva-vm" {
        name                  = "${local.prefix-hub-nva}-vm"
        location              = azurerm_resource_group.hub-nva-rg.location
        resource_group_name   = azurerm_resource_group.hub-nva-rg.name
        network_interface_ids = [azurerm_network_interface.hub-nva-nic.id]
        vm_size               = var.vmsize
    
        storage_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04-LTS"
        version   = "latest"
        }
    
        storage_os_disk {
        name              = "myosdisk1"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
        }
    
        os_profile {
        computer_name  = "${local.prefix-hub-nva}-vm"
        admin_username = var.username
        admin_password = var.password
        }
    
        os_profile_linux_config {
        disable_password_authentication = false
        }
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_virtual_machine_extension" "enable-routes" {
        name                 = "enable-iptables-routes"
        virtual_machine_id   = azurerm_virtual_machine.hub-nva-vm.id
        publisher            = "Microsoft.Azure.Extensions"
        type                 = "CustomScript"
        type_handler_version = "2.0"
    
    
        settings = <<SETTINGS
        {
            "fileUris": [
            "https://raw.githubusercontent.com/mspnp/reference-architectures/master/scripts/linux/enable-ip-forwarding.sh"
            ],
            "commandToExecute": "bash enable-ip-forwarding.sh"
        }
    SETTINGS
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_route_table" "hub-gateway-rt" {
        name                          = "hub-gateway-rt"
        location                      = azurerm_resource_group.hub-nva-rg.location
        resource_group_name           = azurerm_resource_group.hub-nva-rg.name
        disable_bgp_route_propagation = false
    
        route {
        name           = "toHub"
        address_prefix = "10.0.0.0/16"
        next_hop_type  = "VnetLocal"
        }
    
        route {
        name                   = "toSpoke1"
        address_prefix         = "10.1.0.0/16"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.0.0.36"
        }
    
        route {
        name                   = "toSpoke2"
        address_prefix         = "10.2.0.0/16"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.0.0.36"
        }
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_subnet_route_table_association" "hub-gateway-rt-hub-vnet-gateway-subnet" {
        subnet_id      = azurerm_subnet.hub-gateway-subnet.id
        route_table_id = azurerm_route_table.hub-gateway-rt.id
        depends_on = [azurerm_subnet.hub-gateway-subnet]
    }
    
    resource "azurerm_route_table" "spoke1-rt" {
        name                          = "spoke1-rt"
        location                      = azurerm_resource_group.hub-nva-rg.location
        resource_group_name           = azurerm_resource_group.hub-nva-rg.name
        disable_bgp_route_propagation = false
    
        route {
        name                   = "toSpoke2"
        address_prefix         = "10.2.0.0/16"
        next_hop_type          = "VirtualAppliance"
        next_hop_in_ip_address = "10.0.0.36"
        }
    
        route {
        name           = "default"
        address_prefix = "0.0.0.0/0"
        next_hop_type  = "vnetlocal"
        }
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_subnet_route_table_association" "spoke1-rt-spoke1-vnet-mgmt" {
        subnet_id      = azurerm_subnet.spoke1-mgmt.id
        route_table_id = azurerm_route_table.spoke1-rt.id
        depends_on = [azurerm_subnet.spoke1-mgmt]
    }
    
    resource "azurerm_subnet_route_table_association" "spoke1-rt-spoke1-vnet-workload" {
        subnet_id      = azurerm_subnet.spoke1-workload.id
        route_table_id = azurerm_route_table.spoke1-rt.id
        depends_on = [azurerm_subnet.spoke1-workload]
    }
    
    resource "azurerm_route_table" "spoke2-rt" {
        name                          = "spoke2-rt"
        location                      = azurerm_resource_group.hub-nva-rg.location
        resource_group_name           = azurerm_resource_group.hub-nva-rg.name
        disable_bgp_route_propagation = false
    
        route {
        name                   = "toSpoke1"
        address_prefix         = "10.1.0.0/16"
        next_hop_in_ip_address = "10.0.0.36"
        next_hop_type          = "VirtualAppliance"
        }
    
        route {
        name           = "default"
        address_prefix = "0.0.0.0/0"
        next_hop_type  = "vnetlocal"
        }
    
        tags = {
        environment = local.prefix-hub-nva
        }
    }
    
    resource "azurerm_subnet_route_table_association" "spoke2-rt-spoke2-vnet-mgmt" {
        subnet_id      = azurerm_subnet.spoke2-mgmt.id
        route_table_id = azurerm_route_table.spoke2-rt.id
        depends_on = [azurerm_subnet.spoke2-mgmt]
    }
    
    resource "azurerm_subnet_route_table_association" "spoke2-rt-spoke2-vnet-workload" {
        subnet_id      = azurerm_subnet.spoke2-workload.id
        route_table_id = azurerm_route_table.spoke2-rt.id
        depends_on = [azurerm_subnet.spoke2-workload]
    }
    

Azure 上的 Terraform 故障排除

排查在 Azure 上使用 Terraform 时遇到的常见问题

后续步骤