This document explains the basics for Batch job creation: how to create and run a job that is based on a script or container image and use predefined and custom variables. To learn more about creating and running jobs, see Job creation and execution overview.
Before you begin
- If you haven't used Batch before, review Get started with Batch and enable Batch by completing the prerequisites for projects and users.
-
To get the permissions that you need to create a job, ask your administrator to grant you the following IAM roles:
- Batch Job Editor (
roles/batch.jobsEditor) on the project - Service Account User (
roles/iam.serviceAccountUser) on the job's service account, which by default is the default Compute Engine service account
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
- Batch Job Editor (
-
Each time you create a job, make sure the job has a valid resource
configuration.
- Unless the default values are sufficient, you must specify the compute
resources per task (
computeResource) fields required for each task to run, as shown in this document. - If you specify any VM resources for a job (any
instances[].policy,instances[].instanceTemplate, orinstanceFlexibilityPolicyfield), you must make sure that these fields are compatible with each other and the job.
- Unless the default values are sufficient, you must specify the compute
resources per task (
-
Each time you create a job, make sure the job has a valid network configuration.
- If you don't have any specific networking requirements for your workload or project and you haven't modified the default network for your project, no action is required.
- Otherwise, you need to configure networking when you create a job. Learn how to configure networking for a job before you create a basic job, so you can modify the examples below to meet your networking requirements.
-
Each time you create a job, make sure the job has a valid VM
operating system (OS) environment.
- If you don't have any specific VM OS image or boot disk requirements for your workload or project, no action is required.
- Otherwise, you need to prepare a valid VM OS environment option. Before creating a basic job, either allow the default configuration for the VM OS environment or learn how to customize the VM OS environment so you can modify the examples below to meet your requirements.
Create a basic job
For information about all the fields you can specify for a job, see the
reference documentation for the
projects.locations.jobs REST resource.
To summarize, a job consists of an array of one or more tasks
that all run one or more
runnables,
which are the executable script(s) and/or container(s) for your job.
To cover the basics, this section explains how to create an example job with
just one runnable, either a script or a container image:
- If you want to use Batch to write jobs that run a container image, see create a container job.
- Otherwise, if you aren't sure if you want to use container images or if you are unfamiliar with containers, creating a script job is recommended.
The example job for both types of jobs contains a task group with an array of 4 tasks. Each task prints a message and its index to the standard output and Cloud Logging. The definition for this job specifies a parallelism of 2, which indicates that the job should run on 2 VMs to allow 2 tasks to run at a time.
Create a basic container job
You can select or create a container image to provide the code and dependencies for your job to run from any compute environment. For more information, see Working with container images and Running containers on VM instances.
You can create a basic container job using the Google Cloud console, gcloud CLI, Batch API, Go, Java, Node.js, Python, or C++.
Console
To create a basic container job using the Google Cloud console, do the following:
In the Google Cloud console, go to the Job list page.
Click Create. The Create batch job page opens. In the left pane, the Job details page is selected.
Configure the Job details page:
Optional: In the Job name field, customize the job name.
For example, enter
example-basic-job.Configure the Task details section:
In the New runnable window, add at least one script or container for this job to run.
For example, to add one container, do the following:
Select Container image URL (default).
In the Container image URL field, enter the URL for a container image that you want to run for each task in this job.
For example, to use the
busyboxDocker container image, enter the following URL:gcr.io/google-containers/busyboxOptional: To override the container image's
ENTRYPOINTcommand, enter a command in the Entry point field.For example, enter the following:
/bin/shOptional: To override the container image's
CMDcommand, do the following:Select the Override container image's CMD command checkbox. A field appears.
In the field, enter one or more commands, separating each command with a new line.
For example, enter the following commands:
-c echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.Click Done.
In the Task count field, enter the number of tasks for this job. The value must be a whole number between
1and the tasks per task group limit.For example, enter
4.In the Parallelism field, enter the number of tasks to run concurrently. The number cannot be larger than the total number of tasks and must be a whole number between
1and the parallel tasks per job limit.For example, enter
2.
Configure the Resource specifications page:
In the left pane, click Resource specifications. The Resource specifications page opens.
In the VM provisioning model section, select one of the following options for the provisioning model for this job's VMs:
If your job can withstand preemption and you want discounted VMs, select Spot. Learn more about recommendations for Spot VMs.
Otherwise, select Standard.
For example, select Standard (default).
Select the location for this job:
In the Region field, select a region.
For example, select
us-central1 (Iowa)(default).In the Zone field, do one of the following:
If you want to restrict this job to run in a specific zone only, select a zone.
Otherwise, select any.
For example, select any (default).
Select one of the following machine families:
For common workloads, click General purpose.
For performance-intensive workloads, click Compute optimized.
For memory-intensive workloads, click Memory optimized.
For accelerator-optimized workloads, click GPUs. For more information, see Create and run a job that uses GPUs.
For example, click General purpose (default).
In the Series field, select a machine series for this job's VMs.
For example, if you selected General purpose for the machine family, select E2 (default).
In the Machine type field, select a machine type for this job's VMs.
For example, if you selected E2 for the machine series, select e2-medium (2 vCPU, 4 GB memory) (default).
Configure the amount of VM resources required for each task:
In the Cores field, enter the amount of vCPUs per task.
For example, enter
1(default).In the Memory field, enter the amount of RAM in GB per task.
For example, enter
0.5(default).
Optional: To review the job configuration, in the left pane, click Preview.
Click Create.
The Job details page displays the job that you created.
gcloud
To create a basic container job using the gcloud CLI, do the following:
Create a JSON file that specifies your job's configuration details. For example, to create a basic container job, create a JSON file with the following contents. For more information about all the fields you can specify for a job, see the reference documentation for the
projects.locations.jobsREST resource.{ "taskGroups": [ { "taskSpec": { "runnables": [ { "container": { CONTAINER } } ], "computeResource": { "cpuMilli": CORES, "memoryMib": MEMORY }, "maxRetryCount": MAX_RETRY_COUNT, "maxRunDuration": "MAX_RUN_DURATION" }, "taskCount": TASK_COUNT, "parallelism": PARALLELISM } ] }Replace the following:
CONTAINER: the container that each task runs. At minimum, a container must specify an image in theimageUrisubfield, but additional subfields might also be required. For more information, see thecontainersubfields and the example container job in this section.CORES: Optional. The amount of cores—specifically vCPUs, which usually represent half a physical core—to allocate for each task in milliCPU units. If thecpuMillifield is not specified, the value is set to2000(2 vCPUs).MEMORY: Optional. The amount of memory to allocate for each task in MB. If thememoryMibfield is not specified, the value is set to2000(2 GB).MAX_RETRY_COUNT: Optional. The maximum number of retries for a task. The value must be a whole number between0and10. If themaxRetryCountfield is not specified, the value is set to0, which means to not retry the task. For more information about themaxRetryCountfield, see Automate task retries.MAX_RUN_DURATION: Optional. The maximum time a task is allowed to run before being retried or failing, formatted as a value in seconds followed bys—for example,3600sfor 1 hour. If themaxRunDurationfield is not specified, the value is set to the maximum run time for a job. For more information about themaxRunDurationfield, see Limit run times for tasks and runnables using timeouts.TASK_COUNT: Optional. The number of tasks for the job. The value must be a whole number between1and the tasks per task group limit. If thetaskCountfield is not specified, the value is set to1.PARALLELISM: Optional. The number of tasks the job runs concurrently. The number cannot be larger than the number of tasks and must be a whole number between1and the parallel tasks per job limit. If theparallelismfield is not specified, the value is set to1.
Create a job by using the
gcloud batch jobs submitcommand.gcloud batch jobs submit JOB_NAME \ --location LOCATION \ --config JSON_CONFIGURATION_FILEReplace the following:
JOB_NAME: the name of the job.LOCATION: the location of the job.JSON_CONFIGURATION_FILE: the path for a JSON file with the job's configuration details.
For example, to create a job that runs tasks using the
busybox Docker container image:
Create a JSON file in the current directory named
hello-world-container.jsonwith the following contents:{ "taskGroups": [ { "taskSpec": { "runnables": [ { "container": { "imageUri": "gcr.io/google-containers/busybox", "entrypoint": "/bin/sh", "commands": [ "-c", "echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks." ] } } ], "computeResource": { "cpuMilli": 2000, "memoryMib": 16 }, "maxRetryCount": 2, "maxRunDuration": "3600s" }, "taskCount": 4, "parallelism": 2 } ], "allocationPolicy": { "instances": [ { "policy": { "machineType": "e2-standard-4" } } ] }, "labels": { "department": "finance", "env": "testing" }, "logsPolicy": { "destination": "CLOUD_LOGGING" } }Run the following command:
gcloud batch jobs submit example-container-job \ --location us-central1 \ --config hello-world-container.json
API
To create a basic container job using the Batch API, use
the jobs.create method.
For more information about all the fields you can specify for a job, see the
reference documentation for the projects.locations.jobs REST resource.
POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
CONTAINER
}
}
],
"computeResource": {
"cpuMilli": CORES,
"memoryMib": MEMORY
},
"maxRetryCount": MAX_RETRY_COUNT,
"maxRunDuration": "MAX_RUN_DURATION"
},
"taskCount": TASK_COUNT,
"parallelism": PARALLELISM
}
]
}
Replace the following:
PROJECT_ID: the project ID of your project.LOCATION: the location of the job.JOB_NAME: the name of the job.CONTAINER: the container that each task runs. At minimum, a container must specify an image in theimageUrisubfield, but additional subfields might also be required. For more information, see thecontainersubfields and the example container job in this section.CORES: Optional. The amount of cores—specifically vCPUs, which usually represent half a physical core—to allocate for each task in milliCPU units. If thecpuMillifield is not specified, the value is set to2000(2 vCPUs).MEMORY: Optional. The amount of memory to allocate for each task in MB. If thememoryMibfield is not specified, the value is set to2000(2 GB).MAX_RETRY_COUNT: Optional. The maximum number of retries for a task. The value must be a whole number between0and10. If themaxRetryCountfield is not specified, the value is set to0, which means to not retry the task. For more information about themaxRetryCountfield, see Automate task retries.MAX_RUN_DURATION: Optional. The maximum time a task is allowed to run before being retried or failing, formatted as a value in seconds followed bys—for example,3600sfor 1 hour. If themaxRunDurationfield is not specified, the value is set to the maximum run time for a job. For more information about themaxRunDurationfield, see Limit run times for tasks and runnables using timeouts.TASK_COUNT: Optional. The number of tasks for the job, which must be a whole number between1and the tasks per task group limit. If thetaskCountfield is not specified, the value is set to1.PARALLELISM: Optional. The number of tasks the job runs concurrently. The number cannot be larger than the number of tasks and must be a whole number between1and the parallel tasks per job limit. If theparallelismfield is not specified, the value is set to1.
For example, to create a job that runs tasks using the
busybox Docker container image,
use the following request:
POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/jobs?job_id=example-container-job
{
"taskGroups": [
{
"taskSpec": {
"runnables": [
{
"container": {
"imageUri": "gcr.io/google-containers/busybox",
"entrypoint": "/bin/sh",
"commands": [
"-c",
"echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks."
]
}
}
],
"computeResource": {
"cpuMilli": 2000,
"memoryMib": 16
},
"maxRetryCount": 2,
"maxRunDuration": "3600s"
},
"taskCount": 4,
"parallelism": 2
}
],
"allocationPolicy": {
"instances": [
{
"policy": { "machineType": "e2-standard-4" }
}
]
},
"labels": {
"department": "finance",
"env": "testing"
},
"logsPolicy": {
"destination": "CLOUD_LOGGING"
}
}
where PROJECT_ID is the
project ID
of your project.
Go
Go
For more information, see the Batch Go API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Java
Java
For more information, see the Batch Java API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Node.js
Node.js
For more information, see the Batch Node.js API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.
Python
Python
For more information, see the Batch Python API reference documentation.
To authenticate to Batch, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.