如何在 terraform 中的 AKS 群集资源创建的虚拟机规模集或负载均衡器上启用诊断日志?

kjd*_*kjd 3 azure azure-monitoring terraform azure-aks

以下 terraform 资源创建具有虚拟机规模集 (VMSS) 和负载均衡器 (LB) 资源的 AKS 群集。oms_agent目前,通过在 下添加部分,可以在集群资源上启用诊断日志addon_profile

但是,文档没有提及是否有一种方法可以对 . 创建的 VMSSdefault_node_pool和 . 创建的 LB启用诊断network_profile。这可以通过 terraform 实现吗?

或者,集群创建的 VMSS 和 LB 是否有固定的命名方案?如果存在固定的命名方案,此问题的一种解决方案是简单地在正确的资源组中查找具有这些预定义名称的资源来创建日志分析解决方案。

Terraform 文档:
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster# default_node_pool https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/kubernetes_cluster#load_balancer_profile

    resource "azurerm_kubernetes_cluster" "aks-cluster" {
      resource_group_name             = azurerm_resource_group.aks-rg.name
      location                        = azurerm_resource_group.aks-rg.location
      name                            = "my-cluster"
      dns_prefix                      = "my-cluster-aks"
      kubernetes_version              = "1.18.8"
      private_cluster_enabled         = false
      node_resource_group             = "MC_my-cluster-aks"
      api_server_authorized_ip_ranges = [var.authorized_ip]
      service_principal {
        client_id     = var.sp_client_id
        client_secret = var.client_secret
      }
      default_node_pool {
        name                = "default"
        type                = "VirtualMachineScaleSets"
        vm_size             = "Standard_D2_v2"
        node_count          = 4
        enable_auto_scaling = true
        min_count           = 4
        max_count           = 6
        vnet_subnet_id      = azurerm_subnet.aks-vnet-subnet.id
      }
      network_profile {
        network_plugin     = "azure"
        network_policy     = "azure"
        docker_bridge_cidr = var.aks_docker_bridge_cidr
        dns_service_ip     = var.aks_dns_service_ip
        load_balancer_sku  = "standard"
        service_cidr       = var.aks_service_cidr
      }
      addon_profile {
        oms_agent {
          enabled                    = true
          log_analytics_workspace_id = azurerm_log_analytics_workspace.aks_log_ws.id
        }
      }
    }
Run Code Online (Sandbox Code Playgroud)

haz*_*zik 5

负载均衡器的名称固定为kuberneteskubernetes-internal。他们坐在azurerm_kubernetes_cluster.aks-cluster.node_resource_group小组里。但是,由于负载均衡器是动态的,并且仅当您拥有类型为 的服务时才会创建LoadBalancer,因此我怀疑您是否能够通过 terraform 启用监控。

对于VMSS,有一个生成名称的方案:https://github.com/Azure/aks-engine/blob/29c25089d4fa635cb90a3a2cd21d14af47deb40a/pkg/api/types.go#L929-L947,但是它可能无法在地形。所以我认为这是不行的。

此外,在 azurerm terraform 提供程序中提供集群 VMSS 名称时出现了一个问题。然而,它被关闭了,因为无法修复。

因此,解决同样的问题我不得不求助于azurerm_resources数据源

data "azurerm_resources" "aks-cluster-vmss" {
  resource_group_name = "MC_${azurerm_resource_group.aks-rg.name}_my-cluster_${azurerm_resource_group.aks-rg.location}"
  type                = "Microsoft.Compute/virtualMachineScaleSets"
}

resource "azurerm_virtual_machine_scale_set_extension" "monitoring" {
  count = length(data.azurerm_resources.aks-cluster-vmss.resources)

  name                         = "MMAExtension"
  virtual_machine_scale_set_id = data.azurerm_resources.aks-cluster-vmss.resources[count.index].id
  publisher                    = "Microsoft.EnterpriseCloud.Monitoring"
  type                         = "OmsAgentForLinux"
  type_handler_version         = "1.13"
  auto_upgrade_minor_version   = true

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

  protected_settings = <<SETTINGS
  {
      "workspaceKey": "${azurerm_log_analytics_workspace.aks_log_ws.primary_shared_key}"
  }
SETTINGS

  depends_on = [ azurerm_kubernetes_cluster.aks-cluster ]
}
Run Code Online (Sandbox Code Playgroud)

如果您不想计算名称resource_groupazurerm_resources则可以将此代码移至模块并azurerm_kubernetes_cluster.aks-cluster.node_resource_group作为组名称传递。这是因为块中不可能有动态变量count。或者,如果您知道预期的 VMSS 数量,则可以对该数字进行硬编码。