使用 terraform 为现有虚拟机启用 Azure Monitor
Enable Azure Monitor for existing Virtual machines using terraform
-
terraform
-
terraform-template-file
-
terraform-provider-openstack
-
terraform-provider-azure
-
terraform0.12+
我正在尝试为现有虚拟机启用 azure 监视器功能,该功能使用 terraform 检查 VM 的运行状况和性能,但我找不到相同的正确文档。你能帮我做同样的事情吗,因为我想对 azure 进行详细的监控?
要在现有虚拟机上启用 VMinsights,您需要拥有虚拟机的数据源,然后为虚拟机的 OS 部署存储帐户、日志分析工作区、日志分析解决方案、日志分析代理和依赖虚拟机 OS 的代理。
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
}
注意: 根据您的 OS 要求添加监控代理和依赖代理。
以上代码我已经在我的实验室中针对我创建的 Windows VM 进行了测试。
terraform
terraform-template-file
terraform-provider-openstack
terraform-provider-azure
terraform0.12+
我正在尝试为现有虚拟机启用 azure 监视器功能,该功能使用 terraform 检查 VM 的运行状况和性能,但我找不到相同的正确文档。你能帮我做同样的事情吗,因为我想对 azure 进行详细的监控?
要在现有虚拟机上启用 VMinsights,您需要拥有虚拟机的数据源,然后为虚拟机的 OS 部署存储帐户、日志分析工作区、日志分析解决方案、日志分析代理和依赖虚拟机 OS 的代理。
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
}
注意: 根据您的 OS 要求添加监控代理和依赖代理。
以上代码我已经在我的实验室中针对我创建的 Windows VM 进行了测试。