Skip to content

Instantly share code, notes, and snippets.

@EamonKeane
Last active January 29, 2018 17:19
Show Gist options
  • Save EamonKeane/4e371650d76f28c26bd340817517651e to your computer and use it in GitHub Desktop.
Save EamonKeane/4e371650d76f28c26bd340817517651e to your computer and use it in GitHub Desktop.
SSH to azure worker node on Azure kubernetes service (AKS)
#!/usr/bin/env bash
#This assumes you are attempting to connect to Azure AKS from outside Azure.
#By default Azure doesn't provide any external access or assign an IP address to worker nodes, so this script:
#1. Creates a public ip address in the same region as the cluster
#2. Associates the ip address with the NIC card attached to the worker VM
#Azure creates a separate group for managed clusters e.g. MC_my_cluster_my_cluster_group_my_region
#Azure creates node names in the format aks_agentpool_0000000_0
#The respective NIC cards have the same number as the node_number e.g. aks-agentpool-0000000-nic-0
#--------User Input Variables---------
node_name="aks-agentpool-0000000-0" # Run kubectl get nodes and choose the one you would like to ssh into
ssh_username="azureuser" # ssh username chosen when setting up cluster, default is azureuser
#-------------------------------------
IFS=- read -a array <<< "$node_name" #splitting node name up based on - delimiter
node_number="${array[3]}" # e.g. 0
agentpool_number="${array[2]}" # e.g. 0000000
managed_cluster_name=$(kubectl get node ${node_name} -o jsonpath='{.metadata.labels.kubernetes\.azure\.com/cluster}')
IFS=_ read -a array <<< "$managed_cluster_name" #splitting cluster name up and extracting region from last element
region="${array[3]}"
agentpool_prefix=aks-agentpool-${agentpool_number} #e.g. aks-agentpool-0000000
nic_name=${agentpool_prefix}"-nic-"${node_number} #e.g. aks-agentpool-0000000-nic-0
ip_name=ip${node_number} #name to give to the created ip address e.g. ip0
az network public-ip create -g ${managed_cluster_name} -n ${ip_name} -l ${region}
ipconfig=$(az network nic ip-config list --nic-name ${nic_name} -g ${managed_cluster_name} -o json | jq -r '.[] | .name')
az network nic ip-config update -g ${managed_cluster_name} --nic-name ${nic_name} --name ${ipconfig} --public-ip-address ${ip_name}
public_ip=$(az network public-ip show -g ${managed_cluster_name} -n ${ip_name} -o json | jq -r '.ipAddress')
ssh ${ssh_username}@${public_ip}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment