如何使用.Net Fluent SDK设置Azure容器实例重新启动策略

时间:2017-12-20 13:03:36

标签: azure azure-sdk-.net azure-container-instances

如何使用.Net Fluent Management SDK创建Azure容器实例时配置重新启动策略?我错过了一些明显的东西,或者SDK是否落后于该功能?

                _azure.ContainerGroups.Define(containerGroupName)
                    .WithRegion(Region.USEast)
                    .WithExistingResourceGroup("my-resource-group")
                    .WithLinux()
                    .WithPrivateImageRegistry("myreg.azurecr.io", "registry", "XXXXXXXXXXXXX")
                    .WithoutVolume()
                    .DefineContainerInstance(containerGroupName)
                        .WithImage("my-image/tag")
                        .WithoutPorts()
                        .WithCpuCoreCount(4)
                        .WithMemorySizeInGB(0.5)
                        .WithEnvironmentVariable("containerGroup", containerGroupName)
                        .Attach()
                    .Create();

谢谢

2 个答案:

答案 0 :(得分:1)

  

如何使用.Net Fluent Management SDK创建Azure容器实例时配置重新启动策略?

根据Fluent SDK,我发现我们可以使用以下代码设置重启策略

containerGroup.Inner.RestartPolicy = ContainerRestartPolicy.Always;

演示代码:

var containerGroup = _azure.ContainerGroups.Define(containerGroupName)
                .WithRegion(Region.USEast)
                .WithExistingResourceGroup("my-resource-group")
                .WithLinux()
                .WithPrivateImageRegistry("myreg.azurecr.io", "registry", "XXXXXXXXXXXXX")
                .WithoutVolume()
                .DefineContainerInstance(containerGroupName)
                .WithImage("my-image/tag")
                .WithoutPorts()
                .WithCpuCoreCount(4)
                .WithMemorySizeInGB(0.5)
                .WithEnvironmentVariable("containerGroup", containerGroupName)
                .Attach()
                .Create();

 containerGroup.Inner.RestartPolicy = ContainerRestartPolicy.Always;

注意:我发现ContainerRestartPolicy只有一个默认值总是,我不会在我这边测试它。我不确定其他值 Never和OnFailure 是否正常工作

如果可以 ContainerInstanceManagementClient ,我们可以使用ContainerInstanceManagementClient创建Azure容器实例

ContainerInstanceManagementClient client = new ContainerInstanceManagementClient(new TokenCredentials(token));

  var containerGroup = new ContainerGroupInner
            {
                Location = "xx",
                Containers = new List<Container>(),
                ImageRegistryCredentials = new List<ImageRegistryCredential>(),
                RestartPolicy = "xxx"

            };

var groupInner = client.ContainerGroups.CreateOrUpdateAsync("resourceGroup", "containerGroupName", containerGroupInner).Result;

答案 1 :(得分:0)

根据documentation,在启动容器时设置重启策略。 在下面的示例中,我将策略设置为“从不”, .WithRestartPolicy(ContainerGroupRestartPolicy.Never)

_azure.ContainerGroups.Define(containerGroupName)
                    .WithRegion(Region.USEast)
                    .WithExistingResourceGroup("my-resource-group")
                    .WithLinux()
                    .WithPrivateImageRegistry("myreg.azurecr.io", "registry", "XXXXXXXXXXXXX")
                    .WithoutVolume()
                    .DefineContainerInstance(containerGroupName)
                        .WithImage("my-image/tag")
                        .WithoutPorts()
                        .WithCpuCoreCount(4)
                        .WithMemorySizeInGB(0.5)
                        .WithEnvironmentVariable("containerGroup", containerGroupName)
                .WithRestartPolicy(ContainerGroupRestartPolicy.Never)

                        .Attach()
                    .Create();

来自文档:

  

如何指定重新启动策略取决于您创建的方式   容器实例,例如Azure CLI,Azure PowerShell   cmdlet,或Azure门户中。在Azure CLI中,指定   调用az容器创建时的--restart-policy参数。