如何使用 python 在 azure vnet 中创建新的容器组
How to create new container group in azure vnet using python
我需要使用 1 个容器实例创建 Azure 容器组,这些容器实例将使用 python 集成到特定的 Vnet 中。但是我没有找到任何有用的东西,有人可以帮助我吗?
您可以使用以下代码获取容器组使用的委托子网:
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)
输出:
您可以根据您的要求在上面提供的代码中添加类似下面给出的内容以获取 subnetid:
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.
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
我需要使用 1 个容器实例创建 Azure 容器组,这些容器实例将使用 python 集成到特定的 Vnet 中。但是我没有找到任何有用的东西,有人可以帮助我吗?
您可以使用以下代码获取容器组使用的委托子网:
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)
输出:
您可以根据您的要求在上面提供的代码中添加类似下面给出的内容以获取 subnetid:
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.
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