通过用于 .NET 的 Batch Management 客户端库管理 Batch 帐户和配额Manage Batch accounts and quotas with the Batch Management client library for .NET

可以使用 Batch 管理 .NET 库自动执行 Batch 帐户创建、删除、密钥管理和配额发现操作,从而降低 Azure Batch 应用程序的维护开销。You can lower maintenance overhead in your Azure Batch applications by using the Batch Management .NET library to automate Batch account creation, deletion, key management, and quota discovery.

  • 在任何区域中创建和删除 Batch 帐户Create and delete Batch accounts within any region. 例如,如果你是一家独立软件供应商 (ISV),现在要为每个分配了不同计费 Batch 帐户的客户提供服务,则可以将帐户创建和删除功能添加到客户门户中。If, as an independent software vendor (ISV) for example, you provide a service for your clients in which each is assigned a separate Batch account for billing purposes, you can add account creation and deletion capabilities to your customer portal.
  • 以编程方式为任何 Batch 帐户检索和重新生成帐户密钥Retrieve and regenerate account keys programmatically for any of your Batch accounts. 这样可确保遵循安全策略,以便定期滚动更新帐户密钥或令其过期。This can help you comply with security policies that enforce periodic rollover or expiry of account keys. 当各种不同的 Azure 区域中有多个 Batch 帐户时,将此滚动更新过程自动化会提高解决方案的效率。When you have several Batch accounts in various Azure regions, automation of this rollover process increases your solution's efficiency.
  • 检查帐户配额 并采取试错猜测,确定哪些 Batch 帐户存在哪些限制。Check account quotas and take the trial-and-error guesswork out of determining which Batch accounts have what limits. 在启动作业、创建池或添加计算节点之前检查帐户配额可以主动调整创建计算资源的位置或时机。By checking your account quotas before starting jobs, creating pools, or adding compute nodes, you can proactively adjust where or when these compute resources are created. 可以先确定哪些帐户需要增加配额,再为这些帐户分配其他资源。You can determine which accounts require quota increases before allocating additional resources in those accounts.
  • 通过在同一应用程序中使用 Batch Management .NET、Azure Active DirectoryAzure 资源管理器,用户可以结合其他 Azure 服务的功能获得全功能管理体验。Combine features of other Azure services for a full-featured management experience--by using Batch Management .NET, Azure Active Directory, and the Azure Resource Manager together in the same application. 使用这些功能及其 API 可以提供顺畅的身份验证体验、创建和删除资源组以及上述功能,以获取端到端管理解决方案。By using these features and their APIs, you can provide a frictionless authentication experience, the ability to create and delete resource groups, and the capabilities that are described above for an end-to-end management solution.

备注

尽管本文着重介绍如何以编程方式管理 Batch 帐户、密钥和配额,但你可以使用 Azure 门户执行其中的许多活动。While this article focuses on the programmatic management of your Batch accounts, keys, and quotas, you can perform many of these activities by using the Azure portal. 有关详细信息,请参阅使用 Azure 门户创建 Azure Batch 帐户以及 Azure Batch 服务的配额和限制For more information, see Create an Azure Batch account using the Azure portal and Quotas and limits for the Azure Batch service.

创建和删除 Batch 帐户Create and delete Batch accounts

如上所述,Batch Management API 的主要功能之一就是在 Azure 区域中创建和删除 Batch 帐户。As mentioned, one of the primary features of the Batch Management API is to create and delete Batch accounts in an Azure region. 为此,请使用 BatchManagementClient.Account.CreateAsync and DeleteAsync,或其同步等效命令。To do so, use BatchManagementClient.Account.CreateAsync and DeleteAsync, or their synchronous counterparts.

以下代码片段将创建一个帐户,从 Batch 服务获取新建的帐户,并将它删除。The following code snippet creates an account, obtains the newly created account from the Batch service, and then deletes it. 在此代码片段以及本文的其他代码片段中,batchManagementClient 是完全初始化的 BatchManagementClient 实例。In this snippet and the others in this article, batchManagementClient is a fully initialized instance of BatchManagementClient.

// Create a new Batch account
await batchManagementClient.Account.CreateAsync("MyResourceGroup",
    "mynewaccount",
    new BatchAccountCreateParameters() { Location = "China North" });

// Get the new account from the Batch service
AccountResource account = await batchManagementClient.Account.GetAsync(
    "MyResourceGroup",
    "mynewaccount");

// Delete the account
await batchManagementClient.Account.DeleteAsync("MyResourceGroup", account.Name);

备注

使用 Batch Management .NET 库及其 BatchManagementClient 类的应用程序需有服务管理员共同管理员访问权限才能使用拥有要管理的 Batch 帐户的订阅。Applications that use the Batch Management .NET library and its BatchManagementClient class require service administrator or coadministrator access to the subscription that owns the Batch account to be managed. 有关详细信息,请参阅 Azure Active Directory 部分和 AccountManagement 代码示例。For more information, see the Azure Active Directory section and the AccountManagement code sample.

检索和重新生成帐户密钥Retrieve and regenerate account keys

使用 ListKeysAsync 从订阅中的任何 Batch 帐户获取主要和辅助帐户密钥。Obtain primary and secondary account keys from any Batch account within your subscription by using ListKeysAsync. 可以使用 RegenerateKeyAsync 重新生成这些密钥。You can regenerate those keys by using RegenerateKeyAsync.

// Get and print the primary and secondary keys
BatchAccountListKeyResult accountKeys =
    await batchManagementClient.Account.ListKeysAsync(
        "MyResourceGroup",
        "mybatchaccount");
Console.WriteLine("Primary key:   {0}", accountKeys.Primary);
Console.WriteLine("Secondary key: {0}", accountKeys.Secondary);

// Regenerate the primary key
BatchAccountRegenerateKeyResponse newKeys =
    await batchManagementClient.Account.RegenerateKeyAsync(
        "MyResourceGroup",
        "mybatchaccount",
        new BatchAccountRegenerateKeyParameters() {
            KeyName = AccountKeyType.Primary
            });

提示

可以为管理应用程序创建简化的连接工作流。You can create a streamlined connection workflow for your management applications. 首先,获取希望使用 ListKeysAsync 管理的 Batch 帐户的帐户密钥. Then, use this key when initializing the Batch .NET library's BatchSharedKeyCredentials,该类在初始化 BatchClient 时使用。First, obtain an account key for the Batch account you wish to manage with ListKeysAsync. Then, use this key when initializing the Batch .NET library's BatchSharedKeyCredentials class, which is used when initializing BatchClient.

检查 Azure 订阅和 Batch 帐户配额Check Azure subscription and Batch account quotas

Azure 订阅和类似于 Batch 的各个 Azure 服务均有默认配额,用于限制其中特定实体的数目。Azure subscriptions and the individual Azure services like Batch all have default quotas that limit the number of certain entities within them. 有关 Azure 订阅的默认配额,请参阅 Azure 订阅和服务限制、配额与约束For the default quotas for Azure subscriptions, see Azure subscription and service limits, quotas, and constraints. 有关 Batch 服务的默认配额,请参阅 Azure Batch 服务的配额和限制For the default quotas of the Batch service, see Quotas and limits for the Azure Batch service. 使用 Batch 管理 .NET 库可以在应用程序中检查这些配额。By using the Batch Management .NET library, you can check these quotas in your applications. 这样,便可以在添加帐户或计算资源(如池和计算节点)之前做出分配决策。This enables you to make allocation decisions before you add accounts or compute resources like pools and compute nodes.

检查 Azure 订阅和 Batch 帐户配额Check an Azure subscription for Batch account quotas

在区域中创建 Batch 帐户之前,可以检查 Azure 订阅,看是否能将帐户添加到该区域中。Before creating a Batch account in a region, you can check your Azure subscription to see whether you are able to add an account in that region.

在以下的代码片段中,我们先使用 BatchManagementClient.Account.ListAsync 来获取订阅中所有 Batch 帐户的集合。In the code snippet below, we first use BatchManagementClient.Account.ListAsync to get a collection of all Batch accounts that are within a subscription. 获取此集合后,可以确定目标区域有多少个帐户。Once we've obtained this collection, we determine how many accounts are in the target region. 然后使用 BatchManagementClient.Subscriptions 来获取 Batch 帐户配额,并确定可以在该区域中创建多少个帐户(如果有)。Then we use BatchManagementClient.Subscriptions to obtain the Batch account quota and determine how many accounts (if any) can be created in that region.

// Get a collection of all Batch accounts within the subscription
BatchAccountListResponse listResponse =
        await batchManagementClient.Account.ListAsync(new AccountListParameters());
IList<AccountResource> accounts = listResponse.Accounts;
Console.WriteLine("Total number of Batch accounts under subscription id {0}:  {1}",
    creds.SubscriptionId,
    accounts.Count);

// Get a count of all accounts within the target region
string region = "chinanorth";
int accountsInRegion = accounts.Count(o => o.Location == region);

// Get the account quota for the specified region
SubscriptionQuotasGetResponse quotaResponse = await batchManagementClient.Subscriptions.GetSubscriptionQuotasAsync(region);
Console.WriteLine("Account quota for {0} region: {1}", region, quotaResponse.AccountQuota);

// Determine how many accounts can be created in the target region
Console.WriteLine("Accounts in {0}: {1}", region, accountsInRegion);
Console.WriteLine("You can create {0} accounts in the {1} region.", quotaResponse.AccountQuota - accountsInRegion, region);

在上面的代码片段中,credsTokenCloudCredentials 的实例。In the snippet above, creds is an instance of TokenCloudCredentials. 若要查看创建此对象的示例,请参阅 GitHub 上的 AccountManagement 代码示例。To see an example of creating this object, see the AccountManagement code sample on GitHub.

检查 Batch 帐户的计算资源配额Check a Batch account for compute resource quotas

增加 Batch 解决方案中的计算资源之前,可以通过检查来确保需要分配的资源不超过帐户的配额。Before increasing compute resources in your Batch solution, you can check to ensure the resources you want to allocate won't exceed the account's quotas. 在以下代码片段中,输出名为 mybatchaccount的 Batch 帐户的配额信息。In the code snippet below, we print the quota information for the Batch account named mybatchaccount. 在用户自己的应用程序中,可以使用此类信息来确定帐户是否可以处理要创建的其他资源。In your own application, you could use such information to determine whether the account can handle the additional resources to be created.

// First obtain the Batch account
BatchAccountGetResponse getResponse =
    await batchManagementClient.Account.GetAsync("MyResourceGroup", "mybatchaccount");
AccountResource account = getResponse.Resource;

// Now print the compute resource quotas for the account
Console.WriteLine("Core quota: {0}", account.Properties.CoreQuota);
Console.WriteLine("Pool quota: {0}", account.Properties.PoolQuota);
Console.WriteLine("Active job and job schedule quota: {0}", account.Properties.ActiveJobAndJobScheduleQuota);

重要

尽管 Azure 订阅和服务有默认配额,但许多限制都可以通过在 Azure 门户中提出请求来提高。While there are default quotas for Azure subscriptions and services, many of these limits can be raised by issuing a request in the Azure portal. 例如,可以参阅 Azure Batch 服务的配额和限制以获取有关提高 Batch 帐户配额的说明。For example, see Quotas and limits for the Azure Batch service for instructions on increasing your Batch account quotas.

将 Azure AD 和 Batch Management .NET 配合使用Use Azure AD with Batch Management .NET

Batch 管理 .NET 库是 Azure 资源提供程序客户端,与 Azure 资源管理器配合使用,用来以编程方式管理帐户资源。The Batch Management .NET library is an Azure resource provider client, and is used together with Azure Resource Manager to manage account resources programmatically. 需要使用 Azure AD 对通过任何 Azure 资源提供程序客户端(包括 Batch 管理 .NET 库)和 Azure 资源管理器发出的请求进行身份验证。Azure AD is required to authenticate requests made through any Azure resource provider client, including the Batch Management .NET library, and through Azure Resource Manager. 有关将 Azure AD 和 Batch 管理 .NET 库配合使用的信息,请参阅使用 Azure Active Directory 对 Batch 解决方案进行身份验证For information about using Azure AD with the Batch Management .NET library, see Use Azure Active Directory to authenticate Batch solutions.

GitHub 上的示例项目 Sample project on GitHub

若要看到 Batch 管理 .NET 起作用,请查看 GitHub 上的 AccountManagement 示例项目。To see Batch Management .NET in action, check out the AccountManagement sample project on GitHub. AccountManagement 示例应用程序演示了以下操作:The AccountManagement sample application demonstrates the following operations:

  1. 使用 ADAL 从 Azure AD 获取安全令牌。Acquire a security token from Azure AD by using ADAL. 如果用户尚未登录,系统会提示其输入 Azure 凭据。If the user is not already signed in, they are prompted for their Azure credentials.
  2. 使用从 Azure AD 获取的安全令牌,创建 SubscriptionClient,以在 Azure 中查询一组与帐户关联的订阅。With the security token obtained from Azure AD, create a SubscriptionClient to query Azure for a list of subscriptions associated with the account. 如果列表包含多个订阅,则用户可从中选择一个订阅。The user can select a subscription from the list if it contains more than one subscription.
  3. 获取与所选订阅关联的凭据。Get credentials associated with the selected subscription.
  4. 使用凭据创建 ResourceManagementClient 对象。Create a ResourceManagementClient object by using the credentials.
  5. 使用 ResourceManagementClient 对象创建资源组。Use a ResourceManagementClient object to create a resource group.
  6. 使用 BatchManagementClient 对象执行多项 Batch 帐户操作:Use a BatchManagementClient object to perform several Batch account operations:
    • 在新资源组中创建 Batch 帐户。Create a Batch account in the new resource group.
    • 从 Batch 服务获取新建的帐户。Get the newly created account from the Batch service.
    • 输出新帐户的帐户密钥。Print the account keys for the new account.
    • 重新生成帐户的新主密钥。Regenerate a new primary key for the account.
    • 输出帐户的配额信息。Print the quota information for the account.
    • 输出订阅的配额信息。Print the quota information for the subscription.
    • 输出订阅中的所有帐户。Print all accounts within the subscription.
    • 删除新建的帐户。Delete newly created account.
  7. 删除该资源组。Delete the resource group.

删除新建的 Batch 帐户和资源组之前,可以在 Azure 门户中查看它们:Before deleting the newly created Batch account and resource group, you can view them in the Azure portal:

要成功运行示例应用程序,必须首先在 Azure 门户中将其注册到 Azure AD 租户,并向 Azure Resource Manager API 授予权限。To run the sample application successfully, you must first register it with your Azure AD tenant in the Azure portal and grant permissions to the Azure Resource Manager API. 按照使用 Azure AD 对 Batch 管理应用程序进行验证中提供的步骤操作。Follow the steps provided in Authenticate Batch Management solutions with Active Directory.