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
- Bash Scripting
- JQ
Pros
- Multiple clusters can configured locally in an automated way
- 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
Running the above cmd for every cluster can be cumbersome. Let's automate it!!
CODE
Let's jump into the solution right away.
- 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>
}
}
- 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 | .[]')
- 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
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
NOTE: The above solution is applicable only for AKS Clusters.
Subscribe to my newsletter
Read articles from Yuvaraj Selvarajan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Yuvaraj Selvarajan
Yuvaraj Selvarajan
Site Reliability Engineer at Shell. Majorly works on AWS, Azure, Kubernetes, Terraform, PowerShell, Bash and Docker.