Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
可以使用 Azure Batch 在 Linux 和 Windows 虚拟机上运行并行计算工作负荷。 本文详细介绍了如何使用 Batch Python 和 Azure.Compute.Batch 两个客户端库在 Batch 服务中创建 Linux 计算节点池。
虚拟机配置
在 Batch 中创建计算节点池时,可以使用两个选项来选择节点大小和操作系统:“云服务配置”和“虚拟机配置”。 虚拟机配置池由 Azure VM 组成,这些 VM 可以从 Linux 或 Windows 映像中创建。 使用虚拟机配置创建池时,请指定可用的计算节点大小、要安装在节点上的虚拟机映像引用以及 Batch 节点代理 SKU(在每个节点上运行并提供节点和 Batch 服务之间接口的程序)。
虚拟机映像引用
Batch 服务使用虚拟机规模集提供虚拟机配置中的计算节点。 可指定 Azure 市场中的一个映像,或使用 Azure Compute Gallery 准备自定义映像。
在创建虚拟机映像引用时,必须指定以下属性:
| 映像引用属性 | 示例 |
|---|---|
| 发布方 | 标准 |
| 优惠 | 0001-com-ubuntu-server-focal |
| SKU | 20_04-lts |
| 版本 | 最新 |
提示
可以在使用 Azure CLI 在 Azure 市场中查找 Linux VM 映像中详细了解这些属性以及如何指定市场映像。 请注意,某些 Marketplace 映像当前与 Batch 不兼容。
虚拟机映像列表
并非所有市场中的映像都兼容当前可用的 Batch 节点代理。 若要列出 Batch 服务及其相应节点代理 SKU 支持的所有市场虚拟机映像,请使用 list_supported_images (Python)、ListSupportedImages (Batch .NET) 或其他语言 SDK 的相应 API。
节点代理 SKU
Batch 节点代理是一个程序,它在池中的每个节点上运行,并在节点与 Batch 服务之间提供命令和控制接口。 节点代理对于不同操作系统有不同的实现(称为 SKU)。 从根本上讲,在创建虚拟机配置时,需要先指定虚拟机映像引用,然后指定要在其上安装映像的代理节点。 通常,每个节点代理 SKU 与多个虚拟机映像兼容。 若要查看支持的节点代理 SKU 和虚拟机映像兼容性,可以使用 Azure Batch CLI 命令:
az batch pool supported-images list
有关详细信息,请参阅 帐户 - 列出支持的镜像 - REST API(Azure Batch 服务)| Azure。
创建 Linux 池:Batch Python
以下代码片段示范如何使用用于 Python 的 Azure Batch 客户端库创建 Ubuntu Server 计算节点池。 有关 Batch Python 模块的更多详细信息,请查看参考文档。
此代码片段显式创建 BatchVmImageReference 并指定其每个属性(发布者、产品/服务、SKU、版本)。 不过,在生产代码中,我们建议您使用 list_supported_images 方法,在运行时从可用的映像与节点代理 SKU 组合中进行选择。
# Import the required modules from the
# Azure Batch Client Library for Python
from azure.batch import BatchClient, models
from azure.core.credentials import AzureNamedKeyCredential
# Specify Batch account credentials
account = "<batch-account-name>"
key = "<batch-account-key>"
account_endpoint = "<batch-account-url>"
# Pool settings
pool_id = "LinuxNodesSamplePoolPython"
vm_size = "STANDARD_D2_V3"
node_count = 1
# Initialize the Batch client
creds = AzureNamedKeyCredential(account, key)
client = BatchClient(endpoint=account_endpoint, credential=creds)
# Configure the start task for the pool
start_task = models.BatchStartTask(
command_line="printenv AZ_BATCH_NODE_STARTUP_DIR",
user_identity=models.UserIdentity(
auto_user=models.AutoUserSpecification(
elevation_level=models.ElevationLevel.ADMIN,
scope=models.AutoUserScope.POOL,
)
),
)
# Create an ImageReference which specifies the Marketplace
# virtual machine image to install on the nodes
ir = models.BatchVmImageReference(
publisher="canonical",
offer="0001-com-ubuntu-server-focal",
sku="20_04-lts",
version="latest")
# Create the VirtualMachineConfiguration, specifying
# the VM image reference and the Batch node agent
# to install on the node
vmc = models.VirtualMachineConfiguration(
image_reference=ir,
node_agent_sku_id="batch.node.ubuntu 20.04")
# Create the unbound pool
new_pool = models.BatchPoolCreateOptions(
id=pool_id,
vm_size=vm_size,
target_dedicated_nodes=node_count,
virtual_machine_configuration=vmc,
start_task=start_task,
)
# Create pool in the Batch service
client.create_pool(pool=new_pool)
如前所述,我们建议使用 list_supported_images 方法从当前支持的节点代理/市场映像组合(而不是显式创建 BatchVmImageReference )中动态选择。 以下 Python 代码片段演示如何使用此方法。
# Get the list of supported images from the Batch service
images = list(client.list_supported_images())
# Obtain the desired image reference
image = None
for img in images:
if (img.image_reference.publisher.lower() == "canonical" and
img.image_reference.offer.lower() == "0001-com-ubuntu-server-focal" and
img.image_reference.sku.lower() == "20_04-lts"):
image = img
break
if image is None:
raise RuntimeError('invalid image reference for desired configuration')
# Create the VirtualMachineConfiguration, specifying the VM image
# reference and the Batch node agent to be installed on the node
vmc = models.VirtualMachineConfiguration(
image_reference=image.image_reference,
node_agent_sku_id=image.node_agent_sku_id)
创建 Linux 池:批处理 .NET
以下代码片段展示了如何使用 Azure.Compute.Batch 和 Azure.ResourceManager.Batch 客户端库来创建 Ubuntu Server 计算节点池的示例。 有关更多详细信息,请参阅 参考文档。
以下代码片段使用 BatchClient.GetSupportedImages 方法从当前受支持的市场映像和节点代理 SKU 组合列表中选择。 建议使用这种方法,因为受支持组合的列表有时会发生变化。 通常会添加受支持的组合。
// Pool settings
const string poolId = "LinuxNodesSamplePoolDotNet";
const string vmSize = "STANDARD_D2_V3";
const int nodeCount = 1;
// Obtain a collection of all available node agent SKUs.
// This allows us to select from a list of supported
// VM image/node agent combinations.
List<BatchSupportedImage> images = new List<BatchSupportedImage>();
await foreach (BatchSupportedImage img in batchClient.GetSupportedImagesAsync())
{
images.Add(img);
}
// Find the appropriate image information
BatchSupportedImage image = null;
foreach (var img in images)
{
if (img.ImageReference.Publisher == "canonical" &&
img.ImageReference.Offer == "0001-com-ubuntu-server-focal" &&
img.ImageReference.Sku == "20_04-lts")
{
image = img;
break;
}
}
// Create the BatchVmConfiguration for use when actually creating the pool.
// Note that the data-plane discovery uses Azure.Compute.Batch.BatchVmImageReference
// but the ARM pool data type uses Azure.ResourceManager.Batch.Models.BatchImageReference.
BatchVmConfiguration vmConfiguration = new BatchVmConfiguration(
imageReference: new BatchImageReference()
{
Publisher = image.ImageReference.Publisher,
Offer = image.ImageReference.Offer,
Sku = image.ImageReference.Sku,
Version = image.ImageReference.Version
},
nodeAgentSkuId: image.NodeAgentSkuId);
BatchAccountPoolData poolData = new BatchAccountPoolData()
{
VmSize = vmSize,
DeploymentConfiguration = new BatchDeploymentConfiguration() { VmConfiguration = vmConfiguration },
ScaleSettings = new BatchAccountPoolScaleSettings()
{
FixedScale = new BatchAccountFixedScaleSettings() { TargetDedicatedNodes = nodeCount }
}
};
// Commit the pool to the Batch service via Azure.ResourceManager.Batch.
await batchAccount.GetBatchAccountPools().CreateOrUpdateAsync(WaitUntil.Completed, poolId, poolData);
尽管前面的代码片段使用 BatchClient.GetSupportedImages 方法动态列出并从支持的映像和节点代理 SKU 组合中进行选择(建议),但也可以显式配置 BatchVmImageReference :
BatchImageReference imageReference = new BatchImageReference()
{
Publisher = "canonical",
Offer = "0001-com-ubuntu-server-focal",
Sku = "20_04-lts",
Version = "latest"
};
使用 SSH 连接到 Linux 节点
在开发期间或进行故障排除时,可能会发现需要登录到池中的节点。 与 Windows 计算节点不同,你无法使用远程桌面协议 (RDP) 来连接到 Linux 节点。 相反,Batch 服务在每个节点上启用 SSH 访问以建立远程连接。
以下 Python 代码片段会在池中的每个节点上创建一个用户(远程连接时需要)。 然后打印每个节点的安全 Shell(SSH)连接信息。
import datetime
import getpass
from azure.batch import BatchClient, models
from azure.core.credentials import AzureNamedKeyCredential
# Specify your own account credentials
batch_account_name = ''
batch_account_key = ''
batch_account_url = ''
# Specify the ID of an existing pool containing Linux nodes
# currently in the 'idle' state
pool_id = ''
# Specify the username and prompt for a password
username = 'linuxuser'
password = getpass.getpass()
# Create a BatchClient
credentials = AzureNamedKeyCredential(
batch_account_name,
batch_account_key
)
batch_client = BatchClient(
endpoint=batch_account_url,
credential=credentials
)
# Create the user that will be added to each node in the pool
user = models.BatchNodeUserCreateOptions(
name=username,
password=password,
is_admin=True,
expiry_time=(datetime.datetime.utcnow() + datetime.timedelta(days=30)),
)
# Get the list of nodes in the pool
nodes = batch_client.list_nodes(pool_id=pool_id)
# Add the user to each node in the pool and print
# the connection information for the node
for node in nodes:
# Add the user to the node
batch_client.create_node_user(pool_id=pool_id, node_id=node.id, user=user)
# Obtain SSH login information for the node
login = batch_client.get_node_remote_login_settings(pool_id=pool_id,
node_id=node.id)
# Print the connection info for the node
print("{0} | {1} | {2} | {3}".format(node.id,
node.state,
login.remote_login_ip_address,
login.remote_login_port))
此代码将会有类似于以下示例的输出。 在本例中,该池包含四个 Linux 节点。
Password:
tvm-1219235766_1-20160414t192511z | ComputeNodeState.idle | 13.91.7.57 | 50000
tvm-1219235766_2-20160414t192511z | ComputeNodeState.idle | 13.91.7.57 | 50003
tvm-1219235766_3-20160414t192511z | ComputeNodeState.idle | 13.91.7.57 | 50002
tvm-1219235766_4-20160414t192511z | ComputeNodeState.idle | 13.91.7.57 | 50001
在节点上创建用户时不需要指定密码,而可以指定 SSH 公钥。
在 Python SDK 中,在 BatchNodeUserCreateOptions 上使用 ssh_public_key 参数。
在.NET中,使用 BatchNodeUserCreateOptions.SshPublicKey 属性。
定价
Azure Batch 构建在 Azure 云服务和 Azure 虚拟机技术基础之上。 Batch 服务本身免费提供,这意味着您仅需为 Batch 解决方案所使用的计算资源付费(以及由此产生的相关费用)。 如果选择“虚拟机配置”,系统会根据虚拟机定价结构收费。
如果使用应用程序包将应用程序部署到 Batch 节点,系统还会对应用程序包使用的 Azure 存储资源收费。
后续步骤
- 探索 azure-batch-samples GitHub 存储库中的 Python 代码示例,以了解如何执行常见 Batch 操作,如创建池、作业和任务。 Python 示例随附的 README 文件包含有关如何安装所需包的详细信息。