ציון הרשת לעבודה

במאמר הזה מוסבר איך מציינים את הרשת למכונות הווירטואליות שמריצות עבודה.

אתם יכולים לשלוט בחיבורים של המכונות הווירטואליות שמריצות עבודה על ידי ציון רשת עם הגישה הרצויה. לדוגמה, אפשר לציין רשת שמאפשרת לעבודת ה-Job לגשת למשאבים הנדרשים או להגביל את הגישה כדי לשפר את האבטחה. לחלופין, אם אין לכם דרישות לגבי רשתות ואתם לא רוצים להגדיר רשת לעבודה, אתם יכולים לדלג על הגדרת הרשת כדי להשתמש בהגדרת הרשת שמוגדרת כברירת מחדל.

מידע נוסף על מושגים של רשתות ומתי צריך להגדיר רשתות זמין במאמר סקירה כללית על רשתות Batch.

לפני שמתחילים

  1. אם עוד לא השתמשתם ב-Batch, כדאי לעיין במאמר תחילת העבודה עם Batch ולהפעיל את Batch על ידי השלמת הדרישות המוקדמות לפרויקטים ולמשתמשים.
  2. כדי לקבל את ההרשאות שדרושות ליצירת משימה שפועלת ברשת ספציפית, צריך לבקש מהאדמין להקצות לכם את תפקידי ה-IAM הבאים:

    להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.

    יכול להיות שאפשר לקבל את ההרשאות הנדרשות גם באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש.

  3. מזהים את הרשת שרוצים להשתמש בה לעבודה. הרשת שאתם מציינים למשרה צריכה לעמוד בדרישות הבאות: מידע נוסף זמין במאמר בנושא יצירה וניהול של רשתות VPC.
  4. אם רוצים שהעבודה תפעל בתת-רשת של רשת VPC משותפת שמארח פרויקט אחר, צריך להעניק לסוכן שירות Batch של הפרויקט הרשאה להשתמש בתת-הרשת הזו.

    כדי לוודא שלסוכן השירות של Batch בפרויקט יש את ההרשאות הנדרשות ליצירת משימה שפועלת ברשת משנה של רשת VPC משותפת, צריך לבקש מהאדמין להקצות לסוכן השירות של Batch בפרויקט את תפקיד ה-IAM‏ Compute Network User ‏ (roles/compute.networkUser) ברשת המשנה של ה-VPC המשותף.

    מידע נוסף זמין במאמר בנושא הגדרת VPC משותף לחשבונות שירות.

יצירת עבודה שפועלת ברשת ספציפית

מציינים את הרשת לעבודה כשיוצרים אותה. ספציפית, צריך לציין רשת VPC ותת-רשת שנמצאות במיקום שבו רוצים להריץ את העבודה הזו.

אם רוצים להשתמש בתבנית של הגדרות מכונה בזמן יצירת העבודה הזו, צריך לציין את הרשת בתבנית של הגדרות המכונה. אחרת, אפשר להשתמש בשלבים הבאים כדי לציין את הרשת למשימה באמצעות ה-CLI של gcloud או Batch API.

gcloud

כדי ליצור משימה שפועלת ברשת ספציפית באמצעות ה-CLI של gcloud, בוחרים באחת מהאפשרויות הבאות:

שימוש בדגלים של gcloud כדי לציין את הרשת של משימה

כדי ליצור עבודה ולהשתמש בפלאגים של gcloud כדי לציין את הרשת של העבודה, מבצעים את השלבים הבאים:

  1. יוצרים קובץ JSON שמציין את פרטי ההגדרה של העבודה.

    לדוגמה, כדי ליצור עבודת סקריפט בסיסית, יוצרים קובץ JSON עם התוכן הבא.

    {
      "taskGroups": [
        {
          "taskSpec": {
            "runnables": [
              {
                "script": {
                  "text": "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
                }
              }
            ]
          },
          "taskCount": 3
        }
      ],
      "logsPolicy": {
        "destination": "CLOUD_LOGGING"
      }
    }
    
  2. יוצרים את העבודה באמצעות הפקודה gcloud batch jobs submit. כדי לציין את הרשת של העבודה, צריך לכלול את הדגלים --network ו---subnetwork.

    gcloud batch jobs submit JOB_NAME \
        --location LOCATION \
        --config JSON_CONFIGURATION_FILE \
        --network projects/HOST_PROJECT_ID/global/networks/NETWORK \
        --subnetwork projects/HOST_PROJECT_ID/regions/REGION/subnetworks/SUBNET
    

    מחליפים את מה שכתוב בשדות הבאים:

    • JOB_NAME: השם של המשימה.
    • LOCATION: המיקום של המשרה.
    • JSON_CONFIGURATION_FILE: הנתיב לקובץ JSON עם פרטי ההגדרות של העבודה.
    • HOST_PROJECT_ID: מזהה הפרויקט של הפרויקט שבו נמצאת הרשת שציינתם:
      • אם אתם משתמשים ברשת VPC משותפת, צריך לציין את הפרויקט המארח.
      • אחרת, מציינים את הפרויקט הנוכחי.
    • NETWORK: השם של רשת VPC בפרויקט הנוכחי או של רשת VPC משותפת שמארחת את הפרויקט הנוכחי או שמשותפת איתו.
    • REGION: האזור שבו נמצאים תת-הרשת והמכונות הווירטואליות של העבודה:
      • אם כוללים את השדה allowedLocations כדי לציין את המיקום המותר של המכונות הווירטואליות עבור העבודה, צריך לציין כאן את אותו אזור.
      • אחרת, האזור צריך להיות זהה למיקום שבוחרים למשרה (LOCATION).
    • SUBNET: השם של תת-רשת שמהווה חלק מרשת ה-VPC ונמצאת באותו אזור כמו המכונות הווירטואליות של העבודה.

שימוש בשדות JSON כדי לציין את הרשת של משימה

כדי ליצור עבודה ולהשתמש בשדות בקובץ התצורה של JSON כדי לציין את הרשת של העבודה, מבצעים את השלבים הבאים:

  1. יוצרים קובץ JSON שמציין את פרטי ההגדרה של העבודה. כדי לציין את הרשת לעבודה, צריך לכלול את השדות network ו-subnetwork.

    לדוגמה, כדי ליצור עבודת סקריפט בסיסית שפועלת ברשת ספציפית, יוצרים קובץ JSON עם התוכן הבא.

    {
      "taskGroups": [
        {
          "taskSpec": {
            "runnables": [
              {
                "script": {
                  "text": "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
                }
              }
            ]
          },
          "taskCount": 3
        }
      ],
      "allocationPolicy": {
        "network": {
          "networkInterfaces": [
            {
              "network": "projects/HOST_PROJECT_ID/global/networks/NETWORK",
              "subnetwork": "projects/HOST_PROJECT_ID/regions/REGION/subnetworks/SUBNET"
            }
          ]
        }
      },
      "logsPolicy": {
        "destination": "CLOUD_LOGGING"
      }
    }
    

    מחליפים את מה שכתוב בשדות הבאים:

    • HOST_PROJECT_ID: מזהה הפרויקט של הפרויקט שבו נמצאת הרשת שציינתם:
      • אם אתם משתמשים ברשת VPC משותפת, צריך לציין את הפרויקט המארח.
      • אחרת, מציינים את הפרויקט הנוכחי.
    • NETWORK: השם של רשת VPC בפרויקט הנוכחי או של רשת VPC משותפת שמארחת את הפרויקט הנוכחי או שמשותפת איתו.
    • REGION: האזור שבו נמצאים תת-הרשת והמכונות הווירטואליות של העבודה:
      • אם כוללים את השדה allowedLocations כדי לציין את המיקום המותר של המכונות הווירטואליות עבור העבודה, צריך לציין כאן את אותו אזור.
      • אחרת, האזור צריך להיות זהה למיקום שבוחרים למשרה (LOCATION).
    • SUBNET: השם של תת-רשת שמהווה חלק מרשת ה-VPC ונמצאת באותו אזור כמו המכונות הווירטואליות של העבודה.
  2. יוצרים את העבודה באמצעות הפקודה gcloud batch jobs submit.

    gcloud batch jobs submit JOB_NAME \
        --location LOCATION \
        --config JSON_CONFIGURATION_FILE
    

    מחליפים את מה שכתוב בשדות הבאים:

    • JOB_NAME: השם של המשימה.
    • LOCATION: המיקום של המשרה.
    • JSON_CONFIGURATION_FILE: הנתיב לקובץ JSON עם פרטי ההגדרות של העבודה.

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.LogsPolicy;
import com.google.cloud.batch.v1.LogsPolicy.Destination;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.Runnable.Script;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.protobuf.Duration;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateBatchCustomNetwork {

  public static void main(String[] args)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";
    // Name of the region you want to use to run the job. Regions that are
    // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
    String region = "europe-central2";
    // The name of the job that will be created.
    // It needs to be unique for each project and region pair.
    String jobName = "JOB_NAME";
    // The name of a VPC network in the current project or a Shared VPC network that is hosted by
    // or shared with the current project.

    String network = String.format("global/networks/%s", "test-network");
    // The name of a subnet that is part of the VPC network and is located
    // in the same region as the VMs for the job.
    String subnet = String.format("regions/%s/subnetworks/%s", region, "subnet");

    createBatchCustomNetwork(projectId, region, jobName, network, subnet);
  }

  // Create a job that runs on a specific network.
  public static Job createBatchCustomNetwork(String projectId, String region, String jobName,
                                             String network,  String subnet)
      throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
      // Define what will be done as part of the job.
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(
                          "echo Hello world! This is task ${BATCH_TASK_INDEX}. "
                              + "This job has a total of ${BATCH_TASK_COUNT} tasks.")
                      // You can also run a script from a file. Just remember, that needs to be a
                      // script that's already on the VM that will be running the job.
                      // Using setText() and setPath() is mutually exclusive.
                      // .setPath("/tmp/test.sh")
                      .build())
              .build();

      TaskSpec task = TaskSpec.newBuilder()
          // Jobs can be divided into tasks. In this case, we have only one task.
          .addRunnables(runnable)
          .setMaxRetryCount(2)
          .setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
          .build();

      // Tasks are grouped inside a job using TaskGroups.
      // Currently, it's possible to have only one task group.
      TaskGroup taskGroup = TaskGroup.newBuilder()
          .setTaskCount(3)
          .setParallelism(1)
          .setTaskSpec(task)
          .build();

      // Specifies a VPC network and a subnet for Allocation Policy
      AllocationPolicy.NetworkPolicy networkPolicy =
          AllocationPolicy.NetworkPolicy.newBuilder()
              .addNetworkInterfaces(AllocationPolicy.NetworkInterface.newBuilder()
                  .setNetwork(network) // Set the network name
                  .setSubnetwork(subnet) // Set the subnet name
                  .setNoExternalIpAddress(true) // Blocks external access for all VMs
                  .build())
              .build();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      // In this case, we tell the system to use "e2-standard-4" machine type.
      // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
      AllocationPolicy.InstancePolicy instancePolicy =
          AllocationPolicy.InstancePolicy.newBuilder().setMachineType("e2-standard-4")
              .build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(AllocationPolicy.InstancePolicyOrTemplate.newBuilder()
              .setPolicy(instancePolicy).build())
              .setNetwork(networkPolicy)
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING))
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run for the specific project.
              .setParent(String.format("projects/%s/locations/%s", projectId, region))
              .setJob(job)
              .setJobId(jobName)
              .build();

      Job result =
          batchServiceClient
              .createJobCallable()
              .futureCall(createJobRequest)
              .get(5, TimeUnit.MINUTES);

      System.out.printf("Successfully created the job: %s", result.getName());

      return result;
    }
  }
}

Node.js

// Imports the Batch library
const batchLib = require('@google-cloud/batch');
const batch = batchLib.protos.google.cloud.batch.v1;

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

/**
 * TODO(developer): Update these variables before running the sample.
 */
// Project ID or project number of the Google Cloud project you want to use.
const projectId = await batchClient.getProjectId();
// Name of the region you want to use to run the job. Regions that are
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
const region = 'europe-central2';
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'example-job';
// The name of a VPC network in the current project or a Shared VPC network that is hosted by
// or shared with the current project.
const network = 'global/networks/test-network';
// The name of a subnetwork that is part of the VPC network and is located
// in the same region as the VMs for the job.
const subnetwork = `regions/${region}/subnetworks/subnet`;

// Define what will be done as part of the job.
const runnable = new batch.Runnable({
  script: new batch.Runnable.Script({
    commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
  }),
});

// Specify what resources are requested by each task.
const computeResource = new batch.ComputeResource({
  // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
  cpuMilli: 500,
  // In MiB.
  memoryMib: 16,
});

const task = new batch.TaskSpec({
  runnables: [runnable],
  computeResource,
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: task,
});

// Specify VPC network and a subnet for Allocation Policy
const networkPolicy = new batch.AllocationPolicy.NetworkPolicy({
  networkInterfaces: [
    new batch.AllocationPolicy.NetworkInterface({
      // Set the network name
      network,
      // Set the subnetwork name
      subnetwork,
      // Blocks external access for all VMs
      noExternalIpAddress: true,
    }),
  ],
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType: 'e2-standard-4',
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  policy: instancePolicy,
  network: networkPolicy,
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  labels: {env: 'testing', type: 'script'},
  allocationPolicy,
  // We use Cloud Logging as it's an option available out of the box
  logsPolicy: new batch.LogsPolicy({
    destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
  }),
});

// The job's parent is the project and region in which the job will run
const parent = `projects/${projectId}/locations/${region}`;

async function callCreateBatchCustomNetwork() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

  // Run request
  const [response] = await batchClient.createJob(request);
  console.log(JSON.stringify(response));
}

await callCreateBatchCustomNetwork();

Python

from google.cloud import batch_v1


def create_with_custom_network(
    project_id: str,
    region: str,
    network_name: str,
    subnet_name: str,
    job_name: str,
) -> batch_v1.Job:
    """Create a Batch job that runs on a specific network and subnet.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use to run the job. Regions that are
            available for Batch are listed on: https://cloud.google.com/batch/docs/locations
        network_name: The name of a VPC network in the current project or a Shared VPC network.
        subnet_name: Name of the subnetwork to be used within the specified region.
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
    Returns:
        A job object representing the job created.
    """

    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."

    # Jobs can be divided into tasks. In this case, we have only one task.
    task = batch_v1.TaskSpec()
    task.runnables = [runnable]

    # We can specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 2000  # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
    resources.memory_mib = 16  # in MiB
    task.compute_resource = resources

    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    # Tasks are grouped inside a job using TaskGroups.
    # Currently, it's possible to have only one task group.
    group = batch_v1.TaskGroup()
    group.task_count = 3
    group.task_spec = task

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # In this case, we tell the system to use "e2-standard-4" machine type.
    # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "e2-standard-4"
    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    # Create a NetworkInterface object to specify network settings for the job
    network_interface = batch_v1.AllocationPolicy.NetworkInterface()
    # Set the network to the specified network name within the project
    network_interface.network = f"projects/{project_id}/global/networks/{network_name}"
    # Set the subnetwork to the specified subnetwork within the region
    network_interface.subnetwork = (
        f"projects/{project_id}/regions/{region}/subnetworks/{subnet_name}"
    )
    allocation_policy.network.network_interfaces = [network_interface]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    # We use Cloud Logging as it's an out of the box available option
    job.logs_policy = batch_v1.LogsPolicy()
    job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING

    create_request = batch_v1.CreateJobRequest()
    create_request.job = job
    create_request.job_id = job_name
    # The job's parent is the region in which the job will run
    create_request.parent = f"projects/{project_id}/locations/{region}"
    return client.create_job(create_request)

API

כדי ליצור משימה באמצעות Batch API, משתמשים בשיטה jobs.create ומציינים את פרטי ההגדרה של המשימה. כדי לציין את הרשת לעבודה, צריך לכלול את השדות network ו-subnetwork.

לדוגמה, כדי ליצור משימת סקריפט בסיסית שפועלת ברשת ספציפית, שולחים את בקשת POST הבאה:

POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME

{
  "taskGroups": [
    {
      "taskSpec": {
        "runnables": [
          {
            "script": {
              "text": "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
            }
          }
        ]
      },
      "taskCount": 3
    }
  ],
  "allocationPolicy": {
    "network": {
      "networkInterfaces": [
        {
          "network": "projects/HOST_PROJECT_ID/global/networks/NETWORK",
          "subnetwork": "projects/HOST_PROJECT_ID/regions/REGION/subnetworks/SUBNET"
        }
      ]
    }
  },
  "logsPolicy": {
    "destination": "CLOUD_LOGGING"
  }
}

מחליפים את מה שכתוב בשדות הבאים:

  • PROJECT_ID: מזהה הפרויקט שלכם.
  • LOCATION: המיקום של המשרה.
  • JOB_NAME: השם של המשימה.
  • HOST_PROJECT_ID: מזהה הפרויקט של הפרויקט שבו נמצאת הרשת שציינתם:
    • אם אתם משתמשים ברשת VPC משותפת, צריך לציין את הפרויקט המארח.
    • אחרת, מציינים את הפרויקט הנוכחי (PROJECT_ID).
  • NETWORK: השם של רשת VPC בפרויקט הנוכחי או של רשת VPC משותפת שמארחת את הפרויקט הנוכחי או שמשותפת איתו.
  • REGION: האזור שבו נמצאים תת-הרשת והמכונות הווירטואליות של העבודה:
    • אם כוללים את השדה allowedLocations כדי לציין את המיקום המותר של המכונות הווירטואליות עבור העבודה, צריך לציין כאן את אותו אזור.
    • אחרת, האזור צריך להיות זהה למיקום שבוחרים למשרה (LOCATION).
  • SUBNET: השם של תת-רשת שמהווה חלק מרשת ה-VPC ונמצאת באותו אזור כמו המכונות הווירטואליות של העבודה.

המאמרים הבאים