How to wait until Kubernetes assigned an external IP to a LoadBalancer service? How to wait until Kubernetes assigned an external IP to a LoadBalancer service? kubernetes kubernetes

How to wait until Kubernetes assigned an external IP to a LoadBalancer service?


Just to add to the answers here, the best option right now is to use a bash script. For convenience, I've put it into a single line that includes exporting an environmental variable.

Command to wait and find Kubernetes service endpoint

bash -c 'external_ip=""; while [ -z $external_ip ]; do echo "Waiting for end point..."; external_ip=$(kubectl get svc NAME_OF_YOUR_SERVICE --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}"); [ -z "$external_ip" ] && sleep 10; done; echo "End point ready-" && echo $external_ip; export endpoint=$external_ip'

I've also modified your script so it only executes a wait if the ip isn't available. The last bit will export an environment variable called "endpoint"

Bash Script to Check a Given Service

Save this as check-endpoint.sh and then you can execute $sh check-endpoint.sh SERVICE_NAME

#!/bin/bash# Pass the name of a service to check ie: sh check-endpoint.sh staging-voting-app-vote# Will run forever...external_ip=""while [ -z $external_ip ]; do  echo "Waiting for end point..."  external_ip=$(kubectl get svc $1 --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}")  [ -z "$external_ip" ] && sleep 10doneecho 'End point ready:' && echo $external_ip

Using this in a Codefresh Step

I'm using this for a Codefresh pipeline and it passes a variable $endpoint when it's done.

  GrabEndPoint:    title: Waiting for endpoint to be ready    image: codefresh/plugin-helm:2.8.0    commands:      - bash -c 'external_ip=""; while [ -z $external_ip ]; do echo "Waiting for end point..."; external_ip=$(kubectl get svc staging-voting-app-vote --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}"); [ -z "$external_ip" ] && sleep 10; done; echo "End point ready-" && echo $external_ip; cf_export endpoint=$external_ip'


This is little bit tricky by working solution:

kubectl get service -w load-balancer -o 'go-template={{with .status.loadBalancer.ingress}}{{range .}}{{.ip}}{{"\n"}}{{end}}{{.err}}{{end}}' 2>/dev/null | head -n1


Maybe this is not the solution that you're looking for but at least it has less lines of code:

until [ -n "$(kubectl get svc load-balancer -o jsonpath='{.status.loadBalancer.ingress[0].ip}')" ]; do    sleep 10done