当我在Digital Ocean K8S集群上部署https://projectcontour.io/入口控制器时,会自动创建负载均衡器。
我考虑使用Ansible作为K8S的管理工具来实现自动化部署。
在以下任务之后:
- name: retrieve file
get_url:
url: https://projectcontour.io/quickstart/contour.yaml
dest: /testing/contour.yaml
register: download_contour
- name: create deployment
k8s:
src: /testing/deployment.yml
when: download_contour.changed
我想等到轮廓得到外部ip地址分配,然后继续其他任务。下面是我本地电脑上的一个例子:
kubectl get -n projectcontour service envoy -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
envoy LoadBalancer 10.96.226.84 172.18.255.200 80:31092/TCP,443:30362/TCP 2d15h app=envoy
如何等待,直到envoy
LoadBalancer获得外部ip地址分配在Ansible?
我还没有测试过,但我认为你可以尝试这样做:
- shell: if [[ $(kubectl get services envoy -n projectcontour --output jsonpath='{.status.loadBalancer.ingress[0]}') ]]; then exit 0; else exit 1; fi;
register: wait_for_ext_ip
until: wait_for_ext_ip.rc == 0
retries: 10
delay: 5
你应该使用until循环:
tasks:
- name: run kubectl to retreive external IP - Wait for task to complete
shell: "kubectl get -n projectcontour service envoy -o wide | awk '{ print $4}' | grep -v EXT"
register: k_ext_ip
until: k_ext_ip.stdout.find("1.2.3.4") != -1
retries: 6
delay: 10
对于shell命令,我只使用了一些基本的linux命令。最好将这些命令替换为与Kubectl对齐的命令,以便仅查看EXTERNAL-IP。