Search code examples
kuberneteskubectl

Kubectl wait for service to get external ip


I'm trying to use kubectl to wait for a service to get an external ip assigned. I've been trying to use the below just to get started

kubectl wait --for='jsonpath={.spec.externalTrafficPolicy==Cluster}' --timeout=30s --namespace cloud-endpoints svc/esp-echo

But I keep getting the below error message

error: unrecognized condition: "jsonpath={.spec.externalTrafficPolicy==Cluster}"

Solution

  • It is not possible to pass arbitrary jsonpath and there is already a request for the feature.

    However, you can use a bash script with some sleep and monitor the service using other kubectl commands:

    kubectl get --namespace cloud-endpoints svc/esp-echo --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}"
    

    The above command will return the external IP for the LoadBalancer service for example.

    You can write a simple bash file using the above as:

    #!/bin/bash
    ip=""
    while [ -z $ip ]; do
      echo "Waiting for external IP"
      ip=$(kubectl get svc $1 --namespace cloud-endpoints --template="{{range .status.loadBalancer.ingress}}{{.ip}}{{end}}")
      [ -z "$ip" ] && sleep 10
    done
    echo 'Found external IP: '$ip