【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

本文涉及的产品
日志服务 SLS,月写入数据量 50GB 1个月
简介: 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络

问题描述

使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况。Storage Account启用虚拟网络中的服务终结点(Service Endpoint)后,可以实现只能从内网访问。同时,Azure Function也支持集成内网。并且可支持在虚拟网络中被Storage Account所触发。所以根据门户中一步一步的操作,配置完Azure Function和Storage Account的Virtul Network后,发现Function无法启动。在日志中发现问题是:

1

2

3

4

5

6

7

8

9

10

11

12

13

2021-06-30T10:56:00.895 [Error] An unhandled exception has occurred. Host is shutting down.

Microsoft.Azure.Storage.StorageException : This request is not authorized to perform this operation.

   at async Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteAsync[T](RESTCommand`1 cmd,IRetryPolicy policy,OperationContext operationContext,CancellationToken token)

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Extensions.Storage.TimeoutHandler.ExecuteWithTimeout[T](String operationName,String clientRequestId,IWebJobsExceptionHandler exceptionHandler,ILogger logger,CancellationToken cancellationToken,Func`1 operation) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Extensions.Storage\TimeoutHandler.cs : 56

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Host.Queues.Listeners.QueueListener.ExecuteAsync(CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Extensions.Storage\Queues\Listeners\QueueListener.cs : 201

   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

   at async Microsoft.Azure.WebJobs.Host.Timers.TaskSeriesTimer.RunAsync(CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Timers\TaskSeriesTimer.cs : 147

2021-06-30T10:56:00.921 [Information] Stopping JobHost

2021-06-30T10:56:00.921 [Information] Stopping the listener 'Microsoft.Azure.WebJobs.Host.Listeners.CompositeListener' for function 'BlobTrigger1'

2021-06-30T10:56:00.942 [Information] Stopped the listener 'Microsoft.Azure.WebJobs.Host.Listeners.CompositeListener' for function 'BlobTrigger1'

2021-06-30T10:56:00.943 [Information] Job host stopped

(日志可通过高级工具Kudu获取:Kudu地址为:https://<your function app name>.scm.chinacloudsites.cn/DebugConsole 日志路径为:D:\home\LogFiles\Application\Functions\Host\xxxxxxxx.log)

 

问题分析

根据日志发现了:

1) Function Host遇见了异常,导致它被关闭.

2)异常的原因是因为访问Storage没有权限 (Microsoft.Azure.Storage.StorageException : This request is not authorized to perform this operation.

那么问题根源就出现在Azure Function访问Stroage时,走的是公网访问,而公网的访问在Storage中式拒绝的。所以要修改Funciton的配置,让其通过内网的形式访问Storage。在查看官方说明文档后,有发现两个非常重要的配置:

  • WEBSITE_VNET_ROUTE_ALL:设置为1。它会将Azure Function的所有出站调用发送到 VNet。
  • WEBSITE_DNS_SERVER:默认值为168.63.129.16。用与虚拟网络集成时,它将使用与虚拟网络相同的 DNS 服务器。 函数应用需要此设置才能与 Azure DNS 专用区域配合使用。 此设置和 WEBSITE_VNET_ROUTE_ALL 会将应用中的所有出站调用发送到虚拟网络。

 

问题解决

在Azure Funciton的设置页面,添加WEBSITE_VNET_ROUTE_ALLWEBSITE_DNS_SERVER两个参数,配置如下:

 

修改完成后,再次查看Function Host的启动日志,就可见 “Loading functions metadata” 和 “1 functions loaded”等消息。这就表明,Function Host启动成功。

2021-06-30T10:57:00.006 [Information] Initializing Warmup Extension.
2021-06-30T10:57:00.069 [Information] Initializing Host. OperationId: '93cf4ed0-d598-4308-9241-dca5ba6a55ee'.
2021-06-30T10:57:00.073 [Information] Host initialization: ConsecutiveErrors=0, StartupCount=1, OperationId=93cf4ed0-d598-4308-9241-dca5ba6a55ee
2021-06-30T10:57:00.106 [Information] LoggerFilterOptions
{
  "MinLevel": "None",
  "Rules": [
    {
      "ProviderName": null,
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": "None",
      "Filter": null
    },
    {
      "ProviderName": "Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics.SystemLoggerProvider",
      "CategoryName": null,
      "LogLevel": null,
      "Filter": "<AddFilter>b__0"
    }
  ]
}
2021-06-30T10:57:00.107 [Information] FunctionResultAggregatorOptions
{
  "BatchSize": 1000,
  "FlushTimeout": "00:00:30",
  "IsEnabled": true
}
2021-06-30T10:57:00.107 [Information] SingletonOptions
{
  "LockPeriod": "00:00:15",
  "ListenerLockPeriod": "00:00:15",
  "LockAcquisitionTimeout": "10675199.02:48:05.4775807",
  "LockAcquisitionPollingInterval": "00:00:05",
  "ListenerLockRecoveryPollingInterval": "00:01:00"
}
2021-06-30T10:57:00.107 [Information] QueuesOptions
{
  "BatchSize": 16,
  "NewBatchThreshold": 8,
  "MaxPollingInterval": "00:00:02",
  "MaxDequeueCount": 5,
  "VisibilityTimeout": "00:00:00"
}
2021-06-30T10:57:00.107 [Information] BlobsOptions
{
  "CentralizedPoisonQueue": false
}
2021-06-30T10:57:00.108 [Information] Starting JobHost
2021-06-30T10:57:00.110 [Information] Starting Host (HostId=lbfunctionforvnet01, InstanceId=05efd41a-013d-428c-b839-fb4b69127366, Version=3.0.15733.0, ProcessId=5480, AppDomainId=1, InDebugMode=True, InDiagnosticMode=False, FunctionsExtensionVersion=~3)
2021-06-30T10:57:00.121 [Information] Loading functions metadata
2021-06-30T10:57:00.137 [Information] 1 functions loaded
2021-06-30T10:57:01.348 [Information] Generating 1 job function(s)
2021-06-30T10:57:01.373 [Information] Found the following functions:
Host.Functions.BlobTrigger1

最后在Storage Blob中上传文件进行测试,Function能成功被触发及获取到正确的Blob信息

 

 

参考资料

具有虚拟网络触发器的高级计划 https://docs.azure.cn/zh-cn/azure-functions/functions-networking-options#premium-plan-with-virtual-network-triggers

配置函数应用设置https://docs.azure.cn/zh-cn/azure-functions/functions-create-vnet#configure-your-function-app-settings

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
打赏
0
0
0
0
205
分享
相关文章
【Azure Function App】在Function App中使用System Managed Identity访问Storage Account
本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。
127 20
【Azure Function】分享把Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤
本文介绍了将Azure Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤。.NET 6.0作为长期支持版本,生命周期至2024年11月结束。为确保持续支持,建议升级至更新版本。升级步骤包括安装.NET 8 SDK、更新Azure Functions Core Tools、修改项目文件目标框架为net8.0、更新兼容的NuGet包、本地测试以及重新发布到Azure。更多详细信息可参考官方文档。
104 9
|
6月前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
159 64
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
147 73
【Azure K8S | AKS】在AKS的节点中抓取目标POD的网络包方法分享
在AKS中遇到复杂网络问题时,可通过以下步骤进入特定POD抓取网络包进行分析:1. 使用`kubectl get pods`确认Pod所在Node;2. 通过`kubectl node-shell`登录Node;3. 使用`crictl ps`找到Pod的Container ID;4. 获取PID并使用`nsenter`进入Pod的网络空间;5. 在`/var/tmp`目录下使用`tcpdump`抓包。完成后按Ctrl+C停止抓包。
144 12
为什么要在网络设置静态代理ip?
随着科技和互联网的发展,越来越多企业需要使用代理服务器。设置静态代理IP可提高安全性、保护用户IP地址,实现地域性访问、缓存加速及负载均衡等优势。具体配置方法包括在Windows、macOS操作系统或浏览器中进行网络设置,输入代理服务器的地址和端口。通过合理设置代理IP,用户能更好地管理网络流量,提升隐私与性能。
144 37
导入虚拟机到Hyper-V环境时,理解并配置网络适配器设置是确保网络通信的关键
在Hyper-V环境中,正确配置虚拟机的网络适配器是确保其网络通信的关键。需先启用Hyper-V功能并创建虚拟交换机。接着,在Hyper-V管理器中选择目标虚拟机,添加或配置网络适配器,选择合适的虚拟交换机(外部、内部或私有),并根据需求配置VLAN、MAC地址等选项。最后,启动虚拟机并验证网络连接,确保其能正常访问外部网络、与主机及其他虚拟机通信。常见问题包括无法访问外部网络或获取IP地址,需检查虚拟交换机和适配器设置。
【Azure APIM】APIM服务配置网络之后出现3443端口不通,Management Endpoint不健康状态
如果没有关联的网络安全组,则阻止所有网络流量通过子网和网络接口。
137 30
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
135 7

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问