首页 > 解决方案 > 带有 httpd + LoadBalancer 的 Terraform Azure 2xLinux VM 不起作用

问题描述

我正在尝试创建一个具有两个 centos VM 和一个 Azure 负载均衡器的 Terraform PoC。
每个虚拟机都有一个私有和一个公共 IP,并安装了 httpd 包。
即使元素配置成功,访问负载均衡器的公共 IP 也不会返回默认的 httpd 内容(在 CentOS 虚拟机内 curl localhost 或 IP 返回正确的内容)。CentOS 上没有启用防火墙。
下面是 Terraform 文件。(我使用的位置是西欧)。

问:负载均衡器的配置中缺少什么?所有项目都已配置,terraform 没有错误,当访问负载均衡器的公共 ip 时,我得到超时而不是默认的 apache 页面。

resource "azurerm_resource_group" "test" {
  name     = var.rg_name
  location = var.location
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}


resource "azurerm_virtual_network" "test" {
 name                = var.vnet_name
 address_space       = ["192.168.0.0/16"]
 location            = azurerm_resource_group.test.location
 resource_group_name = azurerm_resource_group.test.name
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}

resource "azurerm_subnet" "test" {
 name                 = var.networks["subnet1"]
 resource_group_name  = azurerm_resource_group.test.name
 virtual_network_name = azurerm_virtual_network.test.name
 address_prefixes      = ["192.168.0.0/24"]
}

resource "azurerm_public_ip" "testlb" {
 name                         = "tf-demo-publicIPForLB"
 location                     = azurerm_resource_group.test.location
 resource_group_name          = azurerm_resource_group.test.name
 sku                          = "Standard"
 allocation_method            = "Static"
 domain_name_label            = "acndemo"
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}

resource "azurerm_lb" "test" {
 name                = "tf-demo-loadBalancer"
 location            = azurerm_resource_group.test.location
 resource_group_name = azurerm_resource_group.test.name
 sku                 = "Standard"
 frontend_ip_configuration {
   name                 = "tf-demo-lb-publicIPAddress"
   public_ip_address_id = azurerm_public_ip.testlb.id
 }
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}


resource "azurerm_lb_backend_address_pool" "test" {
 loadbalancer_id     = azurerm_lb.test.id
 name                = "tf-demo-BackEndAddressPool"
}


resource "azurerm_network_interface_backend_address_pool_association" "test" {
  count = 2
  network_interface_id    = "${azurerm_network_interface.test[count.index].id}"
  ip_configuration_name   = "tf-demo-nic-config${count.index}"
  backend_address_pool_id = azurerm_lb_backend_address_pool.test.id
}

resource "azurerm_lb_probe" "test" {
  resource_group_name = azurerm_resource_group.test.name
  loadbalancer_id     = azurerm_lb.test.id
  name                = "tf-demo-http-running-probe"
  protocol            = "Http"
  port                = 80
  request_path        = "/"
}

resource "azurerm_lb_rule" "test" {
  resource_group_name            = azurerm_resource_group.test.name
  loadbalancer_id                = azurerm_lb.test.id
  name                           = "tf-demo-LBRule"
  protocol                       = "Tcp"
  frontend_port                  = 80
  backend_port                   = 80
  frontend_ip_configuration_name = "tf-demo-lb-publicIPAddress"
  backend_address_pool_id        = azurerm_lb_backend_address_pool.test.id
  probe_id                       = azurerm_lb_probe.test.id
}


resource "azurerm_public_ip" "test" {
 count                        = 2
 name                         = "tf-demo-publicIPForVM${count.index}"
 location                     = azurerm_resource_group.test.location
 resource_group_name          = azurerm_resource_group.test.name
 sku                          = "Standard"
 allocation_method            = "Static"
 domain_name_label            = "acngrvm${count.index}"
 tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}



resource "azurerm_network_interface" "test" {
 count               = 2
 name                = "tf-demo-nic${count.index}"
 location            = azurerm_resource_group.test.location
 resource_group_name = azurerm_resource_group.test.name

 ip_configuration {
   name                          = "tf-demo-nic-config${count.index}"
   subnet_id                     = azurerm_subnet.test.id
   private_ip_address_allocation = "dynamic"
   public_ip_address_id          = "${azurerm_public_ip.test[count.index].id}"
 }
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}


resource "azurerm_network_security_group" "test" {
    name                = "tf-demo-vm-nsg"
    location            = azurerm_resource_group.test.location
    resource_group_name = azurerm_resource_group.test.name

    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}

resource "azurerm_network_interface_security_group_association" "test" {
    count = length(azurerm_network_interface.test)
    network_interface_id      = "${azurerm_network_interface.test[count.index].id}"
    network_security_group_id = azurerm_network_security_group.test.id
}

resource "azurerm_availability_set" "test" {
 name                         = "tf-demo-availabilityset"
 location                     = azurerm_resource_group.test.location
 resource_group_name          = azurerm_resource_group.test.name
 platform_fault_domain_count  = 2
 platform_update_domain_count = 2
 managed                      = true
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }
}




resource "azurerm_linux_virtual_machine" "test" {
    count                   = 2
    name                    = "tfdemovm${count.index}"
    location                = azurerm_resource_group.test.location
    resource_group_name     = azurerm_resource_group.test.name
    network_interface_ids   = [azurerm_network_interface.test[count.index].id]
    size                    = "Standard_DS1_v2"
    admin_username          = "centos"
    computer_name           = "tfdemovm${count.index}"
    availability_set_id     = azurerm_availability_set.test.id

        admin_ssh_key {
    username   = "centos"
    public_key = file("~/.ssh/id_rsa.pub")
        }

    os_disk {
        name                    = "tfdemovm${count.index}_OsDisk${count.index}"
        caching                 = "ReadWrite"
        storage_account_type    = "Standard_LRS"
    }

    source_image_reference {
        publisher = "OpenLogic"
        offer     = "CentOS"
        sku       = "7_8-gen2"
        version   = "latest"
   }
    tags = {
    Owner       = var.tags["Owner"]
    Environment = var.tags["Environment"]
  }

}

标签: azureterraform

解决方案


根据评论。

该问题是由于azurerm_network_security_group.test. 只允许使用端口 22。因此打开端口 80 解决了这个问题。


推荐阅读