background image

20 Apr 2022

Azure 的自動裝機

在之前已經有寫過AWS的裝機處理,這次改用Azure同樣的可以讓系統從scale out到上線不需要人工的處理就可以完成系統的部署與設定

Azure 的VM Scale Set

在AWS是稱為Auto Scaling Group,在Azure中的名稱是Virtual Machine Scale Set(以下簡稱VMSS),這也是一個群組設定虛擬機器的規格, 伸縮的條件, 健康檢查等等的設定

custom_data的資料就是我們準備要設定開機後要執行的腳本,這個設定了以後在azure 的Portal是無法再看到的,所以如果不是使用IaC的話一定要找個地方做紀錄 Azure的VMSS就有內建的檢查機制(extension區塊),可以檢查VM的服務是否正確地被啟動,若沒有在時間內被測試成功的話機器會重新收回部署

在terraform 有另一個設定 extension的module, azurerm_virtual_machine_extension設定如果要設定healthRepairExtension在我測試時是會失敗的,所以一定要在extension區塊中設定。

1resource "azurerm_linux_virtual_machine_scale_set" "sample" {
2  name                = "${var.prefix}-vmss"
3  resource_group_name = data.azurerm_resource_group.main.name
4  location            = data.azurerm_resource_group.main.location
5  zone_balance        = true
6  zones               = [1, 2, 3]
7  sku                 = var.machineSize
8  instances           = var.capacity.minimum
9  admin_username      = "azureuser"
10  custom_data         = filebase64("${path.module}/custom-data.sh")
11
12  admin_ssh_key {
13    username   = "azureuser"
14    public_key = data.azurerm_ssh_public_key.logstash.public_key
15  }
16
17  automatic_instance_repair {
18    enabled      = true
19    grace_period = "PT10M"
20  }
21
22  source_image_reference {
23    publisher = "canonical"
24    offer     = "0001-com-ubuntu-server-focal"
25    sku       = "20_04-lts-gen2"
26    version   = "latest"
27  }
28
29  os_disk {
30    storage_account_type = "Standard_LRS"
31    caching              = "ReadWrite"
32    disk_size_gb         = 30
33  }
34
35  extension {
36    name                      = "healthRepairExtension"
37    publisher                 = "Microsoft.ManagedServices"
38    type                      = "ApplicationHealthLinux"
39    type_handler_version      = "1.0"
40    automatic_upgrade_enabled = true
41    settings                  = <<settings
42      {
43        "protocol" : "http",
44        "port" : 80,
45        "requestPath" : "/"
46      }
47    settings
48  }
49
50  network_interface {
51    name    = "${var.prefix}-NIC"
52    primary = true
53
54    ip_configuration {
55      name      = "internal"
56      primary   = true
57      subnet_id = azurerm_subnet.subnet.id
58    }
59  }
60
61  tags = {
62    env      = var.environment
63    service  = "logstash"
64    createby = "brunojan"
65    docker   = "yes"
66    date     = formatdate("YYYY/MM/DD hh:mm:ss", timestamp())
67    version  = var.ap_version
68  }
69}
70

VMSS的擴展計畫

在Azure的設定呢,說真的我還沒有非常的理解整個設定,但目前看起來的設定較為麻煩...

在Profile中,一定要有一組預設的設定資料,接下來才能在設定其他的擴展策略,所以我直接hard code一組default的設定,這個設定會是主要的擴展策略。 其他的設定基本上可以依照特定的時間,或是情境來做設定

在設定中的時間設定在Azure都是使用ISO-8601的設定標準來設定,這個部份對於我來說真的很不順手,也不容易理解...

1resource "azurerm_monitor_autoscale_setting" "autoscale" {
2  name                = "${var.prefix}-scale-set"
3  resource_group_name = data.azurerm_resource_group.main.name
4  location            = data.azurerm_resource_group.main.location
5  target_resource_id  = azurerm_linux_virtual_machine_scale_set.sample.id
6
7  profile {
8    name = "default"
9
10    capacity {
11      default = var.capacity.minimum
12      minimum = var.capacity.minimum
13      maximum = var.capacity.maximum
14    }
15
16    dynamic "rule" {
17      for_each = length(var.policies) > 0 ? var.policies : []
18      content {
19        metric_trigger {
20          metric_name        = rule.value.metric
21          metric_resource_id = azurerm_linux_virtual_machine_scale_set.sample.id
22          time_grain         = rule.value.grain
23          statistic          = rule.value.statistic
24          time_window        = rule.value.duration
25          time_aggregation   = rule.value.statistic
26          operator           = rule.value.operation
27          threshold          = rule.value.threshold
28          metric_namespace   = "microsoft.compute/virtualmachinescalesets"
29        }
30
31        scale_action {
32          direction = rule.value.action
33          type      = "ChangeCount"
34          value     = rule.value.count
35          cooldown  = rule.value.cooldown
36        }
37      }
38    }
39  }
40
41  dynamic "profile" {
42    for_each = length(var.schedules) > 0 ? var.schedules : []
43
44    content {
45      name = profile.value.name
46
47      capacity {
48        default = profile.value.minimum
49        minimum = profile.value.minimum
50        maximum = profile.value.maximum
51      }
52
53      recurrence {
54        timezone = "Taipei Standard Time"
55        days     = profile.value.days
56        hours    = profile.value.hours
57        minutes  = profile.value.minutes
58      }
59
60    }
61  }
62

Terraform azurerm_virtual_machine_scale_set

這個在未來的版本中已經被棄用了,所以如果有要使用terraform的記得改用azurerm_linux_virtual_machine_scale_set(Linux)與azurerm_windows_virtual_machine_scale_set(Windows) 設定上基本差不多

參考資料

ISO-8601 wiki

Terraform azurerm_virtual_machine_scale_set

文章標籤