如何使用python在azure vnet中创建新的容器组

2024-03-29 06:49:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用1个容器实例创建Azure容器组,该实例将使用python集成到特定的Vnet中。但是我没有发现任何有用的东西,有人能帮我吗


Tags: 实例azure容器vnet
1条回答
网友
1楼 · 发布于 2024-03-29 06:49:16

您可以使用以下代码获取容器组要使用的委派子网:

from azure.identity import AzureCliCredential
from azure.mgmt.network import NetworkManagementClient
credential = AzureCliCredential()
subscription_id = "948d4068-xxxxxx-xxxxx-xxxxx-e00a844e059b"
#credential = DefaultAzureCredential
network_client = NetworkManagementClient(credential, subscription_id)
resource_group_name = "ansumantest"
location = "West US 2"
virtual_network_name = "ansuman-vnet"
subnet_name = "acisubnet"
Subnet=network_client.subnets.get(resource_group_name, virtual_network_name, subnet_name)
print(Subnet.id)

输出:

enter image description here

您可以根据自己的要求在上述代码中添加类似的内容,以获取子网:

from azure.mgmt.containerinstance import ContainerInstanceManagementClient
from azure.mgmt.containerinstance.models import (ContainerGroup,
                                                 Container,
                                                 ContainerGroupNetworkProtocol,
                                                 ImageRegistryCredential,
                                                 ContainerPort,
                                                 IpAddress,
                                                 Port,
                                                 ResourceRequests,
                                                 ResourceRequirements,
                                                 ContainerGroupSubnetId,
                                                 OperatingSystemTypes)
    subnet_id = Subnet.id
    container_client = ContainerInstanceManagementClient(credential,subscription_id)
    container_image_name = "your private image present in acr "
    user_name = "username for the server"
    password= "password for the server"
    # Configure the container
    container_resource_requests = ResourceRequests(memory_in_gb=1, cpu=1.0)
    container_resource_requirements = ResourceRequirements(requests=container_resource_requests)
    container = Container(name=container_group_name,image=container_image_name,resources=container_resource_requirements,ports=[ContainerPort(port=80)])
    # Configure the container group
    ports = [Port(protocol=ContainerGroupNetworkProtocol.tcp, port=80)]
    group_ip_address = IpAddress(ports=ports,type="Private")
    acisubnet = ContainerGroupSubnetId(id=subnet_id,name=subnet_name)
    imagecredentials= ImageRegistryCredential(server="server.azurecr.io",username=user_name,password=password)
    container_group= ContainerGroup(location=location,containers=[container], os_type=OperatingSystemTypes.WINDOWS,ip_address=group_ip_address,subnet_ids=[acisubnet],image_registry_credentials=imagecredentials)
    # Create the container group
    container_client.container_groups.begin_create_or_update(resource_group_name,container_group_name,container_group)

Note:

  • If you are creating a container group in your vnet then you won't be able to have public access anymore for the container group and you can't use a public image (error below) . You have to only use your private image present on your Azure container registry.

    enter image description here

  • If you are not sure about subnet delegation then please go to your vnet>>subnet>>click on subnet to be used by containerGroup>>select the subnet delegation as show in image>>save

    enter image description here

相关问题 更多 >