微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 terraform 为现有虚拟机启用 Azure Monitor

如何解决使用 terraform 为现有虚拟机启用 Azure Monitor

我正在尝试为现有虚拟机启用 azure 监视器功能,该功能使用 terraform 检查 VM 的运行状况和性能,但我找不到正确的文档。你能帮我做同样的事情吗,因为我想要对 azure 进行详细的监控?

解决方法

要在现有 Vm 上启用 VMinsights,您需要拥有 VM 的数据源,然后部署存储帐户、日志分析工作区、日志分析解决方案、VM 操作系统的日志分析代理和操作系统的依赖代理虚拟机。

provider "azurerm" {
  features {}
}
data "azurerm_virtual_machine" "example" {
  name                = "test1"
  resource_group_name = "testgroup"# where your VM resides in your subscription
}

output "virtual_machine_id" {
  value = data.azurerm_virtual_machine.example.id
}

resource "azurerm_storage_account" "main" {
  name                     = "vminsightstest1234"
  resource_group_name      = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
  location                 = data.azurerm_virtual_machine.example.location # which region your VM resides 
  account_tier             = "Standard"
  account_replication_type = "GRS"
}

resource "azurerm_log_analytics_workspace" "LAW" {
  name                = "vminsights-logAnalytics"
 location            = data.azurerm_virtual_machine.example.location #which region your VM resides 
  resource_group_name = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
 sku                 = "PerGB2018"
  retention_in_days   = 30
}

resource "azurerm_log_analytics_solution" "example" {
  solution_name         = "ContainerInsights"
  location              = data.azurerm_virtual_machine.example.location # which region your VM resides 
  resource_group_name   = data.azurerm_virtual_machine.example.resource_group_name # where your VM resides in your subscription
  workspace_resource_id = azurerm_log_analytics_workspace.LAW.id
  workspace_name        = azurerm_log_analytics_workspace.LAW.name
  plan {
    publisher = "Microsoft"
    product   = "OMSGallery/ContainerInsights"
  }
}
# Agent for Linux
resource "azurerm_virtual_machine_extension" "OMS" {
  name                       = "test-OMSExtension"
  virtual_machine_id         =  data.azurerm_virtual_machine.example.id
  publisher                  = "Microsoft.EnterpriseCloud.Monitoring"
  type                       = "OmsAgentForLinux"
  type_handler_version       = "1.13"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
      "workspaceId" : "${azurerm_log_analytics_workspace.LAW.workspace_id}"
    }
  SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
    {
      "workspaceKey" : "${azurerm_log_analytics_workspace.LAW.primary_shared_key}"
    }
  PROTECTED_SETTINGS
}

# Dependency Agent for Linux
resource "azurerm_virtual_machine_extension" "da" {
  name                       = "DAExtension"
  virtual_machine_id         =  data.azurerm_virtual_machine.example.id
  publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
  type                       = "DependencyAgentLinux"
  type_handler_version       = "9.5"
  auto_upgrade_minor_version = true

}
#Agent for Windows
resource "azurerm_virtual_machine_extension" "MMA" {
  name                       = "test-MMAextension"
  virtual_machine_id         =  data.azurerm_virtual_machine.example.id
  publisher                  = "Microsoft.EnterpriseCloud.Monitoring"
  type                       = "MicrosoftMonitoringAgent"
  type_handler_version       = "1.0"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
      "workspaceId" : "${azurerm_log_analytics_workspace.LAW.workspace_id}"
    }
  SETTINGS

  protected_settings = <<PROTECTED_SETTINGS
    {
      "workspaceKey" : "${azurerm_log_analytics_workspace.LAW.primary_shared_key}"
    }
  PROTECTED_SETTINGS
}

# Dependency Agent for Windows
resource "azurerm_virtual_machine_extension" "da" {
  name                       = "DAExtension"
  virtual_machine_id         =  data.azurerm_virtual_machine.example.id
  publisher                  = "Microsoft.Azure.Monitoring.DependencyAgent"
  type                       = "DependencyAgentWindows"
  type_handler_version       = "9.5"
  auto_upgrade_minor_version = true

}

注意:根据您的操作系统要求添加监控代理和依赖代理。

我在实验室中为我创建的 Windows VM 测试了上述代码。

enter image description here

enter image description here

enter image description here

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。