You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Azure Java SDK为容器实例附加私有VNet/Subnet的问题咨询

解决Azure Java SDK创建ACI时附加VNet和子网的问题

我来帮你搞定这个问题!你当前的Java代码片段刚好缺少了将ACI关联到现有虚拟网络和子网的配置逻辑,而你用Azure CLI时的--subnet和--vnet-name参数对应的就是这部分操作。下面是修改后的完整代码,以及关键细节的说明:

修改后的Java代码示例

ContainerGroup containerGroup = azure.containerGroups().define(aciName)
    .withRegion(Region.EUROPE_NORTH)
    .withExistingResourceGroup(rgName)
    .withLinux()
    // 关键新增:关联现有虚拟网络和子网
    .withExistingVirtualNetwork(vnetName, rgName)
    .withSubnet(subnetName)
    .withPrivateImageRegistry(registryServer, registryServerName, registryServerKey)
    .defineVolume(volumeMountName)
        .withExistingReadOnlyAzureFileShare(fileShareName)
        .withStorageAccountName(storageAccountName)
        .withStorageAccountKey(storageAccountKey)
        .attach()
    .defineContainerInstance(aciName)
        .withImage(containerImageName)
        // 提示:如果ACI只需要和VNet内的VM通信,可移除外部端口暴露,更安全
        .withExternalTcpPort(80)
        .withVolumeMountSetting(volumeMountName, volumeMountPath)
        .withCpuCoreCount(1)
        .withMemorySizeInGB(1.5)
        .withEnvironmentVariable("APP_PATH", volumeMountPath)
        .withStartingCommandLine(commandLineArgs.toString())
        .attach()
    .withDnsPrefix(aciName)
    .withRestartPolicy(ContainerGroupRestartPolicy.NEVER)
    .create();

核心配置详解

  • withExistingVirtualNetwork(vnetName, rgName):用来指定ACI要绑定的现有虚拟网络。如果你的VNet和ACI在同一个资源组里,第二个参数可以省略,直接用withExistingVirtualNetwork(vnetName)就行。
  • withSubnet(subnetName):指定虚拟网络内要使用的子网名称。这里要注意,这个子网不能被其他类型的Azure资源占用,而且地址空间要足够ACI使用。

额外注意点

  1. 先确认你的Azure Java SDK是最新稳定版,旧版本可能没有这些VNet相关的API。如果用的是azure-resourcemanager-containerinstance包,建议升级到最新版避免兼容性问题。
  2. 确保子网对应的**网络安全组(NSG)**规则允许ACI和你的VM组件之间的通信,不然即使关联了VNet也无法正常交互。
  3. 当ACI加入VNet后,默认会分配私有IP,如果你不需要外部访问,建议移除.withExternalTcpPort(80)这行,减少暴露面。

内容的提问来源于stack exchange,提问作者Archana Ashwin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:32:50