قم بتوفير عقد حساب Linux في مجمعات الدُفعات

يمكنك استخدام Azure Batch لتشغيل أحمال عمل الحوسبة المتوازية على كل من الأجهزة الظاهرية Linux وWindows. تشرح هذه المقالة كيفية إنشاء مجموعات من عقد الحوسبة في لينكس في خدمة Batch باستخدام كل من Batch Python و Azure. مكتبات عملاء Compute.Batch.

تكوين جهاز ظاهري

عند إنشاء مجموعة من عُقد الحوسبة في Batch، يكون لديك خياران يمكنك من خلالهما تحديد حجم العقدة ونظام التشغيل: تكوين الخدمات السحابية وتكوين الجهاز الظاهري. تتكون مجموعات تكوين الجهاز الظاهري من Azure VMs، والتي يمكن إنشاؤها من صور Linux أو Windows. عند إنشاء تجمع باستخدام تكوين الجهاز الظاهري، فإنك تحدد حجم عقدة الحوسبة المتاحة، ومرجع صورة الجهاز الظاهري المراد تثبيته على العقد، ووكيل العقدة الدفعي SKU (برنامج يتم تشغيله على كل عقدة ويوفر واجهة بين العقدة وخدمة الدُفعات).

مرجع صورة الجهاز الظاهري

تستخدم خدمة الدُفعات مجموعات مقياس الآلة الظاهرية لتوفير عُقد الحوسبة في تكوين الجهاز الظاهري. يمكنك تحديد صورة من Azure Marketplace، أو استخدام Azure Compute Gallery لإعداد صورة مخصصة.

عند إنشاء مرجع لصورة جهاز ظاهري، يجب عليك تحديد الخصائص التالية:

خاصية مرجع الصورة مثال
الناشر قانوني
العرض 0001-com-ubuntu-server-focal
وحدة حفظ المخزون SKU 20_04-lts
إصدار الأحدث

تلميح

يمكنك معرفة المزيد حول هذه الخصائص وكيفية تحديد صور Azure Marketplace في البحث عن صور Linux VM في Azure Marketplace باستخدام Azure CLI. لاحظ أن بعض صور Azure Marketplace غير متوافقة حالياً مع الدُّفعة.

قائمة بصور الجهاز الظاهري

ليست جميع صور Azure Marketplace متوافقة مع وكلاء عقد الدُفعات المتاحين حالياً. لسرد جميع صور الآلة الافتراضية المدعومة في السوق لخدمة الدفعة ووحدات تعريف وكيل العقد المقابلة، استخدم list_supported_images (Python)، BatchClient.GetSupportedImagesAsync (Azure. Compute.Batch)، أو واجهة برمجة التطبيقات المقابلة في SDK لغة أخرى.

عامل عقدة وحدة حفظ المخزون

عامل العقدة الدفعة هو برنامج يعمل على كل عقدة في التجمع ويوفر واجهة الأمر والتحكم بين العقدة وخدمة الدفعة. هناك تطبيقات مختلفة لعامل العقدة، والمعروفة باسم وحدة حفظ المخزون، لأنظمة تشغيل مختلفة. بشكل أساسي، عند إنشاء تكوين جهاز ظاهري، فإنك تحدد أولاً مرجع صورة الجهاز الظاهري، ثم تحدد عامل العقدة لتثبيته على الصورة. بشكل عام، كل عامل عقدة وحدة حفظ المخزون متوافق مع صور الجهاز الظاهري متعددة. لعرض وحدات SKU لوكيل العقدة المدعوم وتوافقات صورة الجهاز الظاهري، يمكنك استخدام أمر Azure Batch CLI:

az batch pool supported-images list

لمزيد من المعلومات، يمكنك الرجوع إلى الحساب - قائمة الصور المدعومة - REST API (Azure Batch Service) | Microsoft Docs.

قم بإنشاء مجموعة Linux: Batch Python

يُظهر مقتطف الشفرة التالي مثالاً على كيفية استخدام Microsoft Azure Batch Client Library for Python لإنشاء مجموعة من عُقد حساب Ubuntu Server. لمزيد من التفاصيل حول وحدة Batch Python، قم بعرض الوثائق المرجعية.

هذا المقتطف ينشئ BatchVmImageReference بشكل صريح ويحدد كل خصائصه (الناشر، العرض، SKU، الإصدار). ومع ذلك، في كود الإنتاج، نوصي باستخدام طريقة list_supported_images للاختيار من مجموعات وحدة حفظ المخزون لعامل العقدة والصورة المتوفرة في وقت التشغيل.

# 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: Batch.NET

يظهر مقتطف الكود التالي مثالا على كيفية استخدام Azure. Compute.Batch و Azure. ResourceManager.Batch مكتبات العملاء لإنشاء مجموعة من عقد الحوسبة في خادم أوبونتو. لمزيد من التفاصيل، راجع وثائق المرجع.

يستخدم مقتطف الكود التالي طريقة 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"
};

اتصل بعُقد Linux باستخدام SSH

أثناء التطوير أو أثناء استكشاف الأخطاء وإصلاحها، قد تجد أنه من الضروري تسجيل الدخول إلى العقد الموجودة في مجموعتك. بخلاف عقد حساب Windows، لا يمكنك استخدام بروتوكول سطح المكتب البعيد (RDP) للاتصال بعُقد Linux. بدلاً من ذلك، تتيح خدمة Batch الوصول إلى SSH على كل عقدة للاتصال عن بُعد.

يُنشئ مقتطف كود Python التالي مستخدماً على كل عقدة في التجمع، وهو أمر مطلوب للاتصال عن بُعد. ثم يقوم بطباعة معلومات اتصال الغلاف الآمن (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، استخدم معامل ssh_public_key على BatchNodeUserCreateOptions.

في .NET، استخدم خاصية BatchNodeUserCreateOptions.SshPublicKey.

التسعير

تم إنشاء Azure Batch على خدمات خدمات Azure السحابية وتقنية Azure Virtual Machines. يتم تقديم خدمة الدُفعات نفسها دون تكلفة، ما يعني أنه يتم محاسبتك فقط على موارد الحوسبة (والتكاليف المرتبطة بها) التي تستهلكها حلول الدُفعات الخاصة بك. عند اختيار تكوين الجهاز الظاهري، يتم محاسبتك استنادا إلى بنية تسعير الأجهزة الظاهرية.

إذا قمت بنشر التطبيقات إلى عقد الدُفعات الخاصة بك باستخدام حزم التطبيقات، فستتحمل أيضاً تكاليف موارد تخزين Azure التي تستهلكها حزم التطبيقات الخاصة بك.

الخطوات التالية