Home > Mobile >  create multiple AKS clusters in parallel with AKS CLI using while loop in shell script
create multiple AKS clusters in parallel with AKS CLI using while loop in shell script

Time:10-10

I created the shell script running different az aks create commands.

I have many VARIABLES, but for now I am changing in while loop only the cluster name.

The structure of the shell script is:

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

It does create 2 AKS clusters, but 1 by 1 in series, is there any way to create multiple clusters in parallel, might be using ansible, if so please let me know how?

Thank you

CodePudding user response:

i would suggest to use an IaC Tool like Pulumi or Terraform for creating a AKS Cluster instead of Ansible. Here is Terraform code to create 2 AKS Clusters. If you want more, just update the count value, Terraform will also create those clusters parallel:

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"
  }
}

CodePudding user response:

I agree with @philip-welz. An IaaC tool that can resolve dependencies and achieve parallel provisioning of infrastructure would be the best way to go.

Just in case, you want to stick to Azure CLI: For asynchronous operations using the Azure CLI, many commands offer a --no-wait parameter, which allows other commands to run before it returns a response from the Azure Resource Manager. [Reference]

The az aks create command allows for the --no-wait option. Thus you can modify your shell script as follows:

cnt=1

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

  # and then CLI command:
  az aks create --no-wait --name $aksName #--<MORE AKS CREATE SWITCHES>
  echo Creation of cluster $aksName initiated
  cnt=`expr $cnt   1`
done

This will initiate the creation of the AKS clusters in the specific sequence but every iteration will not wait for the cluster in that iteration to be created before moving to the next iteration. Thus achieving a pseudo-parallel workflow for creating multiple AKS clusters using Azure CLI and shell scripts only.

  • Related