Cluster Autoscaler on AKS

#!/bin/bash

#How to enable k8s Cluster Autoscaler with AKS
# az group create -g aksscaling -l japaneast
# az aks create -g aksscaling -n aksscaling -c 3 -k 1.10.3 -s Standard_D32s_v3

readonly clusterName="aksscaling"

masterResourceGroup=$(az aks list -o tsv --query "[?name == '${clusterName}'].resourceGroup")
az aks get-credentials -g ${masterResourceGroup} -n ${clusterName}
nodeResourceGroup=$(az aks show -g ${masterResourceGroup} -n ${clusterName} --query 'nodeResourceGroup' -o tsv)

#1. check your subscription ID
subID=$(az account show -o tsv --query id)
tenantID=$(az account show -o tsv --query tenantId)

#2. create a service principal
spName="sp-for-k8s-cluster-autoscaler"
az ad sp delete --id http://$spName
clientSecret=$(az ad sp create-for-rbac --name ${spName} --role="Contributor" --scopes="/subscriptions/${subID}" --query password --output tsv)
clientID=$(az ad sp show --id http://$spName --query appId --output tsv)

#3. encode each parameters with base64
clientID64=$(echo $clientID|base64)
clientSecret64=$(echo $clientSecret|base64)
resourceGroup64=$(echo $masterResourceGroup|base64)
subID64=$(echo $subID|base64)
tenantID64=$(echo $tenantID|base64)
clusterName64=$(echo $clusterName|base64)
nodeResourceGroup64=$(echo $nodeResourceGroup|base64)

#4. replace the placeholders in the manifest
manifest=$(curl -o - https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/azure/cluster-autoscaler-containerservice.yaml)
manifest=$(echo -e "$manifest" | \
sed -e 's/{{ ca_version }}/v1.3.0/' \
-e 's/<base64\-encoded\-client\-id>/'$clientID64'/' \
-e 's/<base64\-encoded\-client\-secret>/'$clientSecret64'/' \
-e 's/<base64\-encoded\-resource\-group>/'$resourceGroup64'/' \
-e 's/<base64\-encode\-subscription\-id>/'$subID64'/' \
-e 's/<base64\-encoded\-tenant\-id>/'$tenantID64'/' \
-e 's/<base64\-encoded\-clustername>/'$clusterName64'/' \
-e 's/<base64\-encoded\-node\-resource\-group>/'$nodeResourceGroup64'/')

#5. deploy the Autoscaler into AKS
echo -e "$manifest" | kubectl create -f -
タイトルとURLをコピーしました