Use Azure CLI to obtain Kubernetes Load Balancer's public IP

2/15/2021

I have Kubernetes running on Azure, and it uses a load balancer. Azure has assigned a public IP address to this load balancer, and for the purposes of setting up an Application Gateway via the Azure CLI I wish to obtain the IP address of the k8s load balancer using the Azure CLI.

I can obtain the load balancer resource using...

az resource list --resource-group $k8sResourceGroup --query "[?type=='Microsoft.Network/loadBalancers']"

I obtain the id from that response and pass it to...

az network lb show --id $loadBalancer.id --output json | convertfrom-json).frontendIpConfigurations

The frontendIpConfigurations array contains two objects: an inbound IP address (resource) and an outbound IP address (resource). I filter down to the inbound IP resource like this...

az network lb show --id $loadBalancer.id --output json | convertfrom-json).frontendIpConfigurations | where-object { $null -ne $_.loadBalancingRules }).loadBalancingRules[0].id

I then have something which looks a lot like the IP address resource's ID - it matches that which I see in the Azure portal. But if I then request the resource itself, using...

az network public-ip show --ids $loadBalancingRuleId --query "{fqdn: dnsSettings.fqdn, address: ipAddress}"

...I get...

ResourceNotFoundError: The Resource 'Microsoft.Network/publicIPAddresses/kubernetes' under resource group 'my resource group name' was not found. For more details please go to https://aka.ms/ARMResourceNotFoundFix

Is there a straightforward method of obtaining an AKS load balancer's public inbound IP address?

-- awj
azure
azure-aks
azure-cli
kubernetes
kubernetes-ingress

2 Answers

2/16/2021

Is there a straightforward method of obtaining an AKS load balancer's public inbound IP address?

All the public IPs associated with the Load Balancer of the AKS are the inbound IPs, except the outbound IP. And all the inbound IPs are the public IPs of the services in the AKS. It means if you know how many services with the load balancer type, then you will know how many public inbound IPs associated with the load balancer of the AKS.

According to this cognition, you just need to get the public IP address of the service, then you can get the resource ID of this public IP through Azure CLI:

az network public-ip list --query "[?ipAddress=='publicIPAddress'].id" -o tsv
-- Charles Xu
Source: StackOverflow

2/15/2021

Try this (assuming, your LB only has one inbound IP address. Otherwise $pipId is an array)

$pipId = $(az network lb show --id $loadBalancer.id --query "frontendIpConfigurations | [?loadBalancingRules != null].publicIpAddress.id" -o tsv)

$ip = (az network public-ip show --ids $pipId --query "ipAddress" -o tsv)
-- silent
Source: StackOverflow