在Linux容器上触发Azure函数HTTP-函数无法正常工作404 [英] Azure function HTTP triggered on linux container - function not working 404

查看:57
本文介绍了在Linux容器上触发Azure函数HTTP-函数无法正常工作404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困住了,也许你可以帮帮我. (更新:经过调查,我确实知道它不起作用,因为在构建/发行后,Azure功能的wwwroot文件夹为空.主要问题是为什么)

I am stuck and maybe you can help me out. (Update: After investigation, I do know that it is not working because the wwwroot folder for the Azure function after build/release is empty. The main question is why)

我正在尝试使用Azure容器注册表(Linux)发布Azure函数,这一步我认为我很成功.我确实创建了CI/CD管道,并且一切都正确发布了.

I am trying to publish Azure Function using Azure Container Registry (Linux) This step I think I am successful. I did create CI/CD pipeline and everything is being published correctly.

当我进入我的azure函数主页时,例如: https://myAzureFuncion.azurewebsites.net/ (仅是示例)

When i enter my azure function main page like: https://myAzureFuncion.azurewebsites.net/ (it is only example)

我确实看到了:

在函数开始时.没什么花哨的,因为我只是想测试CI/CD,所以它是AF模板.我只是将Auhtorization更改为Anonymous(以排除身份验证问题):

At the begining the function itself. It is nothing fancy, as i just wanted to test the CI/CD so it is the AF template. I just did changed the Auhtorization to Anonymous (to exlude issue with the authentication):

        [FunctionName("MyAzureFunctionName")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request. Runned!");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }

所以我很好-对吗?不,因为它不起作用.我正在尝试通过HTTP请求来实现此功能:

So i good to go - right? No, as it is not working. I am trying to trigget this function by HTTP request:

https://myAzureFuncion.azurewebsites.net/api/MyAzureFunctionName?name=Mark

并且我不断收到404 Not Found.我确实用Postman进行了检查:相同(以前我曾经使用过函数授权,尝试过使用?code =(code_in_host)和标头x-functions-key.一直都显示相同的信息->找不到404)

And I keep getting 404 Not Found. I did check it with Postman: The same (previously with function authorization i did try with ?code=(code_in_host) and with headers x-functions-key. All the time the same info --> 404 Not Found)

因为此功能不存在.

当然,当我在本地运行(使用Windows的docker)时,一切正常.我正在跑步:

Of course, when I run it locally (using docker for windows) everything is working correctly. I am running:

https://localhost:some_port/api/MyAzureFunctionName?name = Mark

我正在收到正确的答案.

And I am receiving proper answer.

您能在这里指出要检查什么以及如何诊断问题吗?我已经输入了App Insights实时指标->,但是没有记录任何请求.

Can you point me what to check and how to diagnose the issue here? I've entered the App Insights Live Metrics --> but no request is being noted.

如何在这里诊断问题?

[UPDATE 04.05.2020 01:35]

这是我在YAML中该组件的发布管道的样子

Here is how my release pipeline looks like for this component in YAML

steps:
- task: AzureFunctionAppContainer@1
  displayName: 'Azure Function App on Container Deploy: MyFunctionName'
  inputs:
    azureSubscription: MySubscription
    appName: MyFunctionName
    imageName: 'myAcrContainer.azurecr.io/mobile/MyFunctionName:$(Build.BuildNumber)'

我在Azure门户中也看到了针对此Azure功能的一件奇怪的事情.当我转到功能菜单时-有信息,没有信息:

I saw also one weird thing in Azure Portal for this Azure function. When i go to functions menu - there is information, that there is none:

当我使用新的Azure Function管理经验"时,也会出现同样的情况

The same shows up, when i use the "new Azure Function management experience"

[UPDATE 04.05.2020 11:40]

提供有关此案的更多信息.这是docker映像的样子:

Putting more info on the case. Here is how docker image looks like:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/azure-functions/dotnet:3.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
WORKDIR /src
COPY ["BuildChat/BuildChat.csproj", "BuildChat/"]
COPY ["MyFunctionName/MyFunctionName.csproj", "MyFunctionName/"]
RUN dotnet restore "MyFunctionName/MyFunctionName.csproj"
COPY . .
WORKDIR "/src/MyFunctionName"
RUN dotnet build "MyFunctionName.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyFunctionName.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV AzureWebJobsScriptRoot=/app

这是我正在使用的构建管道:

And here is the build pipeline that i am using:

- stage: BuildMyAzureFunction
  displayName: Build and push MyAzureFunction stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push image to container registry
      inputs:
        containerRegistry: $(dockerRegistryServiceConnection)
        repository:  $(imageRepositoryMyAzureFunction)
        command: 'buildAndPush'
        Dockerfile: $(dockerfilePathAzureMyAzureFunction)
        tags: |
          $(tag)

我不知道我还能给你什么:)

I do not know what more i can give you :)

也许是Azure Portal中的容器设置:

Maybe container settings in Azure Portal:

以及来自最新docker版本的日志:

And logs from latest docker release:

2020-05-04 09:32:32.693 INFO  - Recycling container because of AppSettingsChange and isMainSite = True
2020-05-04 09:32:32.777 INFO  - Pulling image: myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11
2020-05-04 09:32:33.829 INFO  - 20200503.11 Pulling from mobile/myAzureFunction
2020-05-04 09:32:33.832 INFO  -  Digest: sha256:688090984dbc5d257b7d4eefff886affa451c59407edd46792dfc81726f393ec
2020-05-04 09:32:33.832 INFO  -  Status: Image is up to date for myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11
2020-05-04 09:32:33.835 INFO  - Pull Image successful, Time taken: 0 Minutes and 1 Seconds
2020-05-04 09:32:33.959 INFO  - Starting container for site
2020-05-04 09:32:33.961 INFO  - docker run -d -p 7287:80 --name myAzureFunction_1_84dd4d10 -e WEBSITE_CORS_ALLOWED_ORIGINS=https://functions.azure.com,https://functions-staging.azure.com,https://functions-next.azure.com -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=myAzureFunction -e WEBSITE_AUTH_ENABLED=False -e PORT=80 -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=myAzureFunction.azurewebsites.net -e WEBSITE_INSTANCE_ID=8da8a02a13a3cdde53ad6aafcd4eb717ca00dd6bc65ff07378d74a0cd859e1c0 -e HTTP_LOGGING_ENABLED=1 myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11  

2020-05-04 09:32:39.421 INFO  - Starting container for site
2020-05-04 09:32:39.422 INFO  - docker run -d -p 1500:8081 --name myAzureFunction_1_84dd4d10_middleware -e WEBSITE_CORS_ALLOWED_ORIGINS=https://functions.azure.com,https://functions-staging.azure.com,https://functions-next.azure.com -e WEBSITE_CORS_SUPPORT_CREDENTIALS=False -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=false -e WEBSITE_SITE_NAME=myAzureFunction-e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=myAzureFunction.azurewebsites.net -e WEBSITE_INSTANCE_ID=8da8a02a13a3cdde53ad6aafcd4eb717ca00dd6bc65ff07378d74a0cd859e1c0 -e HTTP_LOGGING_ENABLED=1 appsvc/middleware:2001061754 /Host.ListenUrl=http://0.0.0.0:8081 /Host.DestinationHostUrl=http://172.16.2.6:80 /Host.UseFileLogging=true 

2020-05-04 09:32:44.658 INFO  - Initiating warmup request to container myAzureFunction_1_84dd4d10 for site myAzureFunction
2020-05-04 09:33:00.400 INFO  - Waiting for response to warmup request for container myAzureFunction_1_84dd4d10. Elapsed time = 15.7420671 sec
2020-05-04 09:33:16.088 INFO  - Waiting for response to warmup request for container myAzureFunction_1_84dd4d10. Elapsed time = 31.4308809 sec
2020-05-04 09:33:33.284 INFO  - Waiting for response to warmup request for container myAzureFunction_1_84dd4d10. Elapsed time = 48.6269148 sec
2020-05-04 09:33:56.001 INFO  - Container myAzureFunction_1_84dd4d10 for site myAzureFunctioninitialized successfully and is ready to serve requests.
2020-05-04 09:33:56.004 INFO  - Initiating warmup request to container myAzureFunction_1_84dd4d10_middleware for site myAzureFunction
2020-05-04 09:34:04.506 INFO  - Container myAzureFunction_1_84dd4d10_middleware for site myAzureFunction initialized successfully and is ready to serve requests.
2020_05_04_RD501AC582A899_easyauth_docker.log:
2020_05_04_RD501AC582A899_default_docker.log:
2020-05-04T09:21:14.187664147Z Hosting environment: Production
2020-05-04T09:21:14.187744046Z Content root path: /app
2020-05-04T09:21:14.187750446Z Now listening on: http://[::]:80
2020-05-04T09:21:14.187754446Z Application started. Press Ctrl+C to shut down.

2020-05-04T09:33:49.229983024Z Hosting environment: Production
2020-05-04T09:33:49.232401696Z Content root path: /app
2020-05-04T09:33:49.232413096Z Now listening on: http://[::]:80
2020-05-04T09:33:49.232417396Z Application started. Press Ctrl+C to shut down.

[UPDATE 04.05.2020 15:55]

我已经检查了部署中心,它看起来还可以:

I've checked the deployment center, and its look like it is ok:

但是因为我正在发布3个thigns(1个SignalR集线器和2个azure函数-当然都不起作用"),所以我在想-也许它以某种方式变得混杂了(并且在这个azure函数应用程序上-实际上SignalR正在发布了吗?

But because i am publishing 3 thigns (1 SignalR hub and 2 azure functions - of course both "not working") i am thinking - maybe it is getting somehow mixed (and on this azure function app - in fact SignalR is being published?

我确实检查了此发布任务日志(假定已发布MyAzureFunction),并且一切正常(日志中到处都是MyAzureFunction):

I did checked this release task log (which suppose to publish MyAzureFunction) and everything looks correctly (everywhere is my MyAzureFunction in the log):

这是我的Release管道的样子:

Here is how my Release pipeline looks like:

这是连续部署触发器配置:

And here is Continous deployment trigger configuration:

这是第2阶段(用于发布Azure功能)的预部署设置:

And here is the stage 2 (for publishing azure function) predeployment settings:

最后,发布步骤本身(尽管我已经发布了该版本的YAML版本.要磨练-一切都检查完了.图片/应用名称中没有错配)

And finally, the release step itself (although I did already posted the YAML version of it. To be hones - everything checks out. There is no missmatch in image/app name)

[UPDATE 04.05.2020 16:15]

所以现在100%我确实知道发生了什么.成功发布后没有任何错误/警告,azure函数只是空的:)

So right now for 100% i do know what is going on. After successful release without any errors/warnings, the azure function is simply empty :)

我已经使用高级工具检查了wwwroot的内容(转到 https://myAzureFunction .scm.azurewebsites.net/wwwroot/)

I've checked with advanced tools the content of wwwroot (after going to https://myAzureFunction.scm.azurewebsites.net/wwwroot/)

这是结果:

所以为什么它不起作用很容易理解:)主要问题是到底发生了什么事" ?:)

So it is quite understandable why it is not working :) The main question is "What the hell happened"?:)

我确实在部署中心尝试同步代码(如@djsly所建议的),但是它不起作用(可能是因为它是由容器管理的),但是我在尝试同步时添加了一条消息:

I did try in deployment center to Sync the code (as @djsly suggested) but it is not working (probably because it is being managed by containers) But i am adding a message when I try to sync:

[UPDATE 05.05.2020 00:18]

我确实连接到Azure容器注册表并下载了该映像,该映像应该是在Azure中运行此Azure函数的映像.

I did connect to Azure container registry and downloaded the image, that suppose to be the image running behing this Azure function in Azure.

所以我确实跑了:

docker login myAcrContainer.azurecr.io

docker pull myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11

docker run -it --rm -p 31234:80 myAcrContainer.azurecr.io/mobile/myAzureFunction:20200503.11

,我确实点击了 http://localhost:31234/api/MyAzureFuncionFuns

,一切正常.因此,它确认了我的怀疑,即在Azure门户的Azure功能上未正确运行"映像.

and everything works correctly. So it confirm my suspisions, that the image is not being properly "runned" on Azure function on Azure portal.

问题是:我做错什么了吗?还是Azure漏洞?

The question is: Am i doing something wrong - or is it an Azure bug?

关于我的应用服务计划存在一个问题.如下所示(在创建Azure函数期间):

There was a question regarding my App Service Plan. Here how it looks like (during Azure function creation):

这是计划的详细信息:

现在这是一个免费试用版(正如我在创建时所建议的那样),这可能是原因吗?他们不应该禁止某些事情,而不是允许然后破坏这些事情吗?;)

It is a free trial right now (as they proposed it while I was creating it) Could that be the reason? Shouldn't they disallow something instead of allowing and then breaking things ?;)

推荐答案

针对有类似问题的人

我或多或少地获得了该问题的根本原因"信息. MS支持人员与我联系并指出,这是docker文件的问题.

I got more or less information "what is the root cause" of the problem. MS Support guy contacted me and pointed me out, that this is an issue with the docker file.

他指出了两件事:我正在将AzureWebJobsScriptRoot更改为/app.他声称这可能是它在本地计算机上而不是在AzureWebJobsScriptRoot/home/site/wwwroot

He pointed out two things: I am changing AzureWebJobsScriptRoot to /app. He claimed that this could be the reason why it is working on the local machine and not on Azure Portal where AzureWebJobsScriptRoot is /home/site/wwwroot

关于此声明的两个有趣事实:

Two fun facts about this statement:

1)创建此dockerfile的不是我(我是菜鸟),这是MS(插件)的功能,它允许您右键单击项目并选择Add-> Docker support

1) it is not I, who is creating this dockerfile (i am far to rookie) It was the functionality of MS (plugin) which allows you to right-click the project and choose Add->Docker support

这是此功能: https://docs.microsoft.com/pl-pl/visualstudio/containers/overview?view=vs-2019

2)到底是谁在工作环境中造成这种差异,以至于本地正在docker上工作的东西将无法在Azure Portal上的docker上工作-这很疯狂:)

2) Who on earth would create such discrepancies in working environments, that something is working on docker locally will not work on docker on Azure Portal - this is crazy :)

他建议我继续阅读本教程:

He advised me to go with this tutorial: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-linux-custom-image?tabs=bash%2Cportal&pivots=programming-language-csharp

并使用此命令:

func init LocalFunctionsProject --worker-runtime dotnet --docker

它将创建(或多或少)这样的docker文件

And it will create such docker file (more or less)

FROM microsoft/dotnet:2.2-sdk AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

# To enable ssh & remote debugging on app service change the base image to the one below
# FROM mcr.microsoft.com/azure-functions/dotnet:2.0-appservice 
FROM mcr.microsoft.com/azure-functions/dotnet:2.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

这里最重要的是-它已针对Azure Function 2解决,并且我正在使用Azure Function3.但是,我确实更改了图像版本以适合版本3,并且确实进行了扩展以涵盖以下事实:我必须建立引用的库.但它仍然无法正常工作:)(意味着,它在本地可运行,但在Azure门户上不可用)

The most important thing here is - that it is addressed for Azure Function 2, and I am using Azure Function 3. Nevertheless, I did change images version to fit version 3, and I did extend it to cover the fact, that I have to build the referenced library. But it is still not working :) (meaning, it works locally but doesn't work on Azure Portal)

我将仅在docker问题上创建新的SOF.但是我留下这个评论-也许会对某人有所帮助.

I will create new SOF simply on the matter of docker. But I leave this comment - maybe it will help someone.

[UPDATE 13.05.2020]

对于任何寻找问题根源的人.如果您要在Azure函数上查找404问题-请在此处查看:

For anyone looking for the root cause of the issue. If you will be looking for the problem with 404 on Azure function - please have a look here: Docker issue with Azure Function on Linux container - function missing leading to 404 on function run

这篇关于在Linux容器上触发Azure函数HTTP-函数无法正常工作404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆