DEV Community

Yuvaraj Selvarajan
Yuvaraj Selvarajan

Posted on • Originally published at codemacho.in on

How to handle Multi-Cluster setup for AKS in your local machine

Introduction

Are you tired of juggling of between multiple clusters in your local machine?

Then, it is time to automate your local setup and it's configuration. If you're working as a cluster-admin for anyone of the enterprises, then this blog might be able to help you.

Prerequisites

  1. Bash Scripting
  2. JQ

Pros

  1. Multiple clusters can configured locally in an automated way
  2. If your local configuration is messed up (Which I do often :P), it is easy to fix it.

Manual Way?

To setup a cluster locally, the cluster context needs to be added in your machine.

az aks get-credentials -n ${name} -g ${resource_group} # Setting the Kube Context

Enter fullscreen mode Exit fullscreen mode

Running the above cmd for every cluster can be cumbersome. Let's automate it!!

CODE

Let's jump into the solution right away.

  1. First, try to create a json file that acts as the input for our script.
{
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  },
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  },
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  },
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  },
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  },
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  },
  <Subscription-ID>: {
    region: <region>,
    name: <cluster name>,
    resource_group: <Resource Group Name>
  }
}

Enter fullscreen mode Exit fullscreen mode
  1. Read the data from the input JSON file created at the previous step using the JQ cmd and fetch the list of subscription IDs.
# Getting the list of subscriptoin IDs
subscription_ids=$(echo ${data} | jq -r 'keys | .[]')

Enter fullscreen mode Exit fullscreen mode
  1. Let's loop the list of subscription IDs and update the kube context for every cluster
echo ${subscription_ids} | while read -r subscription_id; do
    echo Setting the AZ Context for the Current Sub ID: ${subscription_id}
    az account set -s ${subscription_id} # Setting the Az Context

    name=$(echo ${data} | jq -re '.'\$subscription_id\'.'name'') # Fetching the Name of the Cluster
    resource_group=$(echo ${data} | jq -re '.'\$subscription_id\'.'resource_group'') # Fetching the name of the resource group
    echo Getting the aks credentials for the cluster : ${name} in the rg: ${resource_group}
    az aks get-credentials -n ${name} -g ${resource_group} # Setting the Kube Context
done

Enter fullscreen mode Exit fullscreen mode

The whole code looks like something below.

#!/bin/bash

# Getting the Cluster Info from the local Json
data=$(cat <./data/cluster_info.json | jq -r)

# Getting the list of subscriptoin IDs
subscription_ids=$(echo ${data} | jq -r 'keys | .[]')
echo ${subscription_ids} | while read -r subscription_id; do
    echo Setting the AZ Context for the Current Sub ID: ${subscription_id}
    az account set -s ${subscription_id} # Setting the Az Context

    name=$(echo ${data} | jq -re '.'\$subscription_id\'.'name'') # Fetching the Name of the Cluster
    resource_group=$(echo ${data} | jq -re '.'\$subscription_id\'.'resource_group'') # Fetching the name of the resource group
    echo Getting the aks credentials for the cluster : ${name} in the rg: ${resource_group}
    az aks get-credentials -n ${name} -g ${resource_group} # Setting the Kube Context
done

Enter fullscreen mode Exit fullscreen mode

NOTE: The above solution is applicable only for AKS Clusters.

Top comments (0)