如何使用 Azure SDK 在 java 中使用容器实例附加私有 vnet 或子网?

How to attach private vnet or subnet with container instances in java using Azure SDK?

创建 Azure 容器实例时附加子网和 vnet 配置的挑战。

我正在尝试使用 Azure SDK 创建 Azure 容器实例 - Java/.Net。此容器需要与跨不同 VM 的组件进行通信。我能够使用带有 vnet 和子网配置的 Azure CLI 命令来实现这一点。但无法通过 SDK 复制相同内容。

Java

中的代码片段
ContainerGroup containerGroup = azure.containerGroups().define(aciName).withRegion(Region.EUROPE_NORTH)
                    .withExistingResourceGroup(rgName).withLinux()
                    .withPrivateImageRegistry(registryServer, registryServerName, registryServerKey)
                    .defineVolume(volumeMountName).withExistingReadOnlyAzureFileShare(fileShareName)
                    .withStorageAccountName(storageAccountName).withStorageAccountKey(storageAccountKey).attach()
                    .defineContainerInstance(aciName).withImage(containerImageName).withExternalTcpPort(80)
                    .withVolumeMountSetting(volumeMountName, volumeMountPath).withCpuCoreCount(1)
                    .withMemorySizeInGB(1.5).withEnvironmentVariable("APP_PATH", volumeMountPath)
                    .withStartingCommandLine(commandLineArgs.toString()).attach().withDnsPrefix(aciName)
                    .withRestartPolicy(ContainerGroupRestartPolicy.NEVER).create();

Azure CLI

az container create --resource-group --name --image --cpu 1 --memory 1.5 --registry-login-server --registry-username --registry-password --azure-file-volume-share-name --azure-file-volume-account-name > --azure-file-volume-account-key --azure-file-volume-mount-path --restart-policy Never --e --subnet --subnet-address-prefix --vnet --vnet-name --subscription --command-line ""

创建 Azure 容器实例时无法附加 vnet 和子网配置。

很遗憾,Java 目前似乎不支持在 Vnet 中部署 ACI。你可以看看Github中的issue

如果您看一下可以实现它的 CLI 命令,那么您会发现该命令实际上是使用 Azure REST API 来实现的。

在请求中,它使用子网网络配置文件设置容器组 属性 networkProfile。所以如果你真的想在 Vnet 中部署 ACI,你可以使用 Azure REST API for Container Instance 在 Java 代码中实现它。