kubectl等待服务获取外部ip

Kubectl wait for service to get external ip

我正在尝试使用 kubectl 等待服务分配外部 ip。我一直在尝试使用下面的方法来开始

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

但我不断收到以下错误消息

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

您需要提供一个条件。喜欢:

kubectl -n foobar wait --for=condition=complete --timeout=32s foo/bar

这里有一篇很好的文章解释了这一点:https://mrkaran.dev/posts/kubectl-wait/

在您的情况下,您可以使用 k8s probes.

之一

无法任意传递 jsonpath 并且已经有 request for the feature.

但是,您可以使用 bash 脚本并使用其他 kubectl 命令监视服务:

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

上述命令将 return LoadBalancer 服务的外部 IP。

您可以使用上面的代码编写一个简单的 bash 文件:

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

现在 kubectl wait 不适合解决这个问题。从 Kubernetes v1.23 开始(参见 this merged PR),将 jsonpath 与 kubectl wait 一起使用很可能。在那之前你可以这样做:

until kubectl get svc/esp-echo --namespace cloud-endpoints --output=jsonpath='{.status.loadBalancer}' | grep "ingress"; do : ; done

甚至增强命令 using timeout (brew install coreutils on a Mac) 以防止命令无限地 运行:

timeout 10s bash -c 'until kubectl get service/tekton-dashboard-external-svc-manual -n tekton-pipelines --output=jsonpath='{.status.loadBalancer}' | grep "ingress"; do : ; done'

, where a AWS Elastic Load Balancer (ELB) gets provisioned when one creates a Service with the type LoadBalancer. That's most likely comparable to other cloud providers behaviour since it's part of the official kubernetes.io docs:

On cloud providers which support external load balancers, setting the type field to LoadBalancer provisions a load balancer for your Service. The actual creation of the load balancer happens asynchronously, and information about the provisioned balancer is published in the Service's .status.loadBalancer field.

Krishna Chaurasia 的回答也使用了这个字段,但我们希望有一个单行代码,无需额外的 bash 脚本即可使用,就像 kubectl wait 一样。因此,我们的解决方案结合了服务器故障上关于 "watching" the output of a command until a particular string is observed and then exit 的解决方案以及使用 until 循环。

根据 Krishna Chaurasia 的回答更新,kubectl 已经实现了可以等待任意 jsonpath 值的功能。

但是,要注意的是该值只会是 primitive value, excluding nested primitive value (map[string]interface{} or []interface{})