<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Yuvaraj Selvarajan</title>
    <description>The latest articles on Forem by Yuvaraj Selvarajan (@yuvaraj_selvarajan).</description>
    <link>https://forem.com/yuvaraj_selvarajan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F716313%2Fd21079c1-ff49-4294-ab78-7b1fa9f444b0.jpeg</url>
      <title>Forem: Yuvaraj Selvarajan</title>
      <link>https://forem.com/yuvaraj_selvarajan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yuvaraj_selvarajan"/>
    <language>en</language>
    <item>
      <title>How to handle Multi-Cluster setup for AKS in your local machine</title>
      <dc:creator>Yuvaraj Selvarajan</dc:creator>
      <pubDate>Tue, 09 May 2023 17:21:27 +0000</pubDate>
      <link>https://forem.com/yuvaraj_selvarajan/how-to-handle-multi-cluster-setup-for-aks-in-your-local-machine-4931</link>
      <guid>https://forem.com/yuvaraj_selvarajan/how-to-handle-multi-cluster-setup-for-aks-in-your-local-machine-4931</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Are you tired of juggling of between multiple clusters in your local machine?&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Bash Scripting&lt;/li&gt;
&lt;li&gt;&lt;a href="https://gist.github.com/olih/f7437fb6962fb3ee9fe95bda8d2c8fa4"&gt;JQ&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pros
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Multiple clusters can configured locally in an automated way&lt;/li&gt;
&lt;li&gt;If your local configuration is messed up (Which I do often :P), it is easy to fix it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Manual Way?
&lt;/h2&gt;

&lt;p&gt;To setup a cluster locally, the cluster context needs to be added in your machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;az aks get-credentials -n ${name} -g ${resource_group} # Setting the Kube Context

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Running the above cmd for every cluster can be cumbersome. Let's automate it!!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  CODE
&lt;/h2&gt;

&lt;p&gt;Let's jump into the solution right away.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First, try to create a json file that acts as the input for our script.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  },
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  },
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  },
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  },
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  },
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  },
  &amp;lt;Subscription-ID&amp;gt;: {
    region: &amp;lt;region&amp;gt;,
    name: &amp;lt;cluster name&amp;gt;,
    resource_group: &amp;lt;Resource Group Name&amp;gt;
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Read the data from the input JSON file created at the previous step using the JQ cmd and fetch the list of subscription IDs.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Getting the list of subscriptoin IDs
subscription_ids=$(echo ${data} | jq -r 'keys | .[]')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Let's loop the list of subscription IDs and update the kube context for every cluster
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole code looks like something below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash

# Getting the Cluster Info from the local Json
data=$(cat &amp;lt;./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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;NOTE: The above solution is applicable only for AKS Clusters.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>kubernetes</category>
      <category>bash</category>
      <category>scripting</category>
      <category>jq</category>
    </item>
  </channel>
</rss>
