我是初学者,我有一个 Flask 应用程序(计算机视觉用例),并且需要将其部署在 VM 规模集(N 系列 GPU 实例)上,因为我需要自动缩放功能(我们不知道有多少客户需要该产品,因此无法估计客户池)。
这将是我第一次直接在虚拟机上部署应用程序(而不是使用应用服务),我不知道如何继续。除了 VMSS 服务之外,我还创建了负载均衡器和 VNet 服务,但不了解如何配置 .我可以轻松地将 Flask 应用程序部署在单个实例上,并使用实例的公共 IP 访问它。但是,我希望通过负载均衡器的 IP 对其进行访问,以便服务在自动扩展时可以将流量传递到所需的实例。请让我知道如何做到这一点。
我还需要在创建每个虚拟机时向其添加启动脚本。可以通过 ARM/Terraform 实现吗?
使用 terraform 在 Azure VM 规模集上部署应用程序。
将 Flask 应用程序部署到具有自动缩放和负载均衡器功能的 Azure VM 规模集 (VMSS) 的要求确实是可能的。
要使 Flask 应用程序可通过负载均衡器访问,您需要配置 VM 规模集以使用负载均衡器的后端池。此设置将在规模集中的虚拟机实例之间分配传入流量。
install_flask.sh
脚本旨在在 Azure VM 规模集实例上设置和运行 Flask 应用程序。
我的地形配置:
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "example" {
name = "vksbtest-rg"
location = "West Europe"
}
resource "azurerm_virtual_network" "example" {
name = "vksbtest-vnet"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
}
resource "azurerm_subnet" "example" {
name = "vksbt-subnet"
resource_group_name = azurerm_resource_group.example.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_security_group" "example" {
name = "vksb-nsg"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
security_rule {
name = "allow-http"
priority = 100
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "5000"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "example" {
subnet_id = azurerm_subnet.example.id
network_security_group_id = azurerm_network_security_group.example.id
}
resource "azurerm_public_ip" "example" {
name = "vks-public-ip"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
allocation_method = "Static"
}
resource "azurerm_lb" "example" {
name = "vksb-lb"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
frontend_ip_configuration {
name = "publicIPAddress"
public_ip_address_id = azurerm_public_ip.example.id
}
}
resource "azurerm_lb_backend_address_pool" "example" {
loadbalancer_id = azurerm_lb.example.id
name = "vksb-backend-pool"
}
resource "azurerm_linux_virtual_machine_scale_set" "example" {
name = "vksb-vmss"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
sku = "Standard_DS1_v2"
instances = 1
admin_username = "adminuser"
disable_password_authentication = true
admin_ssh_key {
username = "adminuser"
public_key = file("~/.ssh/id_rsa.pub") # Make sure to replace this path with the correct path to your public SSH key
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
network_interface {
name = "vksb-nic"
primary = true
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.example.id
load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.example.id]
}
}
}
resource "azurerm_virtual_machine_scale_set_extension" "example" {
name = "customScript"
virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.example.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
settings = jsonencode({
"commandToExecute" = "bash install_flask.sh"
})
protected_settings = jsonencode({
"fileUris" = ["https://example.com/install.sh"],
"commandToExecute" = "bash install_flask.sh"
})
}
output "public_ip_address" {
value = azurerm_public_ip.example.ip_address
}
注意: 这里我没有使用 N 系列 SKU 大小,因为我的 Azure 订阅有配额限制。
部署成功: