首页 > 解决方案 > 在 shell 脚本中使用 while 循环与 AKS CLI 并行创建多个 AKS 群集

问题描述

az aks create我创建了运行不同命令的 shell 脚本。

我有很多变量,但现在我在 while 循环中只更改集群名称。

shell脚本的结构是:

cnt=1

while [ $cnt -lt 3 ]
do 
  aksName=AKS0$cnt
  # more AKS VARs

  # and then CLI command:
  az aks create --name $aksName #--<MORE AKS CREATE SWITCHES>

  cnt=`expr $cnt + 1`
done

它确实创建了 2 个 AKS 集群,但是 1 接 1 串联,有没有办法并行创建多个集群,可能正在使用 ansible,如果有,请告诉我如何?

谢谢

标签: azureshellansibleazure-aks

解决方案


我建议使用像 Pulumi 或 Terraform 这样的 IaC 工具来创建 AKS 集群而不是 Ansible。这是用于创建 2 个 AKS 集群的 Terraform 代码。如果您想要更多,只需更新计数值,Terraform 还将并行创建这些集群:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=2.80.0"
    }
  }

  required_version = "=1.0.5"
}

provider "azurerm" {
  features {}
}

data "azurerm_client_config" "current" {
}

locals {
  rg_name  = "demo"
  location = "westeurope"
  name     = "demo"
  common_tags = {
    env       = "demo"
    managedBy = data.azurerm_client_config.current.client_id
  }
}

resource "azurerm_resource_group" "demo" {
  name     = local.rg_name
  location = local.location
}

resource "azurerm_kubernetes_cluster" "aks" {
  count               = 2
  name                = "${local.name}-${count.index}"
  location            = azurerm_resource_group.demo.location
  resource_group_name = azurerm_resource_group.demo.name
  dns_prefix          = "${local.name}-${count.index}"
  tags                = local.common_tags

  identity {
    type = "SystemAssigned"
  }

  default_node_pool {
    name       = "default"
    node_count = 3
    vm_size    = "Standard_D2_v2"
  }
}

推荐阅读