3

我正在使用 Terraform 生成 Azure Kubernetes 服务集群(以及其他东西),当我运行 Terraform 时,它会自动生成 AKS 集群使用的不同资源。我可以在 AKS 群集上选择自定义名称,但自动生成的资源接近随机名称。

有没有办法在 Terraform 中的这些自动生成的资源上选择我自己的自定义名称?

例子:

resource "azurerm_kubernetes_cluster" "compute" {
    name                = "MyCluster"
    location            = "westeurope"
    resource_group_name = "my-rg"
    dns_prefix          = "something"

    linux_profile {
         admin_username = "azureuser"

    agent_pool_profile {
        name            = "default"
        count           = "1"
        vm_size         = "Standard_NC6"
        os_type         = "Linux"
   }
}

这会产生:

myCluster- Kubernetes 服务

aks-agentpool-74438003-nsg- 网络安全组

aks-agentpool-74438003-routetable- 路由表

aks-default-74438003-0- 虚拟机

aks-default-74438003-0_OsDisk_1_5d379bc3205545e1bcd3f88ec9605- 磁盘

aks-default-74438003-nic-0- 网络接口

aks-vnet-74438003- 虚拟网络

default-availabilitySet-74438003- 可用性集

例如,我可以选择是否需要那个aks前缀吗?那个重复的数字是多少?我可以基本自定义这些吗?

4

1 回答 1

2

您可以自定义其他资源的前缀或名称。
但是,只有当您自己创建这些资源并明确引用它们时。

请参阅Azure 文档页面

基于来自 Github的此AKS 模块的示例代码

variable "my-prefix" {
  default = "myCluster"
  description = "The prefix name to give to all my resources"
}

variable "location" {
  default     = "West Europe"
  description = "The Azure Region in which all resources in this example should be provisioned"
}

variable "kubernetes_client_id" {
  description = "The Client ID for the Service Principal to use for this Managed Kubernetes Cluster"
}

variable "kubernetes_client_secret" {
  description = "The Client Secret for the Service Principal to use for this Managed Kubernetes Cluster"
}

variable "public_ssh_key_path" {
  description = "The Path at which your Public SSH Key is located. Defaults to ~/.ssh/id_rsa.pub"
  default     = "~/.ssh/id_rsa.pub"
}

resource "azurerm_resource_group" "test" {
  name     = "${var.my-prefix}-anw-resources"
  location = "${var.location}"
}

resource "azurerm_route_table" "test" {
  name                = "${var.my-prefix}-routetable"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"

  route {
    name                   = "default"
    address_prefix         = "10.100.0.0/14"
    next_hop_type          = "VirtualAppliance"
    next_hop_in_ip_address = "10.10.1.1"
  }
}

resource "azurerm_virtual_network" "test" {
  name                = "${var.my-prefix}-network"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  address_space       = ["10.1.0.0/16"]
}

resource "azurerm_subnet" "test" {
  name                 = "${var.my-prefix}-subnet"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  address_prefix       = "10.1.0.0/24"
  virtual_network_name = "${azurerm_virtual_network.test.name}"

  # this field is deprecated and will be removed in 2.0 - but is required until then
  route_table_id = "${azurerm_route_table.test.id}"
}

resource "azurerm_network_security_group" "test" {
  name                = "${var.my-prefix}-nsg"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_virtual_network.test.name}"

  security_rule {
    name                       = "port_80"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefixes    = "0.0.0.0/0"
    destination_address_prefix = "*"
  }
}

resource "azurerm_subnet_route_table_association" "test" {
  subnet_id      = "${azurerm_subnet.test.id}"
  route_table_id = "${azurerm_route_table.test.id}"
}

resource "azurerm_subnet_network_security_group_association" "test" {
  subnet_id                 = "${azurerm_subnet.test.id}"
  network_security_group_id = "${azurerm_network_security_group.test.id}"
}

resource "azurerm_kubernetes_cluster" "test" {
  name                = "${var.my-prefix}-anw"
  location            = "${azurerm_resource_group.test.location}"
  dns_prefix          = "${var.my-prefix}-anw"
  resource_group_name = "${azurerm_resource_group.test.name}"

  linux_profile {
    admin_username = "acctestuser1"

    ssh_key {
      key_data = "${file(var.public_ssh_key_path)}"
    }
  }

  agent_pool_profile {
    name            = "agentpool"
    count           = "2"
    vm_size         = "Standard_DS2_v2"
    os_type         = "Linux"
    os_disk_size_gb = 30

    # Required for advanced networking
    vnet_subnet_id = "${azurerm_subnet.test.id}"
  }

  service_principal {
    client_id     = "${var.kubernetes_client_id}"
    client_secret = "${var.kubernetes_client_secret}"
  }

  network_profile {
    network_plugin = "azure"
  }
}
于 2019-02-19T14:00:33.737 回答