简体   繁体   English

根据输入从 Azure 函数启动和停止 Docker 容器

[英]Start and stop Docker container from Azure function based on input

I was wondering if there is some way that one can achieve the following on the Azure cloud platform:我想知道是否有某种方法可以在 Azure 云平台上实现以下目标:

  1. Start a Docker container (Azure Container Instances) from an Azure function with C# code based on input parameters and file posted to the Azure function.根据输入参数和发布到 Azure 函数的文件,使用 C# 代码从 Azure 函数启动 Docker 容器(Azure 容器实例)。

  2. After the container has been started, a software inside the container has to be started to do some work based on the input file and parameters that was posted to the Azure function.容器启动后,必须启动容器内的软件,以根据发布到 Azure 函数的输入文件和参数执行一些工作。

  3. After the software inside the Docker container is done, and written the output to some database (MS SQL Server also in Azure), the Azure function must stop the container (could also be a different Azure function that polls, for example once every minute, to check if no work is being done by any Docker image).在 Docker 容器内的软件完成并将输出写入某个数据库(Azure 中也有 MS SQL Server)后,Azure 函数必须停止容器(也可以是轮询不同的 Azure 函数,例如每分钟一次,检查是否有任何 Docker 映像正在执行任何工作)。

  4. If there are requests coming to the Azure function while a Docker container currently is up and running, the functions spins a new Docker container up from the same image and does the same ting.如果在 Docker 容器当前启动并运行时有请求进入 Azure 函数,这些函数会从同一映像启动一个新的 Docker 容器并执行相同的操作。

The reason for the need to start and stopping the Docker container is to keep the cost down because of the demanding work load that needs an instance that uses multiple CPU cores and a lot of RAM.需要启动和停止 Docker 容器的原因是为了降低成本,因为需要使用多个 CPU 内核和大量 RAM 的实例的苛刻工作负载。

In your Function, you can start and stop containers in ACI using the Microsoft.Azure.Management.ContainerInstance Namespace in C#.在您的函数中,您可以使用 C# 中的 Microsoft.Azure.Management.ContainerInstance 命名空间在 ACI 中启动和停止容器。 Here's a link to the Nuget package and a sample I found:这是Nuget 包链接和我找到的示例:

var containerGroup = azure.ContainerGroups.Define(containerGroupName)
    .WithRegion(azureRegion)
    .WithExistingResourceGroup(resourceGroupName)
    .WithLinux()
    .WithPublicImageRegistryOnly()
    .WithoutVolume()
    .DefineContainerInstance(containerGroupName)
         .WithImage(containerImage)
         .WithExternalTcpPort(80)
         .WithCpuCoreCount(1.0)
         .WithMemorySizeInGB(1)
         .Attach()
    .WithDnsPrefix(containerGroupName)
    .Create();

There are many ways to achieve your goal.有很多方法可以实现您的目标。 Instead of using Functions, you can use Logic App to start and stop containers in ACI using the ACI connector .您可以使用逻辑应用程序来启动和停止使用ACI 连接器ACI 中的容器,而不是使用函数。 You could think of a workflow similar to this:您可以想到与此类似的工作流程:

  • Add an action that will trigger the Logic App like a new message in a queue.添加将触发逻辑应用程序的操作,就像队列中的新消息一样。
  • Create an ACI container group using the connector.使用连接器创建 ACI 容器组。
  • Launch a container in ACI using the connector.使用连接器在 ACI 中启动容器。
  • You watch the container state to see if it was created successfully.您观察容器状态以查看它是否已成功创建。
  • You can pass values to the container by setting env variables using the ACI connector like a file name or a job number.您可以通过使用 ACI 连接器设置 env 变量(如文件名或作业编号)来将值传递给容器。
  • You add a loop that watch for completion of your code in the container (output that you send in the log)您添加一个循环来监视容器中代码的完成情况(您在日志中发送的输出)
  • You delete the ACI using the connector您使用连接器删除 ACI

You can of course run multiple Logic Apps in parallel.您当然可以并行运行多个逻辑应用程序。

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

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