Get External IP Address From Kubectl Using Powershell

Maciej
Nov 4, 2020

Case

I wanted to link Azure Front Door and AKS, so I just tried to get the EXTERNAL IP assigned to AKS and do this and that.

Example from Microsoft Documentation

Way how do it …

There may be various ways, but this time I will get it from the kubectl command.

It seems that it can be obtained from the Public IP assigned to the load balancer of the node pool, but it seems to be troublesome to search if there are multiple load balancers, so I simply got the EXTERNAL IP with kubectl get serivce.

Result of normal kubectl get service

> kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
azure-vote-back ClusterIP 10.0.37.27 <none> 6379/TCP 56m
azure-vote-front LoadBalancer 10.0.37.127 XXX.XXX.XXX.XXX 80:31284/TCP 56m

Since EXTERNAL-IP exists in azure-vote-front, specify service and display it independently.

> kubectl get service azure-vote-front
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
azure-vote-front LoadBalancer 10.0.37.127 XXX.XXX.XXX.XXX 80:31284/TCP 58m

Take out only EXTERNAL-IP and put it in an appropriate variable as follows.

> $EXTERNAL_IP=$(kubectl get service | ?{ $_ -match '^azure-vote-front'} | Select-Object -first 1 | %{$_ -split "\s+"})[3]
> echo $EXTERNAL_IP
XXX.XXX.XXX.XXX

--

--

Maciej

DevOps Consultant. I’m strongly focused on automation, security, and reliability.