使用Azure容器注册表从C#创建新的Azure容器实例 [英] Using Azure Container Registry creating new Azure Container Instance from C#

查看:93
本文介绍了使用Azure容器注册表从C#创建新的Azure容器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了Azure容器注册表,并将自定义ACI上传到注册表-没问题-一切都按预期进行。我曾经尝试过使用Azure门户从映像创建容器实例,但是那里也没有问题-但是,当我想使用带有Microsoft Azure管理容器实例Fluent API的C#来自动化事情时,即使遇到问题,我仍然遇到问题感觉就像我遍地都是Internet和设置,正在寻找隐藏的障碍物,但是我找不到太多帮助。



我的代码如下:

  var azureCredentials = new AzureCredentials(新的
ServicePrincipalLoginInformation
{
ClientId = xxxxxxxx- xxxx-xxxx-xxxx-xxxxxxxxxxxx,
ClientSecret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}, xxxxxxxxxxxxxxxxxxxxxxxxxxxx,
AzureEnvironment.AzureGlobalCloud);

var azure = Azure
.Configure()
.Authenticate(azureCredentials)
.WithSubscription( xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxxxx);

IContainerGroup containerGroup = azure.ContainerGroups.Define( mytestgroup)
.WithRegion(Region.EuropeWest)
.WithExistingResourceGroup( mytest-rg)
。 WithLinux()
.WithPrivateImageRegistry( mytestreg.azurecr.io, mytestreg, xxxxxxxxxxxxxx)
.WithoutVolume()
.DefineContainerInstance( mytestgroup)
。 WithImage( mytestimage / latest)
.WithExternalTcpPort(5555)
.WithCpuCoreCount(.5)
.WithMemorySizeInGB(.5)
.Attach()
。创建();

上面的代码不断给我一个例外:



Microsoft.Rest.Azure.CloudException:无法访问容器组 mytestgroup中的图像 mytestimage / latest。请检查映像和注册表凭据。'



我尝试了几件事;


  1. 使用 docker登录测试凭据-没问题。

  2. 使用 docker pull mytestreg.azurecr.io/mytestimage -没问题。

  3. 交换 WithPrivateImageRegistry 使用 WithPublicImageRegistryOnly 并仅在 WithImage 中使用 debian -可以如预期的那样-没问题。

  4. 最新标签保留在图像名称之外-仍然无效。

我不知道为什么私人注册表的凭据不起作用-我直接从Azure门户复制/粘贴以避免拼写错误,



使用Fiddler检查流量不会显示任何有趣的内容,除了上面的异常消息是直接从Azure管理API返回的。 / p >

我最想念的东西是什么?

解决方案

上面的答案(即使用完整的Azure注册表服务器名称):

  .WithImage( mytestreg.azurecr.io/mytestimage:latest)

似乎是解决方案的一部分,但是即使进行了更改,我仍然看到此错误。浏览网络上的其他示例( https://github.com/Azure-Samples/aci-dotnet-create-container-groups-using-private-registry/blob/master/Program.cs )包含我需要的内容,我从以下方式更改了Azure身份验证:

  azure = Azure.Authenticate(authFilePath).WithDefaultSubscription(); 

至:

  AzureCredentials凭据= SdkContext.AzureCredentialsFactory.FromFile(authFilePath); 
azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();

随着更改,事情现在可以正常进行。


I have created a Azure Container Registry, and uploaded a custom ACI to the registry - no problem - everything works as intended. I have tried creating an container instance from the image using the Azure Portal, and no problems there either - however - when I want to automate things using C# with the Microsoft Azure Management Container Instance Fluent API, I run into problems, and even though I feel like I have been all over the Internet and the settings, looking for hidden obstructions, I haven't been able to find much help.

My code is as follows:

var azureCredentials = new AzureCredentials(new
ServicePrincipalLoginInformation
{
    ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}, "xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
AzureEnvironment.AzureGlobalCloud);

var azure = Azure
    .Configure()
    .Authenticate(azureCredentials)
    .WithSubscription("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");

IContainerGroup containerGroup = azure.ContainerGroups.Define("mytestgroup")
    .WithRegion(Region.EuropeWest)
    .WithExistingResourceGroup("mytest-rg")
    .WithLinux()
    .WithPrivateImageRegistry("mytestreg.azurecr.io", "mytestreg", "xxxxxxxxxxxxxx")
    .WithoutVolume()
    .DefineContainerInstance("mytestgroup")
    .WithImage("mytestimage/latest")
    .WithExternalTcpPort(5555)
    .WithCpuCoreCount(.5)
    .WithMemorySizeInGB(.5)
    .Attach()
    .Create();

The above code keeps giving me the exception:

Microsoft.Rest.Azure.CloudException: 'The image 'mytestimage/latest' in container group 'mytestgroup' is not accessible. Please check the image and registry credential.'

I have tried a couple of things;

  1. Testing the credentials with docker login - no problem.
  2. Pulling the image with docker pull mytestreg.azurecr.io/mytestimage - no problem.
  3. Swapping WithPrivateImageRegistry with WithPublicImageRegistryOnly and just using debian in WithImage - works as intented - no problem.
  4. Leaving the latest tag out of the image name - still doesn't work.

I have no idea why the credentials for the private Registry won't work - I have been copy/pasting directly from the Azure Portal to avoid typos, tried typing in manually etc.

Using Fiddler to inspect the traffic doesn't reveal anything interesting, other than the above exception message is returned directly from the Azure Management API.

What is the obvious thing that I am missing?

解决方案

The answer above (ie using full azure registry server name):

.WithImage("mytestreg.azurecr.io/mytestimage:latest")

seems to be part of the solution, but even with that change I was still seeing this error. Looking through other examples on the web (https://github.com/Azure-Samples/aci-dotnet-create-container-groups-using-private-registry/blob/master/Program.cs) containing what I needed, I changed my azure authentication from:

azure = Azure.Authenticate(authFilePath).WithDefaultSubscription();

to:

AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(authFilePath);
azure = Azure
    .Configure()
    .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
    .Authenticate(credentials)
    .WithDefaultSubscription();

and with THAT change, things are now working correctly.

这篇关于使用Azure容器注册表从C#创建新的Azure容器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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