在预定时间段内启动 Azure 容器实例中的容器

Start a container in Azure Container Instances for a predetermined period of time

我正在尝试在 Azure 中实现以下目标:允许用户以编程方式启动 docker 容器一段预定的时间,之后容器将自动停止。目的是避免仅在短时间内偶尔使用的闲置容器的成本。 到目前为止,我的想法是使用 Azure 容器实例和两个 Azure 函数:

我遇到的问题是,在本地测试时,stopContainer 函数在 Start-ThreadJob 中调用时似乎永远不会被调用,这应该允许调用是非阻塞的。我使用 Powershell 作为函数的语言,因为我想使用 New-AzContainerGroup cmndlet 按照 this tutorial.

启动容器

运行容器的最小示例 运行。ps1

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "This is the calling function."

# Interact with query parameters or the body of the request.
$UpTime = $Request.Query.UpTime

### Insert code to start the container here ###
# Asynchronously call function that will stop the container
Start-ThreadJob {Invoke-WebRequest -UseBasicParsing -Uri "http://localhost:7071/api/stopContainer?UpTime=$UpTime"}
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = "Called the waiting function with UpTime=$UpTime"
})

stopContainer 的最小示例 运行。ps1

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "This is the waiting function"

# Interact with query parameters or the body of the request.
$UpTime = $Request.Query.UpTime

Start-Sleep -Seconds $UpTime
### Insert code to stop the container here ###
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = "Waited for $UpTime seconds, stopping container."
})

在 Windows 本地启动功能时,我得到以下日志。

Azure Functions Core Tools
Core Tools Version:       3.0.3160 Commit hash: 00aa7f43cc5c5f15241b5e6e5363256f19ceb990
Function Runtime Version: 3.0.14916.0


Functions:

        startContainer: [GET] http://localhost:7071/api/startContainer

        stopContainer: [GET] http://localhost:7071/api/stopContainer

For detailed output, run func with --verbose flag.
[2021-01-14T12:50:27.025Z] Worker process started and initialized.
[2021-01-14T12:50:31.698Z] Executing 'Functions.startContainer' (Reason='This function was programmatically called via the host APIs.', Id=ee48845e-274b-4532-80ae-85372e4474fc)
[2021-01-14T12:50:32.203Z] INFORMATION: This is the calling function.
[2021-01-14T12:50:32.362Z] OUTPUT:
[2021-01-14T12:50:32.375Z] OUTPUT: Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
[2021-01-14T12:50:32.379Z] OUTPUT: --     ----            -------------   -----         -----------     --------             -------
[2021-01-14T12:50:32.382Z] OUTPUT: 1      Job1            ThreadJob       Running       False           PowerShell           Invoke-WebRequest -UseBa.
[2021-01-14T12:50:32.388Z] OUTPUT:
[2021-01-14T12:50:32.498Z] Executed 'Functions.startContainer' (Succeeded, Id=ee48845e-274b-4532-80ae-85372e4474fc, Duration=810ms)

显然从未调用过 stopContainer 函数。关于如何进行的任何想法?对停止容器的函数的异步调用在这里甚至是一个好的解决方案吗?

您的问题可能出在这里:sleeps for UpTime hours

一个 Azure 函数 - 至少在消费计划中 - 只能 运行 大约 5 分钟。在这里睡觉算作“运行ning”。您应该切换到 Durable Function (in preview currently for PowerShell). Create a Durable Timer 之类的东西,它会为您解决问题。

正如 Anatoli 在评论中添加的那样(感谢!)这里是 PS 中的耐用计时器示例:https://github.com/Azure/azure-functions-powershell-worker/blob/dev/examples/durable/DurableApp/MonitorOrchestrator/run.ps1