Créer et exécuter une tâche qui utilise des volumes de stockage

Ce document explique comment créer et exécuter un job par lot qui utilise un ou plusieurs volumes de stockage externes. Les options de stockage externe incluent un disque persistant nouveau ou existant, de nouveaux SSD locaux, des buckets Cloud Storage existants et un système de fichiers réseau (NFS) existant, tel qu'un partage de fichiers Filestore.

Que vous ajoutiez ou non des volumes de stockage externes, chaque VM Compute Engine d'un job dispose d'un disque de démarrage, qui fournit l'espace de stockage pour l'image et les instructions du système d'exploitation (OS) du job. Pour en savoir plus sur la configuration du disque de démarrage d'une tâche, consultez plutôt la section Présentation de l'environnement d'OS de VM.

Avant de commencer

  1. Si vous n'avez jamais utilisé Batch, consultez Premiers pas avec Batch et activez Batch en remplissant les conditions préalables pour les projets et les utilisateurs.
  2. Pour obtenir les autorisations nécessaires pour créer un job, demandez à votre administrateur de vous accorder les rôles IAM suivants:

    Pour en savoir plus sur l'attribution de rôles, consultez la page Gérer l'accès aux projets, aux dossiers et aux organisations.

    Vous pouvez également obtenir les autorisations requises via des rôles personnalisés ou d'autres rôles prédéfinis.

Créer une tâche qui utilise des volumes de stockage

Une tâche peut éventuellement utiliser un ou plusieurs des types de volumes de stockage externes suivants. Pour en savoir plus sur tous les types de volumes de stockage, ainsi que sur les différences et les restrictions associées à chacun d'eux, consultez la documentation sur les options de stockage des VM Compute Engine.

Vous pouvez autoriser une tâche à utiliser chaque volume de stockage en l'incluant dans la définition de votre tâche et en spécifiant son chemin d'installation (mountPath) dans vos exécutables. Pour savoir comment créer un job qui utilise des volumes de stockage, consultez une ou plusieurs des sections suivantes:

Utiliser un disque persistant

Une tâche qui utilise des disques persistants présente les restrictions suivantes:

  • Tous les disques persistants: consultez les restrictions applicables à tous les disques persistants.

  • Nouveaux disques persistants par rapport aux disques persistants existants: chaque disque persistant d'une tâche peut être nouveau (défini et créé avec la tâche) ou existant (déjà créé dans votre projet et spécifié dans la tâche). Pour utiliser un disque persistant, vous devez le formater et l'installer sur les VM de la tâche, qui doivent se trouver au même emplacement que le disque persistant. Le montage par lot associe tous les disques persistants que vous incluez dans une tâche et formate tous les nouveaux disques persistants. Toutefois, vous devez formater et dissocier tous les disques persistants existants que vous souhaitez qu'une tâche utilise.

    Les options de localisation, les options de format et les options de montage acceptées varient selon que les disques persistants sont nouveaux ou existants, comme décrit dans le tableau suivant:

    Nouveaux disques persistants Disques persistants existants
    Options de mise en forme

    Le disque persistant est formaté automatiquement avec un système de fichiers ext4.

    Vous devez formater le disque persistant pour utiliser un système de fichiers ext4 avant de l'utiliser pour une tâche.

    Options d'installation

    Toutes les options sont acceptées.

    Toutes les options, à l'exception de l'écriture sont acceptées. Cela est dû aux restrictions du mode écriture simultanée.

    Vous devez dissocier le disque persistant de toutes les VM auxquelles il est associé avant de l'utiliser pour une tâche.

    Options d'emplacement

    Vous ne pouvez créer que des disques persistants zonaux.

    Vous pouvez sélectionner n'importe quel lieu pour votre tâche. Les disques persistants sont créés dans la zone dans laquelle votre projet s'exécute.

    Vous pouvez sélectionner des disques persistants zonaux et régionaux.


    Vous devez définir l'emplacement de la tâche (ou, le cas échéant, uniquement ses emplacements autorisés) sur seuls les emplacements contenant tous les disques persistants de la tâche. Par exemple, pour un disque persistant zonal, l'emplacement de la tâche doit être la zone du disque. Pour un disque persistant régional, l'emplacement de la tâche doit être la région du disque ou, si vous spécifiez des zones, l'une ou les deux zones spécifiques dans lesquelles se trouve le disque persistant régional.

  • Modèles d'instance: si vous souhaitez utiliser un modèle d'instance de VM lors de la création de cette tâche, vous devez associer tous les disques persistants de cette tâche dans le modèle d'instance. Sinon, si vous ne souhaitez pas utiliser de modèle d'instance, vous devez associer les disques persistants directement dans la définition de la tâche.

Vous pouvez créer un job qui utilise un disque persistant à l'aide de la console Google Cloud, de la Google Cloud CLI, de l'API Batch, de C++, de Go, de Java, de Node.js ou de Python.

Console

À l'aide de la console Google Cloud, l'exemple suivant crée une tâche qui exécute un script pour lire un fichier à partir d'un disque persistant zonal existant situé dans la zone us-central1-a. L'exemple de script suppose que la tâche dispose d'un disque persistant zonal existant contenant un fichier texte nommé example.txt dans le répertoire racine.

Facultatif: Créer un exemple de disque persistant zonal

Si vous souhaitez créer un disque persistant zonal que vous pourrez utiliser pour exécuter l'exemple de script, procédez comme suit avant de créer votre tâche:

  1. Associez un nouveau disque persistant vide nommé example-disk à une VM Linux dans la zone us-central1-a, puis exécutez des commandes sur la VM pour formater et installer le disque. Pour obtenir des instructions, consultez la section Ajouter un disque persistant à votre VM.

    Ne vous déconnectez pas encore de la VM.

  2. Pour créer example.txt sur le disque persistant, exécutez les commandes suivantes sur la VM:

    1. Pour remplacer le répertoire de travail actuel par le répertoire racine du disque persistant, saisissez la commande suivante:

      cd VM_MOUNT_PATH
      

      Remplacez VM_MOUNT_PATH par le chemin d'accès au répertoire dans lequel le disque persistant a été installé sur cette VM à l'étape précédente (par exemple, /mnt/disks/example-disk).

    2. Appuyez sur la touche Enter.

    3. Pour créer et définir un fichier nommé example.txt, saisissez la commande suivante:

      cat > example.txt
      
    4. Appuyez sur la touche Enter.

    5. Saisissez le contenu du fichier. Par exemple, saisissez Hello world!.

    6. Pour enregistrer le fichier, appuyez sur Ctrl+D (ou Command+D sous macOS).

    Lorsque vous avez terminé, vous pouvez vous déconnecter de la VM.

  3. Dissociez le disque persistant de la VM.

    • Si vous n'avez plus besoin de la VM, vous pouvez la supprimer, ce qui dissocie automatiquement le disque persistant.

    • Sinon, dissociez le disque persistant. Pour obtenir des instructions, consultez la section Dissocier et réassocier des disques de démarrage et dissociez le disque persistant example-disk au lieu du disque de démarrage de la VM.

Créer un job qui utilise le disque persistant zonal existant

Pour créer une tâche qui utilise des disques persistants zonaux existants à l'aide de la console Google Cloud, procédez comme suit:

  1. Dans la console Google Cloud, accédez à la page Liste des jobs.

    Accéder à la liste des tâches

  2. Cliquez sur Créer. La page Créer une tâche par lot s'ouvre. Dans le volet de gauche, la page Informations sur la tâche est sélectionnée.

  3. Configurez la page Informations sur la tâche:

    1. (Facultatif) Dans le champ Nom de la tâche, personnalisez le nom de la tâche.

      Par exemple, saisissez example-disk-job.

    2. Configurez la section Détails de la tâche:

      1. Dans la fenêtre Nouvel exécutable, ajoutez au moins un script ou un conteneur pour que cette tâche s'exécute.

        Par exemple, pour exécuter un script qui imprime le contenu d'un fichier nommé example.txt et situé dans le répertoire racine du disque persistant utilisé par cette tâche, procédez comme suit:

        1. Cochez la case Script. Une zone de texte s'affiche.

        2. Dans la zone de texte, saisissez le script suivant:

          echo "Here is the content of the example.txt file in the persistent disk."
          cat MOUNT_PATH/example.txt
          

          Remplacez MOUNT_PATH par le chemin d'accès à l'emplacement où vous prévoyez de monter le disque persistant sur les VM pour cette tâche (par exemple, /mnt/disks/example-disk).

        3. Cliquez sur OK.

      2. Dans le champ Nombre de tâches, saisissez le nombre de tâches pour cette tâche.

        Par exemple, saisissez 1 (valeur par défaut).

      3. Dans le champ Parallélisme, saisissez le nombre de tâches à exécuter simultanément.

        Par exemple, saisissez 1 (valeur par défaut).

  4. Configurez la page Spécifications de la ressource:

    1. Dans le volet de gauche, cliquez sur Spécifications des ressources. La page Spécifications des ressources s'ouvre.

    2. Sélectionnez l'emplacement de cette tâche. Pour utiliser un disque persistant zonal existant, les VM d'une tâche doivent se trouver dans la même zone.

      1. Dans le champ Région, sélectionnez une région.

        Par exemple, pour utiliser l'exemple de disque persistant zonal, sélectionnez us-central1 (Iowa) (par défaut).

      2. Dans le champ Zone, sélectionnez une zone.

        Par exemple, sélectionnez us-central1-a (Iowa).

  5. Configurez la page Configurations supplémentaires:

    1. Dans le volet de gauche, cliquez sur Configurations supplémentaires. La page Configurations supplémentaires s'ouvre.

    2. Pour chaque disque persistant zonal existant que vous souhaitez installer sur cette tâche, procédez comme suit:

      1. Dans la section Volume de stockage, cliquez sur Ajouter un volume. La fenêtre New volume (Nouveau volume) s'affiche.

      2. Dans la fenêtre Nouveau volume, procédez comme suit:

        1. Dans la section Type de volume, sélectionnez Disque persistant (par défaut).

        2. Dans la liste Disque, sélectionnez un disque persistant zonal existant que vous souhaitez monter à cette tâche. Le disque doit se trouver dans la même zone que cette tâche.

          Par exemple, sélectionnez le disque persistant zonal existant que vous avez préparé, qui se trouve dans la zone us-central1-a et contient le fichier example.txt.

        3. Facultatif: Si vous souhaitez renommer ce disque zonal persistant, procédez comme suit:

          1. Sélectionnez Personnaliser le nom de l'appareil.

          2. Dans le champ Nom de l'appareil, saisissez le nouveau nom de votre disque.

        4. Dans le champ Chemin d'installation, saisissez le chemin d'installation (MOUNT_PATH) de ce disque persistant:

          Par exemple, saisissez les éléments suivants:

          /mnt/disks/EXISTING_PERSISTENT_DISK_NAME
          

          Remplacez EXISTING_PERSISTENT_DISK_NAME par le nom du disque. Si vous avez renommé le disque persistant zonal, utilisez le nouveau nom.

          Par exemple, remplacez EXISTING_PERSISTENT_DISK_NAME par example-disk.

        5. Cliquez sur OK.

  6. Facultatif: Configurez les autres champs de cette tâche.

  7. Facultatif: Pour examiner la configuration de la tâche, dans le volet de gauche, cliquez sur Aperçu.

  8. Cliquez sur Créer.

La page Détails de la tâche affiche la tâche que vous avez créée.

gcloud

À l'aide de la CLI gcloud, l'exemple suivant crée une tâche qui associe et installe un disque persistant existant et un nouveau disque persistant. La tâche comporte trois tâches qui exécutent chacune un script pour créer un fichier dans le nouveau disque persistant nommé output_task_TASK_INDEX.txt, où TASK_INDEX est l'index de chaque tâche: 0, 1 et 2.

Pour créer une tâche qui utilise des disques persistants à l'aide de gcloud CLI, utilisez la commande gcloud batch jobs submit. Dans le fichier de configuration JSON de la tâche, spécifiez les disques persistants dans le champ instances et installez le disque persistant dans le champ volumes.

  1. Créez un fichier JSON.

    • Si vous n'utilisez pas de modèle d'instance pour cette tâche, créez un fichier JSON contenant les éléments suivants:

      {
          "allocationPolicy": {
              "instances": [
                  {
                      "policy": {
                          "disks": [
                              {
                                  "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                                  "existingDisk": "projects/PROJECT_ID/EXISTING_PERSISTENT_DISK_LOCATION/disks/EXISTING_PERSISTENT_DISK_NAME"
                              },
                              {
                                  "newDisk": {
                                      "sizeGb": NEW_PERSISTENT_DISK_SIZE,
                                      "type": "NEW_PERSISTENT_DISK_TYPE"
                                  },
                                  "deviceName": "NEW_PERSISTENT_DISK_NAME"
                              }
                          ]
                      }
                  }
              ],
              "location": {
                  "allowedLocations": [
                      "EXISTING_PERSISTENT_DISK_LOCATION"
                  ]
              }
          },
          "taskGroups": [
              {
                  "taskSpec": {
                      "runnables": [
                          {
                              "script": {
                                  "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                              }
                          }
                      ],
                      "volumes": [
                          {
                              "deviceName": "NEW_PERSISTENT_DISK_NAME",
                              "mountPath": "/mnt/disks/NEW_PERSISTENT_DISK_NAME",
                              "mountOptions": "rw,async"
                          },
                          {
      
                              "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                              "mountPath": "/mnt/disks/EXISTING_PERSISTENT_DISK_NAME"
                          }
                      ]
                  },
                  "taskCount":3
              }
          ],
          "logsPolicy": {
              "destination": "CLOUD_LOGGING"
          }
      }
      

      Remplacez les éléments suivants :

      • PROJECT_ID: ID de projet de votre projet.
      • EXISTING_PERSISTENT_DISK_NAME: nom d'un disque persistant existant.
      • EXISTING_PERSISTENT_DISK_LOCATION: emplacement d'un disque persistant existant. Pour chaque disque persistant zonal existant, l'emplacement de la tâche doit être la zone du disque. Pour chaque disque persistant régional existant, l'emplacement de la tâche doit être la région du disque ou, si vous spécifiez des zones, l'une ou les deux zones spécifiques dans lesquelles se trouve le disque persistant régional. Si vous ne spécifiez aucun disque persistant existant, vous pouvez sélectionner n'importe quel emplacement. En savoir plus sur le champ allowedLocations
      • NEW_PERSISTENT_DISK_SIZE: taille du nouveau disque persistant en Go. Les tailles autorisées dépendent du type de disque persistant, mais la taille minimale est souvent de 10 Go (10) et la taille maximale est souvent de 64 To (64000).
      • NEW_PERSISTENT_DISK_TYPE: type de disque du nouveau disque persistant, pd-standard, pd-balanced, pd-ssd ou pd-extreme. Le type de disque par défaut pour les disques persistants non de démarrage est pd-standard.
      • NEW_PERSISTENT_DISK_NAME: nom du nouveau disque persistant.
    • Si vous utilisez un modèle d'instance de VM pour cette tâche, créez un fichier JSON comme indiqué précédemment, mais remplacez le champ instances par le code suivant:

      "instances": [
          {
              "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
          }
      ],
      

      INSTANCE_TEMPLATE_NAME est le nom du modèle d'instance de cette tâche. Pour une tâche qui utilise des disques persistants, ce modèle d'instance doit définir et associer les disques persistants que vous souhaitez que la tâche utilise. Pour cet exemple, le modèle doit définir et associer un nouveau disque persistant nommé NEW_PERSISTENT_DISK_NAME et un disque persistant existant nommé EXISTING_PERSISTENT_DISK_NAME.

  2. Exécutez la commande suivante :

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

    Remplacez les éléments suivants :

    • JOB_NAME: nom de la tâche.

    • LOCATION: emplacement de la tâche.

    • JSON_CONFIGURATION_FILE: chemin d'accès à un fichier JSON contenant les détails de configuration de la tâche.

API

À l'aide de l'API Batch, l'exemple suivant crée une tâche qui associe et installe un disque persistant existant et un nouveau disque persistant. La tâche comporte trois tâches qui exécutent chacune un script pour créer un fichier dans le nouveau disque persistant nommé output_task_TASK_INDEX.txt, où TASK_INDEX est l'index de chaque tâche: 0, 1 et 2.

Pour créer une tâche qui utilise des disques persistants à l'aide de l'API Batch, utilisez la méthode jobs.create. Dans la requête, spécifiez les disques persistants dans le champ instances et installez le disque persistant dans le champ volumes.

  • Si vous n'utilisez pas de modèle d'instance pour cette tâche, envoyez la requête suivante:

    POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
    
    {
        "allocationPolicy": {
            "instances": [
                {
                    "policy": {
                        "disks": [
                            {
                                "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                                "existingDisk": "projects/PROJECT_ID/EXISTING_PERSISTENT_DISK_LOCATION/disks/EXISTING_PERSISTENT_DISK_NAME"
                            },
                            {
                                "newDisk": {
                                    "sizeGb": NEW_PERSISTENT_DISK_SIZE,
                                    "type": "NEW_PERSISTENT_DISK_TYPE"
                                },
                                "deviceName": "NEW_PERSISTENT_DISK_NAME"
                            }
                        ]
                    }
                }
            ],
            "location": {
                "allowedLocations": [
                    "EXISTING_PERSISTENT_DISK_LOCATION"
                ]
            }
        },
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "deviceName": "NEW_PERSISTENT_DISK_NAME",
                            "mountPath": "/mnt/disks/NEW_PERSISTENT_DISK_NAME",
                            "mountOptions": "rw,async"
                        },
                        {
    
                            "deviceName": "EXISTING_PERSISTENT_DISK_NAME",
                            "mountPath": "/mnt/disks/EXISTING_PERSISTENT_DISK_NAME"
                        }
                    ]
                },
                "taskCount":3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Remplacez les éléments suivants :

    • PROJECT_ID: ID de projet de votre projet.
    • LOCATION: emplacement de la tâche.
    • JOB_NAME: nom de la tâche.
    • EXISTING_PERSISTENT_DISK_NAME: nom d'un disque persistant existant.
    • EXISTING_PERSISTENT_DISK_LOCATION: emplacement d'un disque persistant existant. Pour chaque disque persistant zonal existant, l'emplacement de la tâche doit être la zone du disque. Pour chaque disque persistant régional existant, l'emplacement de la tâche doit être la région du disque ou, si vous spécifiez des zones, l'une ou les deux zones spécifiques dans lesquelles se trouve le disque persistant régional. Si vous ne spécifiez aucun disque persistant existant, vous pouvez sélectionner n'importe quel emplacement. En savoir plus sur le champ allowedLocations
    • NEW_PERSISTENT_DISK_SIZE: taille du nouveau disque persistant en Go. Les tailles autorisées dépendent du type de disque persistant, mais la taille minimale est souvent de 10 Go (10) et la taille maximale est souvent de 64 To (64000).
    • NEW_PERSISTENT_DISK_TYPE: type de disque du nouveau disque persistant, pd-standard, pd-balanced, pd-ssd ou pd-extreme. Le type de disque par défaut pour les disques persistants non de démarrage est pd-standard.
    • NEW_PERSISTENT_DISK_NAME: nom du nouveau disque persistant.
  • Si vous utilisez un modèle d'instance de VM pour cette tâche, créez un fichier JSON comme indiqué précédemment, mais remplacez le champ instances par le code suivant:

    "instances": [
        {
            "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
        }
    ],
    ...
    

    INSTANCE_TEMPLATE_NAME correspond au nom du modèle d'instance de cette tâche. Pour une tâche qui utilise des disques persistants, ce modèle d'instance doit définir et associer les disques persistants que vous souhaitez que la tâche utilise. Pour cet exemple, le modèle doit définir et associer un nouveau disque persistant nommé NEW_PERSISTENT_DISK_NAME et un disque persistant existant nommé EXISTING_PERSISTENT_DISK_NAME.

C++

Pour créer une tâche de traitement par lot qui utilise des disques persistants nouveaux ou existants à l'aide des bibliothèques clientes Cloud pour C++, utilisez la fonction CreateJob et incluez les éléments suivants:

  • Pour associer des disques persistants aux VM pour une tâche, incluez l'une des options suivantes :
    • Si vous n'utilisez pas de modèle d'instance de VM pour cette tâche, utilisez la méthode set_remote_path.
    • Si vous utilisez un modèle d'instance de VM pour cette tâche, utilisez la méthode set_instance_template.
  • Pour monter les disques persistants sur la tâche, utilisez le champ volumes avec les champs deviceName et mountPath. Pour les nouveaux disques persistants, utilisez également le champ mountOptions pour activer l'écriture.

Pour obtenir un exemple de code d'un cas d'utilisation similaire, consultez Utiliser un bucket Cloud Storage.

Go

Pour créer un job Batch qui utilise des disques persistants nouveaux ou existants à l'aide des bibliothèques clientes Cloud pour Go, utilisez la fonction CreateJob et incluez les éléments suivants:

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job with persistent disk
func createJobWithPD(w io.Writer, projectID, jobName, pdName string) error {
	// jobName := job-name
	// pdName := disk-name
	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("batchClient error: %w", err)
	}
	defer batchClient.Close()

	runn := &batchpb.Runnable{
		Executable: &batchpb.Runnable_Script_{
			Script: &batchpb.Runnable_Script{
				Command: &batchpb.Runnable_Script_Text{
					Text: "echo Hello world from script 1 for task ${BATCH_TASK_INDEX}",
				},
			},
		},
	}
	volume := &batchpb.Volume{
		MountPath: fmt.Sprintf("/mnt/disks/%v", pdName),
		Source: &batchpb.Volume_DeviceName{
			DeviceName: pdName,
		},
	}

	// The disk type of the new persistent disk, either pd-standard,
	// pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced
	disk := &batchpb.AllocationPolicy_Disk{
		Type:   "pd-balanced",
		SizeGb: 10,
	}

	taskSpec := &batchpb.TaskSpec{
		ComputeResource: &batchpb.ComputeResource{
			// CpuMilli is milliseconds per cpu-second. This means the task requires 1 CPU.
			CpuMilli:  1000,
			MemoryMib: 16,
		},
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Runnables:     []*batchpb.Runnable{runn},
		Volumes:       []*batchpb.Volume{volume},
	}

	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	labels := map[string]string{"env": "testing", "type": "container"}

	// Policies are used to define on what kind of virtual machines the tasks will run on.
	// Read more about local disks here: https://cloud.google.com/compute/docs/disks/persistent-disks
	allocationPolicy := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					MachineType: "n1-standard-1",
					Disks: []*batchpb.AllocationPolicy_AttachedDisk{
						{
							Attached: &batchpb.AllocationPolicy_AttachedDisk_NewDisk{
								NewDisk: disk,
							},
							DeviceName: pdName,
						},
					},
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	job := &batchpb.Job{
		Name:             jobName,
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           labels,
		LogsPolicy:       logsPolicy,
	}

	request := &batchpb.CreateJobRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, "us-central1"),
		JobId:  jobName,
		Job:    job,
	}

	created_job, err := batchClient.CreateJob(ctx, request)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)
	return nil
}

Java

Pour créer un job Batch qui utilise des disques persistants nouveaux ou existants à l'aide des bibliothèques clientes Cloud pour Java, utilisez la classe CreateJobRequest et incluez les éléments suivants:

  • Pour associer des disques persistants aux VM pour une tâche, incluez l'une des options suivantes :
  • Pour installer les disques persistants sur la tâche, utilisez la classe Volume avec les méthodes setDeviceName et setMountPath. Pour les nouveaux disques persistants, utilisez également la méthode setMountOptions pour activer l'écriture.

Par exemple, utilisez l'exemple de code suivant:


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.AttachedDisk;
import com.google.cloud.batch.v1.AllocationPolicy.Disk;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.AllocationPolicy.LocationPolicy;
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.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.cloud.batch.v1.Volume;
import com.google.common.collect.Lists;
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 CreatePersistentDiskJob {

  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 size of the new persistent disk in GB.
    // The allowed sizes depend on the type of persistent disk,
    // but the minimum is often 10 GB (10) and the maximum is often 64 TB (64000).
    int diskSize = 10;
    // The name of the new persistent disk.
    String newPersistentDiskName = "DISK-NAME";
    // The name of an existing persistent disk.
    String existingPersistentDiskName = "EXISTING-DISK-NAME";
    // The location of an existing persistent disk. For more info :
    // https://cloud.google.com/batch/docs/create-run-job-storage#gcloud
    String location = "regions/us-central1";
    // The disk type of the new persistent disk, either pd-standard,
    // pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced.
    String newDiskType = "pd-balanced";

    createPersistentDiskJob(projectId, region, jobName, newPersistentDiskName,
            diskSize, existingPersistentDiskName, location, newDiskType);
  }

  // Creates a job that attaches and mounts an existing persistent disk and a new persistent disk
  public static Job createPersistentDiskJob(String projectId, String region, String jobName,
                                            String newPersistentDiskName, int diskSize,
                                            String existingPersistentDiskName,
                                            String location, String newDiskType)
      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.
      String text = "echo Hello world from task ${BATCH_TASK_INDEX}. "
              + ">> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt";
      Runnable runnable =
          Runnable.newBuilder()
              .setScript(
                  Script.newBuilder()
                      .setText(text)
                      // 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.
              .addAllVolumes(volumes(newPersistentDiskName, existingPersistentDiskName))
              .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();

      // Policies are used to define the type of virtual machines the tasks will run on.
      InstancePolicy policy = InstancePolicy.newBuilder()
              .addAllDisks(attachedDisks(newPersistentDiskName, diskSize, newDiskType,
                  projectId, location, existingPersistentDiskName))
              .build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(
                  InstancePolicyOrTemplate.newBuilder()
                      .setPolicy(policy))
                  .setLocation(LocationPolicy.newBuilder().addAllowedLocations(location))
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              // We use Cloud Logging as it's an out-of-the-box option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(LogsPolicy.Destination.CLOUD_LOGGING))
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .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;
    }
  }

  // Creates link to existing disk and creates configuration for new disk
  private static Iterable<AttachedDisk> attachedDisks(String newPersistentDiskName, int diskSize,
                                                      String newDiskType, String projectId,
                                                      String existingPersistentDiskLocation,
                                                      String existingPersistentDiskName) {
    AttachedDisk newDisk = AttachedDisk.newBuilder()
            .setDeviceName(newPersistentDiskName)
            .setNewDisk(Disk.newBuilder().setSizeGb(diskSize).setType(newDiskType))
            .build();

    String diskPath = String.format("projects/%s/%s/disks/%s", projectId,
            existingPersistentDiskLocation, existingPersistentDiskName);

    AttachedDisk existingDisk = AttachedDisk.newBuilder()
            .setDeviceName(existingPersistentDiskName)
            .setExistingDisk(diskPath)
            .build();

    return Lists.newArrayList(existingDisk, newDisk);
  }

  // Describes a volume and parameters for it to be mounted to a VM.
  private static Iterable<Volume> volumes(String newPersistentDiskName,
                                          String existingPersistentDiskName) {
    Volume newVolume = Volume.newBuilder()
            .setDeviceName(newPersistentDiskName)
            .setMountPath("/mnt/disks/" + newPersistentDiskName)
            .addMountOptions("rw")
            .addMountOptions("async")
            .build();

    Volume existingVolume = Volume.newBuilder()
            .setDeviceName(existingPersistentDiskName)
            .setMountPath("/mnt/disks/" + existingPersistentDiskName)
            .build();

    return Lists.newArrayList(newVolume, existingVolume);
  }
}

Node.js

Pour créer un job Batch qui utilise des disques persistants nouveaux ou existants à l'aide des bibliothèques clientes Cloud pour Node.js, utilisez la méthode createJob et incluez les éléments suivants:

// 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();
// The name of the job that will be created.
// It needs to be unique for each project and region pair.
const jobName = 'batch-create-persistent-disk-job';
// 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 an existing persistent disk.
const existingPersistentDiskName = 'existing-persistent-disk-name';
// The name of the new persistent disk.
const newPersistentDiskName = 'new-persistent-disk-name';
// The size of the new persistent disk in GB.
// The allowed sizes depend on the type of persistent disk,
// but the minimum is often 10 GB (10) and the maximum is often 64 TB (64000).
const diskSize = 10;
// The location of an existing persistent disk. For more info :
// https://cloud.google.com/batch/docs/create-run-job-storage#gcloud
const location = 'regions/us-central1';
// The disk type of the new persistent disk, either pd-standard,
// pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced.
const newDiskType = 'pd-balanced';

// 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}.' +
        '>> /mnt/disks/NEW_PERSISTENT_DISK_NAME/output_task_${BATCH_TASK_INDEX}.txt',
    ],
  }),
});

// Define volumes and their parameters to be mounted to a VM.
const newVolume = new batch.Volume({
  deviceName: newPersistentDiskName,
  mountPath: `/mnt/disks/${newPersistentDiskName}`,
  mountOptions: ['rw', 'async'],
});

const existingVolume = new batch.Volume({
  deviceName: existingPersistentDiskName,
  mountPath: `/mnt/disks/${existingPersistentDiskName}`,
});

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

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

const newDisk = new batch.AllocationPolicy.Disk({
  type: newDiskType,
  sizeGb: diskSize,
});

// Policies are used to define on what kind of virtual machines the tasks will run on.
// Read more about local disks here: https://cloud.google.com/compute/docs/disks/persistent-disks
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  disks: [
    // Create configuration for new disk
    new batch.AllocationPolicy.AttachedDisk({
      deviceName: newPersistentDiskName,
      newDisk,
    }),
    // Create link to existing disk
    new batch.AllocationPolicy.AttachedDisk({
      existingDisk: `projects/${projectId}/${location}/disks/${existingPersistentDiskName}`,
      deviceName: existingPersistentDiskName,
    }),
  ],
});

const locationPolicy = new batch.AllocationPolicy.LocationPolicy({
  allowedLocations: [location],
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{policy: instancePolicy}],
  location: locationPolicy,
});

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 callCreateBatchPersistentDiskJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

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

await callCreateBatchPersistentDiskJob();

Python

Pour créer une tâche Batch qui utilise des disques persistants nouveaux ou existants à l'aide des bibliothèques clientes Cloud pour Python, utilisez la fonction CreateJob et incluez les éléments suivants:

  • Pour associer des disques persistants aux VM pour une tâche, incluez l'une des options suivantes :
  • Pour monter les disques persistants sur la tâche, utilisez la classe Volume avec les attributs device_name et mount_path. Pour les nouveaux disques persistants, utilisez également l'attribut mount_options pour activer l'écriture.

Par exemple, utilisez l'exemple de code suivant:

from google.cloud import batch_v1


def create_with_pd_job(
    project_id: str,
    region: str,
    job_name: str,
    disk_name: str,
    zone: str,
    existing_disk_name=None,
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances with mounted persistent disk.

    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/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        disk_name: name of the disk to be mounted for your Job.
        existing_disk_name(optional): existing disk name, which you want to attach to a job

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = (
        "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/"
        + disk_name
        + "/output_task_${BATCH_TASK_INDEX}.txt"
    )
    task.runnables = [runnable]
    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    volume = batch_v1.Volume()
    volume.device_name = disk_name
    volume.mount_path = f"/mnt/disks/{disk_name}"
    task.volumes = [volume]

    if existing_disk_name:
        volume2 = batch_v1.Volume()
        volume2.device_name = existing_disk_name
        volume2.mount_path = f"/mnt/disks/{existing_disk_name}"
        task.volumes.append(volume2)

    # 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 = 4
    group.task_spec = task

    disk = batch_v1.AllocationPolicy.Disk()
    # The disk type of the new persistent disk, either pd-standard,
    # pd-balanced, pd-ssd, or pd-extreme. For Batch jobs, the default is pd-balanced
    disk.type_ = "pd-balanced"
    disk.size_gb = 10

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # Read more about local disks here: https://cloud.google.com/compute/docs/disks/persistent-disks
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "n1-standard-1"

    attached_disk = batch_v1.AllocationPolicy.AttachedDisk()
    attached_disk.new_disk = disk
    attached_disk.device_name = disk_name
    policy.disks = [attached_disk]

    if existing_disk_name:
        attached_disk2 = batch_v1.AllocationPolicy.AttachedDisk()
        attached_disk2.existing_disk = (
            f"projects/{project_id}/zones/{zone}/disks/{existing_disk_name}"
        )
        attached_disk2.device_name = existing_disk_name
        policy.disks.append(attached_disk2)

    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy

    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    location = batch_v1.AllocationPolicy.LocationPolicy()
    location.allowed_locations = [f"zones/{zone}"]
    allocation_policy.location = location

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script"}

    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)

Utiliser un SSD local

Une tâche qui utilise des SSD locaux est soumise aux restrictions suivantes:

Vous pouvez créer un job qui utilise un SSD local à l'aide de la Google Cloud CLI, de l'API Batch, de Java ou de Python. L'exemple suivant décrit comment créer une tâche qui crée, associe et monte un SSD local. L'ordre comporte également trois tâches qui exécutent chacune un script pour créer un fichier dans le SSD local nommé output_task_TASK_INDEX.txt, où TASK_INDEX est l'indice de chaque tâche : 0, 1 et 2.

gcloud

Pour créer une tâche qui utilise des SSD locaux à l'aide de la CLI gcloud, utilisez la commande gcloud batch jobs submit. Dans le fichier de configuration JSON de la tâche, créez et associez les SSD locaux dans le champ instances, puis installez-les dans le champ volumes.

  1. Créez un fichier JSON.

    • Si vous n'utilisez pas de modèle d'instance pour cette tâche, créez un fichier JSON contenant les éléments suivants:

      {
          "allocationPolicy": {
              "instances": [
                  {
                      "policy": {
                          "machineType": MACHINE_TYPE,
                          "disks": [
                              {
                                  "newDisk": {
                                      "sizeGb": LOCAL_SSD_SIZE,
                                      "type": "local-ssd"
                                  },
                                  "deviceName": "LOCAL_SSD_NAME"
                              }
                          ]
                      }
                  }
              ]
          },
          "taskGroups": [
              {
                  "taskSpec": {
                      "runnables": [
                          {
                              "script": {
                                  "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/LOCAL_SSD_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                              }
                          }
                      ],
                      "volumes": [
                          {
                              "deviceName": "LOCAL_SSD_NAME",
                              "mountPath": "/mnt/disks/LOCAL_SSD_NAME",
                              "mountOptions": "rw,async"
                          }
                      ]
                  },
                  "taskCount":3
              }
          ],
          "logsPolicy": {
              "destination": "CLOUD_LOGGING"
          }
      }
      

      Remplacez les éléments suivants :

      • MACHINE_TYPE: type de machine, qui peut être prédéfini ou personnalisé, des VM de la tâche. Le nombre de SSD locaux autorisé dépend du type de machine des VM de votre tâche.
      • LOCAL_SSD_NAME: nom d'un SSD local créé pour cette tâche.
      • LOCAL_SSD_SIZE: taille de tous les SSD locaux en Go. Chaque SSD local est de 375 Go. Cette valeur doit donc être un multiple de 375 Go. Par exemple, pour deux SSD locaux, définissez cette valeur sur 750 Go.
    • Si vous utilisez un modèle d'instance de VM pour cette tâche, créez un fichier JSON comme indiqué précédemment, mais remplacez le champ instances par le code suivant:

      "instances": [
          {
              "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
          }
      ],
      

      INSTANCE_TEMPLATE_NAME est le nom du modèle d'instance de cette tâche. Pour une tâche qui utilise des SSD locaux, ce modèle d'instance doit définir et associer les SSD locaux que vous souhaitez que la tâche utilise. Pour cet exemple, le modèle doit définir et associer un SSD local nommé LOCAL_SSD_NAME.

  2. Exécutez la commande suivante :

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

    Remplacez les éléments suivants :

    • JOB_NAME: nom de la tâche.
    • LOCATION: emplacement de la tâche.
    • JSON_CONFIGURATION_FILE: chemin d'accès à un fichier JSON contenant les détails de configuration de la tâche.

API

Pour créer une tâche qui utilise des SSD locaux à l'aide de l'API Batch, utilisez la méthode jobs.create. Dans la requête, créez et associez les SSD locaux dans le champ instances, puis installez-les dans le champ volumes.

  • Si vous n'utilisez pas de modèle d'instance pour cette tâche, envoyez la requête suivante:

    POST https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs?job_id=JOB_NAME
    
    {
        "allocationPolicy": {
            "instances": [
                {
                    "policy": {
                        "machineType": MACHINE_TYPE,
                        "disks": [
                            {
                                "newDisk": {
                                    "sizeGb": LOCAL_SSD_SIZE,
                                    "type": "local-ssd"
                                },
                                "deviceName": "LOCAL_SSD_NAME"
                            }
                        ]
                    }
                }
            ]
        },
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/disks/LOCAL_SSD_NAME/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "deviceName": "LOCAL_SSD_NAME",
                            "mountPath": "/mnt/disks/LOCAL_SSD_NAME",
                            "mountOptions": "rw,async"
                        }
                    ]
                },
                "taskCount":3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Remplacez les éléments suivants :

    • PROJECT_ID: ID de projet de votre projet.
    • LOCATION: emplacement de la tâche.
    • JOB_NAME: nom de la tâche.
    • MACHINE_TYPE: type de machine, qui peut être prédéfini ou personnalisé, des VM de la tâche. Le nombre de SSD locaux autorisé dépend du type de machine des VM de votre tâche.
    • LOCAL_SSD_NAME: nom d'un SSD local créé pour cette tâche.
    • LOCAL_SSD_SIZE: taille de tous les SSD locaux en Go. Chaque SSD local est de 375 Go. Cette valeur doit donc être un multiple de 375 Go. Par exemple, pour deux SSD locaux, définissez cette valeur sur 750 Go.
  • Si vous utilisez un modèle d'instance de VM pour cette tâche, créez un fichier JSON comme indiqué précédemment, mais remplacez le champ instances par le code suivant:

    "instances": [
        {
            "instanceTemplate": "INSTANCE_TEMPLATE_NAME"
        }
    ],
    ...
    

    INSTANCE_TEMPLATE_NAME correspond au nom du modèle d'instance de cette tâche. Pour une tâche qui utilise des SSD locaux, ce modèle d'instance doit définir et associer les SSD locaux que vous souhaitez que la tâche utilise. Pour cet exemple, le modèle doit définir et associer un SSD local nommé LOCAL_SSD_NAME.

Go

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job with local SSD
// Note: local SSD does not guarantee Local SSD data persistence.
// More details here: https://cloud.google.com/compute/docs/disks/local-ssd#data_persistence
func createJobWithSSD(w io.Writer, projectID, jobName, ssdName string) error {
	// jobName := job-name
	// ssdName := disk-name
	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("batchClient error: %w", err)
	}
	defer batchClient.Close()

	runn := &batchpb.Runnable{
		Executable: &batchpb.Runnable_Script_{
			Script: &batchpb.Runnable_Script{
				Command: &batchpb.Runnable_Script_Text{
					Text: "echo Hello world from script 1 for task ${BATCH_TASK_INDEX}",
				},
			},
		},
	}
	volume := &batchpb.Volume{
		MountPath: fmt.Sprintf("/mnt/disks/%v", ssdName),
		Source: &batchpb.Volume_DeviceName{
			DeviceName: ssdName,
		},
	}

	// The size of all the local SSDs in GB. Each local SSD is 375 GB,
	// so this value must be a multiple of 375 GB.
	// For example, for 2 local SSDs, set this value to 750 GB.
	disk := &batchpb.AllocationPolicy_Disk{
		Type:   "local-ssd",
		SizeGb: 375,
	}

	taskSpec := &batchpb.TaskSpec{
		ComputeResource: &batchpb.ComputeResource{
			// CpuMilli is milliseconds per cpu-second. This means the task requires 1 CPU.
			CpuMilli:  1000,
			MemoryMib: 16,
		},
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Runnables:     []*batchpb.Runnable{runn},
		Volumes:       []*batchpb.Volume{volume},
	}

	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	labels := map[string]string{"env": "testing", "type": "container"}

	allocationPolicy := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					// The allowed number of local SSDs depends on the machine type for your job's VMs.
					// In this case, we tell the system to use "n1-standard-1" machine type, which require to attach local ssd manually.
					// Read more about local disks here: https://cloud.google.com/compute/docs/disks/local-ssd#lssd_disk_options
					MachineType: "n1-standard-1",
					Disks: []*batchpb.AllocationPolicy_AttachedDisk{
						{
							Attached: &batchpb.AllocationPolicy_AttachedDisk_NewDisk{
								NewDisk: disk,
							},
							DeviceName: ssdName,
						},
					},
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	job := &batchpb.Job{
		Name:             jobName,
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           labels,
		LogsPolicy:       logsPolicy,
	}

	request := &batchpb.CreateJobRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, "us-central1"),
		JobId:  jobName,
		Job:    job,
	}

	created_job, err := batchClient.CreateJob(ctx, request)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)
	return nil
}

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.AttachedDisk;
import com.google.cloud.batch.v1.AllocationPolicy.Disk;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
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.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.cloud.batch.v1.Volume;
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 CreateLocalSsdJob {

  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 local SSD created for this job.
    String localSsdName = "SSD-NAME";
    // The machine type, which can be predefined or custom, of the job's VMs.
    // The allowed number of local SSDs depends on the machine type
    // for your job's VMs are listed on: https://cloud.google.com/compute/docs/disks#localssds
    String machineType = "c3d-standard-8-lssd";
    // The size of all the local SSDs in GB. Each local SSD is 375 GB,
    // so this value must be a multiple of 375 GB.
    // For example, for 2 local SSDs, set this value to 750 GB.
    int ssdSize = 375;

    createLocalSsdJob(projectId, region, jobName, localSsdName, ssdSize, machineType);
  }

  // Create a job that uses local SSDs
  public static Job createLocalSsdJob(String projectId, String region, String jobName,
                                      String localSsdName, int ssdSize, String machineType)
      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();

      Volume volume = Volume.newBuilder()
          .setDeviceName(localSsdName)
          .setMountPath("/mnt/disks/" + localSsdName)
          .addMountOptions("rw")
          .addMountOptions("async")
          .build();

      TaskSpec task = TaskSpec.newBuilder()
          // Jobs can be divided into tasks. In this case, we have only one task.
          .addVolumes(volume)
          .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();

      // Policies are used to define on what kind of virtual machines the tasks will run on.
      InstancePolicy policy = InstancePolicy.newBuilder()
          .setMachineType(machineType)
          .addDisks(AttachedDisk.newBuilder()
              .setDeviceName(localSsdName)
              // For example, local SSD uses type "local-ssd".
              // Persistent disks and boot disks use "pd-balanced", "pd-extreme", "pd-ssd"
              // or "pd-standard".
              .setNewDisk(Disk.newBuilder().setSizeGb(ssdSize).setType("local-ssd")))
          .build();

      AllocationPolicy allocationPolicy =
          AllocationPolicy.newBuilder()
              .addInstances(
                  InstancePolicyOrTemplate.newBuilder()
                      .setPolicy(policy)
                      .build())
              .build();

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

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .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 = 'batch-local-ssd-job';
// The name of a local SSD created for this job.
const localSsdName = 'ssd-name';
// The machine type, which can be predefined or custom, of the job's VMs.
// The allowed number of local SSDs depends on the machine type
// for your job's VMs are listed on: https://cloud.google.com/compute/docs/disks#localssds
const machineType = 'c3d-standard-8-lssd';
// The size of all the local SSDs in GB. Each local SSD is 375 GB,
// so this value must be a multiple of 375 GB.
// For example, for 2 local SSDs, set this value to 750 GB.
const ssdSize = 375;

// 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}. This job has a total of ${BATCH_TASK_COUNT} tasks.',
    ],
  }),
});

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

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

// Policies are used to define on what kind of virtual machines the tasks will run on.
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType,
  disks: [
    new batch.AllocationPolicy.AttachedDisk({
      deviceName: localSsdName,
      // For example, local SSD uses type "local-ssd".
      // Persistent disks and boot disks use "pd-balanced", "pd-extreme", "pd-ssd"
      // or "pd-standard".
      newDisk: new batch.AllocationPolicy.AttachedDisk({
        type: 'local-ssd',
        sizeGb: ssdSize,
      }),
    }),
  ],
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{policy: instancePolicy}],
});

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 callCreateBatchGPUJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

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

await callCreateBatchGPUJob();

Python

from google.cloud import batch_v1


def create_local_ssd_job(
    project_id: str, region: str, job_name: str, ssd_name: str
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances with mounted local SSD.
    Note: local SSD does not guarantee Local SSD data persistence.
    More details here: https://cloud.google.com/compute/docs/disks/local-ssd#data_persistence

    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/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        ssd_name: name of the local ssd to be mounted for your Job.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    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."
    task.runnables = [runnable]
    task.max_retry_count = 2
    task.max_run_duration = "3600s"

    volume = batch_v1.Volume()
    volume.device_name = ssd_name
    volume.mount_path = f"/mnt/disks/{ssd_name}"
    task.volumes = [volume]

    # 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 = 4
    group.task_spec = task

    disk = batch_v1.AllocationPolicy.Disk()
    disk.type_ = "local-ssd"
    # The size of all the local SSDs in GB. Each local SSD is 375 GB,
    # so this value must be a multiple of 375 GB.
    # For example, for 2 local SSDs, set this value to 750 GB.
    disk.size_gb = 375
    assert disk.size_gb % 375 == 0

    # Policies are used to define on what kind of virtual machines the tasks will run on.
    # The allowed number of local SSDs depends on the machine type for your job's VMs.
    # In this case, we tell the system to use "n1-standard-1" machine type, which require to attach local ssd manually.
    # Read more about local disks here: https://cloud.google.com/compute/docs/disks/local-ssd#lssd_disk_options
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "n1-standard-1"

    attached_disk = batch_v1.AllocationPolicy.AttachedDisk()
    attached_disk.new_disk = disk
    attached_disk.device_name = ssd_name
    policy.disks = [attached_disk]

    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy

    allocation_policy = batch_v1.AllocationPolicy()
    allocation_policy.instances = [instances]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script"}
    # 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)

Utiliser un bucket Cloud Storage

Pour créer un job qui utilise un bucket Cloud Storage existant, sélectionnez l'une des méthodes suivantes:

  • Recommandation: Installez un bucket directement sur les VM de votre tâche en spécifiant le bucket dans la définition de la tâche, comme indiqué dans cette section. Lorsque la tâche s'exécute, le bucket est automatiquement installé sur les VM de votre tâche à l'aide de Cloud Storage FUSE.
  • Créez une tâche avec des tâches qui accèdent directement à un bucket Cloud Storage à l'aide de la gcloud CLI ou des bibliothèques clientes pour l'API Cloud Storage. Pour savoir comment accéder à un bucket Cloud Storage directement à partir d'une VM, consultez la documentation Compute Engine sur l'écriture et la lecture de données à partir de buckets Cloud Storage.

Avant de créer une tâche qui utilise un bucket, créez un bucket ou identifiez-en un existant. Pour en savoir plus, consultez les pages Créer des buckets et Lister les buckets.

Vous pouvez créer un job qui utilise un bucket Cloud Storage à l'aide de la console Google Cloud, de gcloud CLI, de l'API Batch, de C++, de Go, de Java, de Node.js ou de Python.

L'exemple suivant explique comment créer une tâche qui monte un bucket Cloud Storage. La tâche comporte également trois tâches qui exécutent chacune un script pour créer un fichier dans le bucket nommé output_task_TASK_INDEX.txt, où TASK_INDEX est l'index de chaque tâche : 0, 1 et 2.

Console

Pour créer une tâche qui utilise un bucket Cloud Storage à l'aide de la console Google Cloud, procédez comme suit:

  1. Dans la console Google Cloud, accédez à la page Liste des jobs.

    Accéder à la liste des tâches

  2. Cliquez sur Créer. La page Créer une tâche par lot s'ouvre. Dans le volet de gauche, la page Informations sur la tâche est sélectionnée.

  3. Configurez la page Informations sur la tâche:

    1. (Facultatif) Dans le champ Nom de la tâche, personnalisez le nom de la tâche.

      Par exemple, saisissez example-bucket-job.

    2. Configurez la section Détails de la tâche:

      1. Dans la fenêtre Nouvel exécutable, ajoutez au moins un script ou un conteneur pour que cette tâche s'exécute.

        Par exemple, procédez comme suit:

        1. Cochez la case Script. Une zone de texte s'affiche.

        2. Dans la zone de texte, saisissez le script suivant:

          echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt
          

          Remplacez MOUNT_PATH par le chemin d'installation que les exécutables de cette tâche utilisent pour accéder à un bucket Cloud Storage existant. Le chemin d'accès doit commencer par /mnt/disks/, suivi d'un répertoire ou d'un chemin d'accès de votre choix. Par exemple, si vous souhaitez représenter ce bucket avec un répertoire nommé my-bucket, définissez le chemin d'installation sur /mnt/disks/my-bucket.

        3. Cliquez sur OK.

      2. Dans le champ Nombre de tâches, saisissez le nombre de tâches pour cette tâche.

        Par exemple, saisissez 3.

      3. Dans le champ Parallélisme, saisissez le nombre de tâches à exécuter simultanément.

        Par exemple, saisissez 1 (valeur par défaut).

  4. Configurez la page Configurations supplémentaires:

    1. Dans le volet de gauche, cliquez sur Configurations supplémentaires. La page Configurations supplémentaires s'ouvre.

    2. Pour chaque bucket Cloud Storage que vous souhaitez installer sur cette tâche, procédez comme suit:

      1. Dans la section Volume de stockage, cliquez sur Ajouter un volume. La fenêtre New volume (Nouveau volume) s'affiche.

      2. Dans la fenêtre Nouveau volume, procédez comme suit:

        1. Dans la section Type de volume, sélectionnez Bucket Cloud Storage.

        2. Dans le champ Nom du bucket de stockage, saisissez le nom d'un bucket existant.

          Par exemple, saisissez le bucket que vous avez spécifié dans l'exécutable de cette tâche.

        3. Dans le champ Chemin d'installation, saisissez le chemin d'installation du bucket (MOUNT_PATH), que vous avez spécifié dans le fichier exécutable.

        4. Cliquez sur OK.

  5. Facultatif: Configurez les autres champs de cette tâche.

  6. Facultatif: Pour examiner la configuration de la tâche, dans le volet de gauche, cliquez sur Aperçu.

  7. Cliquez sur Créer.

La page Détails de la tâche affiche la tâche que vous avez créée.

gcloud

Pour créer une tâche qui utilise un bucket Cloud Storage à l'aide de gcloud CLI, utilisez la commande gcloud batch jobs submit. Dans le fichier de configuration JSON de la tâche, montez le bucket dans le champ volumes.

Par exemple, pour créer une tâche qui génère des fichiers dans un bucket Cloud Storage:

  1. Créez un fichier json contenant les éléments suivants :

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "gcs": {
                                "remotePath": "BUCKET_PATH"
                            },
                            "mountPath": "MOUNT_PATH"
                        }
                    ]
                },
                "taskCount": 3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Remplacez les éléments suivants :

    • BUCKET_PATH: chemin d'accès au répertoire du bucket auquel vous souhaitez que cette tâche accède, qui doit commencer par le nom du bucket. Par exemple, pour un bucket nommé BUCKET_NAME, le chemin d'accès BUCKET_NAME représente le répertoire racine du bucket et le chemin d'accès BUCKET_NAME/subdirectory représente le sous-répertoire subdirectory.
    • MOUNT_PATH: chemin d'accès au montage utilisé par les exécutables de la tâche pour accéder à ce bucket. Le chemin d'accès doit commencer par /mnt/disks/, suivi d'un répertoire ou d'un chemin d'accès de votre choix. Par exemple, si vous souhaitez représenter ce bucket avec un répertoire nommé my-bucket, définissez le chemin d'installation sur /mnt/disks/my-bucket.
  2. Exécutez la commande suivante :

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

    Remplacez les éléments suivants :

    • JOB_NAME: nom de la tâche.
    • LOCATION: emplacement de la tâche.
    • JSON_CONFIGURATION_FILE: chemin d'accès à un fichier JSON contenant les détails de configuration de la tâche.

API

Pour créer un job qui utilise un bucket Cloud Storage à l'aide de l'API Batch, utilisez la méthode jobs.create et montez le bucket dans le champ volumes.

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

{
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                        }
                    }
                ],
                "volumes": [
                    {
                        "gcs": {
                            "remotePath": "BUCKET_PATH"
                        },
                        "mountPath": "MOUNT_PATH"
                    }
                ]
            },
            "taskCount": 3
        }
    ],
    "logsPolicy": {
            "destination": "CLOUD_LOGGING"
    }
}

Remplacez les éléments suivants :

  • PROJECT_ID: ID de projet de votre projet.
  • LOCATION: emplacement de la tâche.
  • JOB_NAME: nom de la tâche.
  • BUCKET_PATH: chemin d'accès au répertoire du bucket auquel vous souhaitez que cette tâche accède, qui doit commencer par le nom du bucket. Par exemple, pour un bucket nommé BUCKET_NAME, le chemin d'accès BUCKET_NAME représente le répertoire racine du bucket et le chemin d'accès BUCKET_NAME/subdirectory représente le sous-répertoire subdirectory.
  • MOUNT_PATH: chemin d'accès au montage utilisé par les exécutables de la tâche pour accéder à ce bucket. Le chemin d'accès doit commencer par /mnt/disks/, suivi d'un répertoire ou d'un chemin d'accès de votre choix. Par exemple, si vous souhaitez représenter ce bucket avec un répertoire nommé my-bucket, définissez le chemin d'installation sur /mnt/disks/my-bucket.

C++

C++

Pour en savoir plus, consultez la documentation de référence de l'API C++ par lot.

Pour vous authentifier auprès de Batch, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

#include "google/cloud/batch/v1/batch_client.h"

  [](std::string const& project_id, std::string const& location_id,
     std::string const& job_id, std::string const& bucket_name) {
    // Initialize the request; start with the fields that depend on the sample
    // input.
    google::cloud::batch::v1::CreateJobRequest request;
    request.set_parent("projects/" + project_id + "/locations/" + location_id);
    request.set_job_id(job_id);
    // Most of the job description is fixed in this example; use a string to
    // initialize it, and then override the GCS remote path.
    auto constexpr kText = R"pb(
      task_groups {
        task_count: 4
        task_spec {
          compute_resource { cpu_milli: 500 memory_mib: 16 }
          max_retry_count: 2
          max_run_duration { seconds: 3600 }
          runnables {
            script {
              text: "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
            }
          }
          volumes { mount_path: "/mnt/share" }
        }
      }
      allocation_policy {
        instances {
          policy { machine_type: "e2-standard-4" provisioning_model: STANDARD }
        }
      }
      labels { key: "env" value: "testing" }
      labels { key: "type" value: "script" }
      logs_policy { destination: CLOUD_LOGGING }
    )pb";
    auto* job = request.mutable_job();
    if (!google::protobuf::TextFormat::ParseFromString(kText, job)) {
      throw std::runtime_error("Error parsing Job description");
    }
    job->mutable_task_groups(0)
        ->mutable_task_spec()
        ->mutable_volumes(0)
        ->mutable_gcs()
        ->set_remote_path(bucket_name);
    // Create a client and issue the request.
    auto client = google::cloud::batch_v1::BatchServiceClient(
        google::cloud::batch_v1::MakeBatchServiceConnection());
    auto response = client.CreateJob(request);
    if (!response) throw std::move(response).status();
    std::cout << "Job : " << response->DebugString() << "\n";
  }

Go

Go

Pour en savoir plus, consultez la documentation de référence de l'API Go par lot.

Pour vous authentifier auprès de Batch, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	durationpb "google.golang.org/protobuf/types/known/durationpb"
)

// Creates and runs a job that executes the specified script
func createScriptJobWithBucket(w io.Writer, projectID, region, jobName, bucketName string) error {
	// projectID := "your_project_id"
	// region := "us-central1"
	// jobName := "some-job"
	// jobName := "some-bucket"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %w", err)
	}
	defer batchClient.Close()

	// Define what will be done as part of the job.
	command := &batchpb.Runnable_Script_Text{
		Text: "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt",
	}

	// Specify the Google Cloud Storage bucket to mount
	volume := &batchpb.Volume{
		Source: &batchpb.Volume_Gcs{
			Gcs: &batchpb.GCS{
				RemotePath: bucketName,
			},
		},
		MountPath:    "/mnt/share",
		MountOptions: []string{},
	}

	// We can specify what resources are requested by each task.
	resources := &batchpb.ComputeResource{
		// CpuMilli is milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
		CpuMilli:  500,
		MemoryMib: 16,
	}

	taskSpec := &batchpb.TaskSpec{
		Runnables: []*batchpb.Runnable{{
			Executable: &batchpb.Runnable_Script_{
				Script: &batchpb.Runnable_Script{Command: command},
			},
		}},
		ComputeResource: resources,
		MaxRunDuration: &durationpb.Duration{
			Seconds: 3600,
		},
		MaxRetryCount: 2,
		Volumes:       []*batchpb.Volume{volume},
	}

	// Tasks are grouped inside a job using TaskGroups.
	taskGroups := []*batchpb.TaskGroup{
		{
			TaskCount: 4,
			TaskSpec:  taskSpec,
		},
	}

	// 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 := &batchpb.AllocationPolicy{
		Instances: []*batchpb.AllocationPolicy_InstancePolicyOrTemplate{{
			PolicyTemplate: &batchpb.AllocationPolicy_InstancePolicyOrTemplate_Policy{
				Policy: &batchpb.AllocationPolicy_InstancePolicy{
					MachineType: "e2-standard-4",
				},
			},
		}},
	}

	// We use Cloud Logging as it's an out of the box available option
	logsPolicy := &batchpb.LogsPolicy{
		Destination: batchpb.LogsPolicy_CLOUD_LOGGING,
	}

	jobLabels := map[string]string{"env": "testing", "type": "script"}

	// The job's parent is the region in which the job will run
	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, region)

	job := batchpb.Job{
		TaskGroups:       taskGroups,
		AllocationPolicy: allocationPolicy,
		Labels:           jobLabels,
		LogsPolicy:       logsPolicy,
	}

	req := &batchpb.CreateJobRequest{
		Parent: parent,
		JobId:  jobName,
		Job:    &job,
	}

	created_job, err := batchClient.CreateJob(ctx, req)
	if err != nil {
		return fmt.Errorf("unable to create job: %w", err)
	}

	fmt.Fprintf(w, "Job created: %v\n", created_job)

	return nil
}

Java

Java

Pour en savoir plus, consultez la documentation de référence de l'API Java par lot.

Pour vous authentifier auprès de Batch, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
import com.google.cloud.batch.v1.CreateJobRequest;
import com.google.cloud.batch.v1.GCS;
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.cloud.batch.v1.Volume;
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 CreateWithMountedBucket {

  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 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";

    // Name of the bucket to be mounted for your Job.
    String bucketName = "BUCKET_NAME";

    createScriptJobWithBucket(projectId, region, jobName, bucketName);
  }

  // This method shows how to create a sample Batch Job that will run
  // a simple command on Cloud Compute instances.
  public static void createScriptJobWithBucket(String projectId, String region, String jobName,
      String bucketName)
      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. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    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 from task ${BATCH_TASK_INDEX}. >> "
                              + "/mnt/share/output_task_${BATCH_TASK_INDEX}.txt")
                      // 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();

      Volume volume = Volume.newBuilder()
          .setGcs(GCS.newBuilder()
              .setRemotePath(bucketName)
              .build())
          .setMountPath("/mnt/share")
          .build();

      // We can specify what resources are requested by each task.
      ComputeResource computeResource =
          ComputeResource.newBuilder()
              // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
              .setCpuMilli(500)
              // In MiB.
              .setMemoryMib(16)
              .build();

      TaskSpec task =
          TaskSpec.newBuilder()
              // Jobs can be divided into tasks. In this case, we have only one task.
              .addRunnables(runnable)
              .addVolumes(volume)
              .setComputeResource(computeResource)
              .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(4).setTaskSpec(task).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
      InstancePolicy instancePolicy =
          InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

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

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              .putLabels("mount", "bucket")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(
                  LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .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());
    }
  }
}

Node.js

Node.js

Pour en savoir plus, consultez la documentation de référence de l'API Node.js par lot.

Pour vous authentifier auprès de Batch, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region you want to the job to run in. The regions that support Batch are listed here:
 * https://cloud.google.com/batch/docs/get-started#locations
 */
// const region = 'us-central-1';
/**
 * The name of the job that will be created.
 * It needs to be unique for each project and region pair.
 */
// const jobName = 'YOUR_JOB_NAME';
/**
 * The name of the bucket to be mounted.
 */
// const bucketName = 'YOUR_BUCKET_NAME';

// 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();

// Define what will be done as part of the job.
const task = new batch.TaskSpec();
const runnable = new batch.Runnable();
runnable.script = new batch.Runnable.Script();
runnable.script.text =
  'echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt';
// 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 runnable.script.text and runnable.script.path is mutually
// exclusive.
// runnable.script.path = '/tmp/test.sh'
task.runnables = [runnable];

const gcsBucket = new batch.GCS();
gcsBucket.remotePath = bucketName;
const gcsVolume = new batch.Volume();
gcsVolume.gcs = gcsBucket;
gcsVolume.mountPath = '/mnt/share';
task.volumes = [gcsVolume];

// We can specify what resources are requested by each task.
const resources = new batch.ComputeResource();
resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
resources.memoryMib = 16;
task.computeResource = resources;

task.maxRetryCount = 2;
task.maxRunDuration = {seconds: 3600};

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup();
group.taskCount = 4;
group.taskSpec = 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
const allocationPolicy = new batch.AllocationPolicy();
const policy = new batch.AllocationPolicy.InstancePolicy();
policy.machineType = 'e2-standard-4';
const instances = new batch.AllocationPolicy.InstancePolicyOrTemplate();
instances.policy = policy;
allocationPolicy.instances = [instances];

const job = new batch.Job();
job.name = jobName;
job.taskGroups = [group];
job.allocationPolicy = allocationPolicy;
job.labels = {env: 'testing', type: 'script'};
// We use Cloud Logging as it's an option available out of the box
job.logsPolicy = new batch.LogsPolicy();
job.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 callCreateJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

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

await callCreateJob();

Python

Python

Pour en savoir plus, consultez la documentation de référence de l'API Python par lot.

Pour vous authentifier auprès de Batch, configurez le service Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

from google.cloud import batch_v1


def create_script_job_with_bucket(
    project_id: str, region: str, job_name: str, bucket_name: str
) -> batch_v1.Job:
    """
    This method shows how to create a sample Batch Job that will run
    a simple command on Cloud Compute instances.

    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/get-started#locations
        job_name: the name of the job that will be created.
            It needs to be unique for each project and region pair.
        bucket_name: name of the bucket to be mounted for your Job.

    Returns:
        A job object representing the job created.
    """
    client = batch_v1.BatchServiceClient()

    # Define what will be done as part of the job.
    task = batch_v1.TaskSpec()
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt"
    task.runnables = [runnable]

    gcs_bucket = batch_v1.GCS()
    gcs_bucket.remote_path = bucket_name
    gcs_volume = batch_v1.Volume()
    gcs_volume.gcs = gcs_bucket
    gcs_volume.mount_path = "/mnt/share"
    task.volumes = [gcs_volume]

    # We can specify what resources are requested by each task.
    resources = batch_v1.ComputeResource()
    resources.cpu_milli = 500  # in milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
    resources.memory_mib = 16
    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 = 4
    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
    allocation_policy = batch_v1.AllocationPolicy()
    policy = batch_v1.AllocationPolicy.InstancePolicy()
    policy.machine_type = "e2-standard-4"
    instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
    instances.policy = policy
    allocation_policy.instances = [instances]

    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "script", "mount": "bucket"}
    # 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)

Utiliser un système de fichiers réseau

Vous pouvez créer un job qui utilise un système de fichiers réseau (NFS) existant, tel qu'un partage de fichiers Filestore, à l'aide de la console Google Cloud, de la gcloud CLI ou de l'API Batch.

Avant de créer une tâche qui utilise un NFS, assurez-vous que le pare-feu de votre réseau est correctement configuré pour autoriser le trafic entre les VM de votre tâche et le NFS. Pour en savoir plus, consultez la section Configurer des règles de pare-feu pour Filestore.

L'exemple suivant décrit comment créer une tâche qui spécifie et monte un NFS. La tâche comporte également trois tâches qui exécutent chacune un script pour créer un fichier dans le NFS nommé output_task_TASK_INDEX.txt, où TASK_INDEX est l'indice de chaque tâche : 0, 1 et 2.

Console

Pour créer une tâche qui utilise un NFS à l'aide de la console Google Cloud, procédez comme suit:

  1. Dans la console Google Cloud, accédez à la page Liste des jobs.

    Accéder à la liste des tâches

  2. Cliquez sur Créer. La page Créer une tâche par lot s'ouvre. Dans le volet de gauche, la page Informations sur la tâche est sélectionnée.

  3. Configurez la page Informations sur la tâche:

    1. (Facultatif) Dans le champ Nom de la tâche, personnalisez le nom de la tâche.

      Par exemple, saisissez example-nfs-job.

    2. Configurez la section Détails de la tâche:

      1. Dans la fenêtre Nouvel exécutable, ajoutez au moins un script ou un conteneur pour que cette tâche s'exécute.

        Par exemple, procédez comme suit:

        1. Cochez la case Script. Une zone de texte s'affiche.

        2. Dans la zone de texte, saisissez le script suivant:

          echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt
          

          Remplacez MOUNT_PATH par le chemin d'installation utilisé par l'exécutable de la tâche pour accéder à ce NFS. Le chemin d'accès doit commencer par /mnt/disks/, suivi d'un répertoire ou d'un chemin d'accès de votre choix. Par exemple, si vous souhaitez représenter ce NFS avec un répertoire nommé my-nfs, définissez le chemin d'installation sur /mnt/disks/my-nfs.

        3. Cliquez sur OK.

      2. Dans le champ Nombre de tâches, saisissez le nombre de tâches pour cette tâche.

        Par exemple, saisissez 3.

      3. Dans le champ Parallélisme, saisissez le nombre de tâches à exécuter simultanément.

        Par exemple, saisissez 1 (valeur par défaut).

  4. Configurez la page Configurations supplémentaires:

    1. Dans le volet de gauche, cliquez sur Configurations supplémentaires. La page Configurations supplémentaires s'ouvre.

    2. Pour chaque bucket Cloud Storage que vous souhaitez installer sur cette tâche, procédez comme suit:

      1. Dans la section Volume de stockage, cliquez sur Ajouter un volume. La fenêtre New volume (Nouveau volume) s'affiche.

      2. Dans la fenêtre Nouveau volume, procédez comme suit:

        1. Dans la section Type de volume, sélectionnez Network file system (Système de fichiers réseau).

        2. Dans le champ File server (Serveur de fichiers), saisissez l'adresse IP du serveur sur lequel se trouve le NFS que vous avez spécifié dans l'exécutable de cette tâche.

          Par exemple, si votre NFS est un partage de fichiers Filestore, spécifiez l'adresse IP de l'instance Filestore, que vous pouvez obtenir en décrivant l'instance Filestore.

        3. Dans le champ Chemin distant, saisissez un chemin d'accès au NFS que vous avez spécifié à l'étape précédente.

          Le chemin d'accès du répertoire NFS doit commencer par /, suivi du répertoire racine du NFS.

        4. Dans le champ Chemin d'installation, saisissez le chemin d'installation du NFS (MOUNT_PATH), que vous avez spécifié à l'étape précédente.

    3. Cliquez sur OK.

  5. Facultatif: Configurez les autres champs de cette tâche.

  6. Facultatif: Pour examiner la configuration de la tâche, dans le volet de gauche, cliquez sur Aperçu.

  7. Cliquez sur Créer.

La page Détails de la tâche affiche la tâche que vous avez créée.

gcloud

Pour créer une tâche qui utilise un NFS à l'aide de la CLI gcloud, utilisez la commande gcloud batch jobs submit. Dans le fichier de configuration JSON de la tâche, montez le NFS dans le champ volumes.

  1. Créez un fichier json contenant les éléments suivants :

    {
        "taskGroups": [
            {
                "taskSpec": {
                    "runnables": [
                        {
                            "script": {
                                "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                            }
                        }
                    ],
                    "volumes": [
                        {
                            "nfs": {
                                "server": "NFS_IP_ADDRESS",
                                "remotePath": "NFS_PATH"
                            },
                            "mountPath": "MOUNT_PATH"
                        }
                    ]
                },
                "taskCount": 3
            }
        ],
        "logsPolicy": {
            "destination": "CLOUD_LOGGING"
        }
    }
    

    Remplacez les éléments suivants :

    • NFS_IP_ADDRESS: adresse IP du NFS. Par exemple, si votre NFS est un partage de fichiers Filestore, spécifiez l'adresse IP de l'instance Filestore, que vous pouvez obtenir en décrivant l'instance Filestore.
    • NFS_PATH: chemin d'accès au répertoire NFS auquel vous souhaitez que cette tâche accède, qui doit commencer par /, suivi du répertoire racine du NFS. Par exemple, pour un partage de fichiers Filestore nommé FILE_SHARE_NAME, le chemin /FILE_SHARE_NAME représente le répertoire racine du partage de fichiers et le chemin /FILE_SHARE_NAME/subdirectory représente le sous-répertoire subdirectory.
    • MOUNT_PATH: chemin d'installation utilisé par les exécutables de la tâche pour accéder à ce NFS. Le chemin d'accès doit commencer par /mnt/disks/, suivi d'un répertoire ou d'un chemin d'accès de votre choix. Par exemple, si vous souhaitez représenter ce NFS avec un répertoire nommé my-nfs, définissez le chemin d'installation sur /mnt/disks/my-nfs.
  2. Exécutez la commande suivante :

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

    Remplacez les éléments suivants :

    • JOB_NAME: nom de la tâche.
    • LOCATION: emplacement de la tâche.
    • JSON_CONFIGURATION_FILE: chemin d'accès à un fichier JSON contenant les détails de configuration de la tâche.

API

Pour créer une tâche qui utilise un NFS à l'aide de l'API Batch, utilisez la méthode jobs.create et montez le NFS dans le champ volumes.

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

   {
    "taskGroups": [
        {
            "taskSpec": {
                "runnables": [
                    {
                        "script": {
                            "text": "echo Hello world from task ${BATCH_TASK_INDEX}. >> MOUNT_PATH/output_task_${BATCH_TASK_INDEX}.txt"
                        }
                    }
                ],
                "volumes": [
                    {
                        "nfs": {
                            "server": "NFS_IP_ADDRESS",
                            "remotePath": "NFS_PATH"
                        },
                        "mountPath": "MOUNT_PATH"
                    }
                ]
            },
            "taskCount": 3
        }
    ],
    "logsPolicy": {
        "destination": "CLOUD_LOGGING"
    }
}

Remplacez les éléments suivants :

  • PROJECT_ID: ID de projet de votre projet.
  • LOCATION: emplacement de la tâche.
  • JOB_NAME: nom de la tâche.
  • NFS_IP_ADDRESS: adresse IP du système de fichiers réseau. Par exemple, si votre NFS est un partage de fichiers Filestore, spécifiez l'adresse IP de l'instance Filestore, que vous pouvez obtenir en décrivant l'instance Filestore.
  • NFS_PATH: chemin d'accès au répertoire NFS auquel vous souhaitez que cette tâche accède, qui doit commencer par /, suivi du répertoire racine du NFS. Par exemple, pour un partage de fichiers Filestore nommé FILE_SHARE_NAME, le chemin /FILE_SHARE_NAME représente le répertoire racine du partage de fichiers et le chemin /FILE_SHARE_NAME/subdirectory représente un sous-répertoire.
  • MOUNT_PATH: chemin d'installation utilisé par les exécutables de la tâche pour accéder à ce NFS. Le chemin d'accès doit commencer par /mnt/disks/, suivi d'un répertoire ou d'un chemin d'accès de votre choix. Par exemple, si vous souhaitez représenter ce NFS avec un répertoire nommé my-nfs, définissez le chemin d'installation sur /mnt/disks/my-nfs.

Java


import com.google.cloud.batch.v1.AllocationPolicy;
import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.ComputeResource;
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.NFS;
import com.google.cloud.batch.v1.Runnable;
import com.google.cloud.batch.v1.TaskGroup;
import com.google.cloud.batch.v1.TaskSpec;
import com.google.cloud.batch.v1.Volume;
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 CreateScriptJobWithNfs {

  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 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 path of the NFS directory that you want this job to access.
    String nfsPath = "NFS_PATH";
    // The IP address of the Network File System.
    String nfsIpAddress = "NFS_IP_ADDRESS";

    createScriptJobWithNfs(projectId, region, jobName, nfsPath, nfsIpAddress);
  }

  // This method shows how to create a batch script job that specifies and mounts a NFS.
  public static Job createScriptJobWithNfs(String projectId, String region, String jobName,
                                            String nfsPath, String nfsIpAddress)
      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(
                  Runnable.Script.newBuilder()
                      .setText(
                          "echo Hello world from task ${BATCH_TASK_INDEX}. >> "
                              + "/mnt/share/output_task_${BATCH_TASK_INDEX}.txt")
                      // 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();

      // Describes a volume and parameters for it to be mounted to a VM.
      Volume volume = Volume.newBuilder()
          .setNfs(NFS.newBuilder()
              .setServer(nfsIpAddress)
              .setRemotePath(nfsPath)
              .build())
          .setMountPath("/mnt/share")
          .build();

      // We can specify what resources are requested by each task.
      ComputeResource computeResource =
          ComputeResource.newBuilder()
              // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
              .setCpuMilli(500)
              // In MiB.
              .setMemoryMib(16)
              .build();

      TaskSpec task =
          TaskSpec.newBuilder()
              // Jobs can be divided into tasks. In this case, we have only one task.
              .addRunnables(runnable)
              .addVolumes(volume)
              .setComputeResource(computeResource)
              .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(4).setTaskSpec(task).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())
              .build();

      Job job =
          Job.newBuilder()
              .addTaskGroups(taskGroup)
              .setAllocationPolicy(allocationPolicy)
              .putLabels("env", "testing")
              .putLabels("type", "script")
              .putLabels("mount", "bucket")
              // We use Cloud Logging as it's an out of the box available option.
              .setLogsPolicy(LogsPolicy.newBuilder()
                      .setDestination(LogsPolicy.Destination.CLOUD_LOGGING).build())
              .build();

      CreateJobRequest createJobRequest =
          CreateJobRequest.newBuilder()
              // The job's parent is the region in which the job will run.
              .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 = 'batch-nfs-job';
// The path of the NFS directory that you want this job to access.
const nfsPath = '/your_nfs_path';
// The IP address of the Network File System.
const nfsIpAddress = '0.0.0.0';
// The mount path that the job's tasks use to access the NFS.
const mountPath = '/mnt/disks';

// 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 from task ${BATCH_TASK_INDEX}. >> ' +
        '/mnt/share/output_task_${BATCH_TASK_INDEX}.txt',
    ],
  }),
});

// Define a volume that uses NFS.
const volume = new batch.Volume({
  nfs: new batch.NFS({
    server: nfsIpAddress,
    remotePath: nfsPath,
  }),
  mountPath,
});

// 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],
  volumes: [volume],
  computeResource,
  maxRetryCount: 2,
  maxRunDuration: {seconds: 3600},
});

// Tasks are grouped inside a job using TaskGroups.
const group = new batch.TaskGroup({
  taskCount: 3,
  taskSpec: 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
const instancePolicy = new batch.AllocationPolicy.InstancePolicy({
  machineType: 'e2-standard-4',
});

const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({
  instances: [{policy: instancePolicy}],
});

const job = new batch.Job({
  name: jobName,
  taskGroups: [group],
  allocationPolicy,
  labels: {env: 'testing', type: 'script'},
  // 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 callCreateBatchNfsJob() {
  // Construct request
  const request = {
    parent,
    jobId: jobName,
    job,
  };

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

await callCreateBatchNfsJob();

Python

from google.cloud import batch_v1


def create_job_with_network_file_system(
    project_id: str,
    region: str,
    job_name: str,
    mount_path: str,
    nfs_ip_address: str,
    nfs_path: str,
) -> batch_v1.Job:
    """
    Creates a Batch job with status events that mounts a Network File System (NFS).
    Function mounts an NFS volume using the provided NFS server, IP address and path.

    Args:
        project_id (str): project ID or project number of the Cloud project you want to use.
        region (str): 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
        job_name (str): the name of the job that will be created.
            It needs to be unique for each project and region pair.
        mount_path (str): The mount path that the job's tasks use to access the NFS.
        nfs_ip_address (str): The IP address of the NFS server (e.g., Filestore instance).
            Documentation on how to create a
            Filestore instance is available here: https://cloud.google.com/filestore/docs/create-instance-gcloud
        nfs_path (str): The path of the NFS directory that the job accesses.
            The path must start with a / followed by the root directory of the NFS.

    Returns:
        batch_v1.Job: The created Batch job object containing configuration details.
    """
    client = batch_v1.BatchServiceClient()

    # Create a runnable with a script that writes a message to a file
    runnable = batch_v1.Runnable()
    runnable.script = batch_v1.Runnable.Script()
    runnable.script.text = f"echo Hello world from task ${{BATCH_TASK_INDEX}}. >> {mount_path}/output_task_${{BATCH_TASK_INDEX}}.txt"

    # Define a volume that uses NFS
    volume = batch_v1.Volume()
    volume.nfs = batch_v1.NFS(server=nfs_ip_address, remote_path=nfs_path)
    volume.mount_path = mount_path

    # Create a task specification and assign the runnable and volume to it
    task = batch_v1.TaskSpec()
    task.runnables = [runnable]
    task.volumes = [volume]

    # 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"

    # Create a task group and assign the task specification to it
    group = batch_v1.TaskGroup()
    group.task_count = 1
    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 the job and assign the task group and allocation policy to it
    job = batch_v1.Job()
    job.task_groups = [group]
    job.allocation_policy = allocation_policy
    job.labels = {"env": "testing", "type": "container"}
    # 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 the job request and set the job and job ID
    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)

Étape suivante