Azure容器实例-.net SDK-更改环境变量并重新启动任务容器 [英] Azure Container Instances - .net sdk - change environment variable and restart task container

查看:62
本文介绍了Azure容器实例-.net SDK-更改环境变量并重新启动任务容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经部署了一个仅包含一个容器的容器组,并且重新启动策略为从不".我想再次运行此容器,但使用不同的环境变量.我在powershell和azure cmdlet中看到了实现此目标的方法,但是Azure .net SDK尚不清楚如何实现此目标.

I have deployed a container group with just one container in it, with the restart policy as Never. I want to run this container again, but with different environment variables. I see ways to do this in powershell and azure cmdlets, but the Azure .net SDK is not very clear on how this can be achieved.

有人对使用Fluent API如何实现这一目标有任何指示吗?这对我很有用,因为不需要再次拉映像,只需重新启动容器实例即可.

Does anyone have any pointers on how this can be achieved with the Fluent API? This will be useful for me, since the image need not be pulled again, just the container instance restarted.

推荐答案

我不确定您是否能够防止在容器启动时再次拉出图像.我经常看到ACI在重新启动时再次拉入映像,无论之前是否拉过相同的映像.我想这就是无服务器的真正含义;)

I'm not sure you will be able to prevent the image from being pulled again on container start. I often see ACIs pull in the image again on restart, regardless of whether the same exact image was pulled before. I guess that's what serverless means in the real ;)

根据您的问题,尚不清楚您要实现的目标以及使用环境vars重新启动容器是否是实现此目的的最佳方法.

From your question it's not entirely clear what you are trying to achieve and whether restarting the container with environment vars is the best way to do this.

因此,在不知道具体细节的情况下,有一些想法:

So without knowing the specifics, here's a couple of ideas:

如果您偶尔运行容器,并且只想给它一些说明,则可以考虑从参数化的ARM模板重新部署容器.您绝对可以通过ARM模板传入环境变量.

If you're running the container occasionally and just want to give it some instructions, you could consider redeploying the container from a parameterized ARM template. You can definitely pass in environment variables through the ARM template.

您可以将文件共享从Azure存储帐户装载到容器中,然后运行从那里加载变量的脚本.您已将要处理的数据上传到文件共享并启动容器.

You could mount a file share from an Azure Storage account into the container and run a script that loads the vars from there. You'd upload the data to process to the file share and start the container.

例如,出于测试目的,我已经使用它在通用容器中运行任意(.NET)可执行文件.

I've used this to run arbitrary (.NET) executables in a generic container, for example for testing purposes.

对于更频繁的运行,您可以将配置数据粘贴到数据库,队列或Blob中,然后从容器中进行处理.Blob和队列可以轻松地绑定到Azure函数,因此您可以使用一个函数来在要处理的工作时自动启动容器.

For more frequent runs, you could stick the configuration data in a database, a queue or a blob and process that from the container. Blobs and queues can easily be tied to Azure Functions so you could have a function that starts your container automatically when there's work to be processed.

从您的问题出发,尽管我有点猜测您正在寻找一些.NET代码来实现这一目标.管理SDK中似乎对此提供了一些支持,但是到目前为止,我一直无法使它起作用:

From you're question though I'm sort of guessing you're looking for some .NET code to pull this off. There seems to be some support for that in the management SDK but I've been unable to get that to work so far:

using System.Threading.Tasks;
using Microsoft.Azure.Management.ContainerInstance.Fluent;
using Microsoft.Azure.Management.ContainerInstance.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;

// ...

public static async Task SetEnvAndStart(string varName, string customValue)
{
    // connect to Azure
    var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(
        Configuration.ClientId(), 
        Configuration.ClientSecret(), 
        Configuration.TenantId(), 
        AzureEnvironment.AzureGlobalCloud);            

    var azure = await Azure.Authenticate(credentials).WithDefaultSubscriptionAsync();
    // Find your container group
    var containerGroup = await azure.ContainerGroups.GetByIdAsync("<resource id>");
    // Add the environment variable
    containerGroup.Containers["mycontainer"].EnvironmentVariables.Add(new EnvironmentVariable(varName, customValue));
    // Apply the changes
    await containerGroup.Update().ApplyAsync();
    // Restart the container
    await ContainerGroupsOperationsExtensions.StartAsync(containerGroup.Manager.Inner.ContainerGroups, containerGroup.ResourceGroupName, containerGroup.Name);
}

如果您真的想使用环境变量,我认为最好的选择是重新创建容器.您可能会发现一些示例代码对于Azure管理SDK很有帮助.

If you really want to go with environment vars, I think your best bet is to re-create the container. You may find some of the example code for the Azure Management SDK helpful.

这篇关于Azure容器实例-.net SDK-更改环境变量并重新启动任务容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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