简体   繁体   English

无法访问 Azure IoT Edge API 模块。 请求超时

[英]Azure IoT Edge API module cannot be accessed . Request timed out

I'm implementing API on azure IoT edge runtime installed on a Ubuntu hyper-v VM.我正在 Ubuntu hyper-v VM 上安装的 azure IoT Edge 运行时实现 API。 I have a flask API module implemented and up and running in the edge runtime on Ubuntu vm.我在 Ubuntu vm 的边缘运行时中实现并运行了一个 Flask API 模块。 So i have a requirement to access this API from the host machine which is my local machine running on windows 10. IP address the API module running on is http://172.20.36.197:5000/predict .所以我需要从主机访问这个 API,主机是我在 Windows 10 上运行的本地机器。运行 API 模块的 IP 地址是http://172.20.36.197:5000/predict

this works in the guest(Ubuntu hyper-v VM) machine but request times out every time i try to invoke this call in host machine.这适用于来宾(Ubuntu hyper-v VM)机器,但每次我尝试在主机中调用此调用时请求都会超时。 I have disabled even the firewalls of host VM.我什至禁用了主机 VM 的防火墙。 But no luck.但没有运气。 seems I have to access the IoT edge runtime running on Ubuntu VM from my host machine.似乎我必须从我的主机访问在 Ubuntu VM 上运行的 IoT Edge 运行时。

I just ran the same flask API in a docker container not within the IoT edge runtime as an edge module but within docker inside Ubuntu Hyper-v VM and i can access that API with from the host without any issue.我只是在 docker 容器中运行相同的 Flask API,而不是在 IoT 边缘运行时作为边缘模块,而是在 Ubuntu Hyper-v VM 内的 docker 中运行,我可以从主机访问该 API,没有任何问题。 Following is my code for flask API edge module running on IoT edge runtime以下是我在 IoT 边缘运行时上运行的 Flask API 边缘模块的代码

import os
from azure.iot.device.aio import IoTHubModuleClient
from flask import Flask

port =int(os.environ.get("port",5000))
print("port is ",port)

# Initialize flask app
app = Flask(__name__)

# Create an endpoint
@app.route('/predict')
def predict_chiller_condition():
    print("api func called")
    predictedVal=3
    return 'predicted value is '+str(predictedVal)

if __name__ == "__main__":
    app.run(host='0.0.0.0',port=port)

For anyone who is struggling to solve such a scenario, you should expose your edge module container running on a guest VM by adding ExposedPorts in createOptions section in deployment.template.json as following.对于任何正在努力解决这种情况的人,您应该通过在createOptions部分中添加ExposedPortsExposedPorts在来宾 VM 上运行的边缘模块容器,如下所示。 And also IPv4 address of the VM should be used with the relevant port.并且 VM 的 IPv4 地址也应与相关端口一起使用。

        "modules": {
        "test_flask_api": {
        "version": "1.0",
        "type": "docker",
        "status": "running",
        "restartPolicy": "always",
        "settings": {
          "image": "${MODULES.test_flask_api}",
          "createOptions": {
            "ExposedPorts": {
              "5000/tcp": {}
            },
            "HostConfig": {
              "PortBindings": {
                "5000/tcp": [
                  {
                    "HostPort": "5000"
                  }
                ]
              }
            }
          }
        }
      }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM