【问题标题】:I want to create and Azure Container Instance (ACI) from azure functions app in python我想从 python 中的 azure 函数应用程序创建和 Azure 容器实例 (ACI)
【发布时间】:2022-01-08 08:34:00
【问题描述】:

我正在尝试在 Azure 上复制我在 AWS 上的部署。我在 aws 上有一个完全基于无服务器的架构,它由带有 s3 触发器的 aws lambda、aws sagemaker 处理作业和 aws ecr 组成。只有存在 FILE AFILE B 时,lambda 才会触发 sagemaker 处理作业(使用 python BOTO3 库)。 从某种意义上说,我在我的 lambda 函数中提到了一个 if 条件,它表示只有在这两个文件存在时才会触发作业。当我上传这两个文件时会触发 lambda。 aws 对此有非常广泛的文档,它有一个我可以提到的特定路径,然后将这些文件复制到 sagemaker 处理作业容器 (opt/ml/processing) 中。它还有一个参数来指定算法或 docker 图像,它可以指示作业如何处理这两个文件。 现在我想在天蓝色上实现同样的目标,但在这里很难找到类似的东西。 然而,我确实发现了一些非常相似的东西,那就是我可以在具有 blob 触发器的 azure 函数中提供的 azure 容器实例。 现在我遇到的困难是如何将这些 FILE AFILE B 推入容器中? 同样,一旦容器完成了它应该做的任何事情,它就会生成一堆需要存储在某个地方的输出文件。如果可能,我需要将它们存储在 blob 存储中。

我想要的是在触发函数应用程序时触发创建天蓝色容器实例。当上传文件 A 和文件 B 时,将触发函数应用程序(blob 触发器)。然后这些文件应该进入容器。我找到了一个脚本:

def create_container_group(aci_client, resource_group,
                       container_group_name, container_image_name):
"""Creates a container group with a single container.

Arguments:
    aci_client {azure.mgmt.containerinstance.ContainerInstanceManagementClient}
                -- An authenticated container instance management client.
    resource_group {azure.mgmt.resource.resources.models.ResourceGroup}
                -- The resource group in which to create the container group.
    container_group_name {str}
                -- The name of the container group to create.
    container_image_name {str}
                -- The container image name and tag, for example:
                   microsoft\aci-helloworld:latest
"""
print("Creating container group '{0}'...".format(container_group_name))

# Configure the container
container_resource_requests = ResourceRequests(memory_in_gb=1, cpu=1.0)
container_resource_requirements = ResourceRequirements(
    requests=container_resource_requests)
container = Container(name=container_group_name,
                      image=container_image_name,
                      resources=container_resource_requirements,
                      ports=[ContainerPort(port=80)])

# Configure the container group
ports = [Port(protocol=ContainerGroupNetworkProtocol.tcp, port=80)]
group_ip_address = IpAddress(ports=ports,
                             dns_name_label=container_group_name,
                             type="Public")
group = ContainerGroup(location=resource_group.location,
                       containers=[container],
                       os_type=OperatingSystemTypes.linux,
                       ip_address=group_ip_address)

但我不知道如何在容器内传输这些文件。

【问题讨论】:

    标签: python azure docker azure-functions azure-container-instances


    【解决方案1】:

    对于 Azure 容器实例,您必须将映像上传到 Azure 容器注册表:https://azure.microsoft.com/en-gb/services/container-registry/#features

    然后,当 Azure 容器初始化时,它会尝试拉下该注册表中的任何内容。

    默认情况下,容器实例上没有操作系统。

    我使用 Azure CLI 创建容器实例并使用 docker 构建图像。

    这种中等艺术作品有一些步骤,但当我这样做时,它需要一些游戏:https://medium.com/kocsistem/creating-azure-container-registry-using-the-azure-cli-769138d9d804

    如果我是你,我会使用以下 docker 镜像:

    FROM mcr.microsoft.com/windows/servercore:ltsc2022

    这是它的 github 图像文件:https://github.com/docker-library/python/blob/3d43bcf8ddd26ae85fd6a63a7e1d502b445c9cce/3.9/windows/windowsservercore-ltsc2022/Dockerfile

    更新:

    我打算分享我的整个 docker 文件,但是当我查看它时,我发现它上面有一些公司设置。所以只解释我做了什么。

    在我的图像设置中,我在该代码中有一个名为 finalrunco​​mmands.bat 的文件,它可以调用很多东西,但其中一个是我制作的 vsts powershell 脚本,它是一个 .ps1 文件。 (类似于您在此处尝试实现的目标)

    这是我的文件夹结构,可能对你更有意义。

    这是我最后调用的最终运行命令 .bat 文件的图片。

    然后在我的 docker 文件的最后,我有一个入口点,允许图像等待命令。在您的情况下,您的图像正在等待文件或命令以获取该文件,或者您通过 cmets 调用脚本,我认为这就是您正在做的事情。

    # Define the entry point for the docker container.
    # This entry point starts the developer command prompt and launches the PowerShell shell.
    ENTRYPOINT "C:\azp\Finalruncommands.bat"

    所以在你的情况下,你做一个入口点,它指向你的脚本。

    更新:

    我重新阅读了我的答案,除非我分享我的代码,否则它并没有完全点击,所以这里没有个人设置。

    # escape=`
    
    FROM mcr.microsoft.com/windows/servercore:ltsc2019
    
    
    ENV chocolateyUseWindowsCompression=false
    
    SHELL ["powershell", "-Command"]
    
    
    RUN Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    
    RUN choco config set cachelocation C:\chococache
    
    RUN choco feature enable --name allowGlobalConfirmation
    
    #Install Required Software
    RUN choco install git --confirm --timeout 216000; 
    RUN choco install nodejs-lts --version=14.16.1 --confirm --timeout 216000;
    RUN choco install openjdk --confirm --timeout 216000;
    RUN choco install curl --confirm --timeout 216000;
    RUN choco install terraform --confirm --timeout 216000;
    RUN npm install -g npm@6.14.12
    
    #Configure npm
    WORKDIR /usr/src/app
    COPY package*.json ./
    COPY . .
    COPY npm.cmd C:\Users\ContainerAdministrator\AppData\Roaming\npm\
    
    # Install .Net Core SDK Used for Building Web Apps
    #RUN choco install dotnet-sdk --confirm --limit-output --timeout 216000;
    RUN choco install dotnet-5.0-sdk --version=5.0.301 --confirm --limit-output --timeout 216000;
    
    # Restore the default Windows shell for correct batch processing.
    SHELL ["cmd", "/S", "/C"]
    
    # Destroy Choclotatey cache
    RUN rmdir /S /Q C:\chococache
    
    # Set Path Variable for dotnet
    
    RUN setx /M PATH "%PATH%;%ProgramFiles%\dotnet"
    
    # Trigger the population of the local package cache
    ENV NUGET_XMLDOC_MODE skip
    #USER ContainerAdministrator
    #RUN mkdir warmup 
    WORKDIR /warmup
    RUN dotnet new
    WORKDIR /azp
    RUN rmdir /S /Q C:\warmup 
    
    #Expose Ports for Azure Container Instance
    
    EXPOSE 80/udp 
    EXPOSE 80/tcp 
    EXPOSE 5000/udp
    EXPOSE 5000/tcp
    EXPOSE 8080/udp 
    EXPOSE 8080/tcp
    
    #Install DevOps Agents
    COPY start.ps1 .
    
    #Configure DevOps Agent and prep Image for run.
    ADD Finalruncommands.bat C:\azp\
    
    # Define the entry point for the docker container.
    # This entry point starts the developer command prompt and launches the PowerShell shell.
    ENTRYPOINT "C:\azp\Finalruncommands.bat"

    【讨论】:

    • 感谢您的回复,但如前所述,我需要从函数应用程序配置容器。函数应用程序有一个 blob 触发器,当我上传 FILE A 和 FILE B 时触发。我确实将我的图像放在带有基本图像 ubuntu 的 ACR 中,其中有一个算法,说明如何处理文件。这些文件必须以某种方式进入 azure 容器实例,这就是我遇到的问题。
    • 您的图像是否需要命令?
    • 图像不期待命令,但基本上复制到图像中的“script.py”期待文件完成它的工作。我无法在构建图像时复制文件,因为这些文件不断变化。因此,我有事件驱动的架构。
    • 所以你必须让图像期望命令能够正常工作。我在我的一张图片上有相同的设置。我现在出去了,但是当我回到家时,我会用我的 docker 文件代码更新这个答案,并向你展示我为我运行的脚本做了什么。不用担心,我们会解决这个问题。
    • 非常感谢杰森。期待您的回答。
    猜你喜欢
    • 2020-11-13
    • 2020-11-08
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多