【问题标题】:How to do a health-check to Spring Boot app running inside Windows Docker Windows Container with Ansible?如何使用 Ansible 对在 Windows Docker Windows 容器中运行的 Spring Boot 应用程序进行健康检查?
【发布时间】:2017-02-23 11:31:56
【问题描述】:

我想使用 Ansible 将 Spring Boot 应用程序配置到 Windows Docker 容器中,该容器托管在 Windows Docker 主机上(在 Mac 上虚拟化,但这是另一个故事;))。我已经成功使用Ansible Windows Modulesprovision a Spring Boot app to Windows

我在最后一章,只是想在最后添加一个健康检查。正如没有 Docker 的博文中所述,这很容易:

  - name: Wait until our Spring Boot app is up & running
    win_uri:
      url: "http://localhost:8080/health"
      method: GET
    register: result
    until: result.status_code == 200  
    retries: 5
    delay: 5

现在有了 Docker Windows 容器there´s a known limitation,所以你现在不能使用localhost。我们必须使用 Windows Docker Containers 内部 Hyper-V IP 地址(在NetworkSettings.Networks.nat.IPAddress 的 JSON 输出中运行 docker inspect <yourContainerIdHere> 后可以看到 IP)。

我的问题是:如何获取 Windows Docker 容器的 Hyper-V 内部 IP 地址,将其输出到调试语句中并进行类似于我概述的健康检查?

【问题讨论】:

    标签: windows docker spring-boot ansible


    【解决方案1】:

    在进行一次简单的健康检查之后,我找到了解决方案。

    首先,我们必须获取 Docker 容器的 IP 地址,这可以在 Powershell 上使用以下命令轻松完成:

    docker inspect --format '{{ .NetworkSettings.Networks.nat.IPAddress }}' <yourInstanceIdHere>
    

    我们只需要使用win_shell module

    但是因为这使用了 Docker 模板机制,Jinja2 模板不知道,这次它不应该解释这些命令。我们必须正确地避开花括号,这在this so q&a already 中有概述。您可以使用建议的解决方案之一:

    "{%raw%}"{{ .NetworkSettings.Networks.nat.IPAddress }}"{%endraw%}"

    "{{'{{'}} .NetworkSettings.Networks.nat.IPAddress {{'}}'}}" - 两者都可以在这里为我们工作。

    现在从这个输出中获取 IP 地址,我尝试只注册一个结果并进行健康检查。遗憾的是这不起作用,因为返回的 stdoutstdout_lines 确实包含您的 IP,但也再次包含 Docker 模板 - 但这次没有转义序列,这反过来会让任务失败(作为评论来自Davide Guerri in this so answer 已报告)。

    lanwen 的以下评论为救援提供了建议:我们可以将第一个 win_shell 输出通过管道传输到一个临时文本文件 container_ip.txt 然后 - 在第二个 win_shell 任务中 - 我们只需读取该文件的内容并注册一个输出变量。

    这似乎很简单,我们再次使用win_shell

    win_shell: cat container_ip.txt
    register: win_shell_txt_return
    

    但是,嘿,这还不是全部 -> 因为在 Windows 上,有 nice carriage return line feeds :),它会在最后用 \r\n 污染我们的 IP 地址,并让我们的健康检查再次失败。

    但同样,有帮助:Ansible 有一个很好的 splitlines 功能(这有点没有记录......)我们只需要使用尾随 [0] 来获得知识产权:

    "{{ win_shell_txt_return.stdout.splitlines()[0] }}"
    

    现在我们可以按照最初的意愿进行健康检查。这是完整的解决方案:

      - name: Obtain the Docker Container´s internal IP address (because localhost doesn´t work for now https://github.com/docker/for-win/issues/458)
        win_shell: "docker inspect -f {% raw %}'{{ .NetworkSettings.Networks.nat.IPAddress }}' {% endraw %} {{spring_boot_app_name}} {{ '>' }} container_ip.txt"
    
      - name: Get the Docker Container´s internal IP address from the temporary txt-file (we have to do this because of templating problems, see https://stackoverflow.com/a/32279729/4964553)
        win_shell: cat container_ip.txt
        register: win_shell_txt_return
    
      - name: Define the IP as variable
        set_fact:
          docker_container_ip: "{{ win_shell_txt_return.stdout.splitlines()[0] }}"
    
      - debug:
          msg: "Your Docker Container has the internal IP {{ docker_container_ip }} --> Let´s do a health-check against this URI: 'http://{{ docker_container_ip }}:{{spring_boot_app.port}}/{{spring_boot_app.health_url_ending}}'"
    
      - name: Wait until our Spring Boot app is up & running
        win_uri:
          url: "http://{{ docker_container_ip }}:8080/health"
          method: GET
        register: health_result
        until: health_result.status_code == 200  
        retries: 5
        delay: 5
    

    【讨论】:

      猜你喜欢
      • 2019-12-22
      • 1970-01-01
      • 2021-05-27
      • 2019-03-08
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 2019-09-08
      相关资源
      最近更新 更多