Create a Managed Service for Apache Spark custom image

You can create a Managed Service for Apache Spark cluster with a custom image that includes your pre-installed packages. This page shows you how to create a custom image and install it on a Managed Service for Apache Spark cluster.

Usage considerations and limitations

Before you begin

Make sure to set up your project before generating your custom image.

Set up your project

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Dataproc API, Compute Engine API, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Dataproc API, Compute Engine API, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. Install Python 3.11+
  15. Prepare a customization script that installs custom packages and/or updates configurations, for example:
      #! /usr/bin/bash
      apt-get -y update
      apt-get install python-dev
      apt-get install python-pip
      pip install numpy
      

Create a Cloud Storage bucket in your project

  1. In the Google Cloud console, go to the Cloud Storage Buckets page.

    Go to Buckets

  2. Click Create.
  3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
    1. In the Get started section, do the following:
      • Enter a globally unique name that meets the bucket naming requirements.
      • To add a bucket label, expand the Labels section (), click Add label, and specify a key and a value for your label.
    2. In the Choose where to store your data section, do the following:
      1. Select a Location type.
      2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
      3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

        Set up cross-bucket replication

        1. In the Bucket menu, select a bucket.
        2. In the Replication settings section, click Configure to configure settings for the replication job.

          The Configure cross-bucket replication pane appears.

          • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
          • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
          • Click Done.
    3. In the Choose how to store your data section, do the following:
      1. Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
      2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
    4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
    5. In the Choose how to protect object data section, do the following:
      • Select any of the options under Data protection that you want to set for your bucket.
        • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
        • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
        • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
          • To enable Object Retention Lock, click the Enable object retention checkbox.
          • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
      • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
  4. Click Create.

Generate a custom image

You use generate_custom_image.py, a Python program, to create a Managed Service for Apache Spark custom image.

How it works

The generate_custom_image.py program launches a temporary Compute Engine VM instance with the specified Managed Service for Apache Spark base image, then runs the customization script inside the VM instance to install custom packages and or update configurations. After the customization script finishes, it shuts down the VM instance and creates a Managed Service for Apache Spark custom image from the VM instance disk. The temporary VM is deleted after the custom image is created. The custom image is saved and can be used to create Managed Service for Apache Spark clusters.

The generate_custom_image.py program uses gcloud CLI to run multi-step workflows on Compute Engine.

Run the code

Fork or clone the files on GitHub at Managed Service for Apache Spark custom images.

Then, run the generate_custom_image.py script to have Managed Service for Apache Spark generate and save your custom image.

python3 generate_custom_image.py \
    --image-name=CUSTOM_IMAGE_NAME \
    [--family=CUSTOM_IMAGE_FAMILY_NAME] \
    --dataproc-version=IMAGE_VERSION \
    --customization-script=LOCAL_PATH \
    --zone=ZONE \
    --gcs-bucket=gs://BUCKET_NAME \
    [--no-smoke-test]

Required flags

Optional flags

For a listing of available optional flags, see Optional Arguments on GitHub.

If generate_custom_image.py is successful, the imageURI of the custom image is displayed in the terminal window output (the full imageUri is shown in bold below):

...
managedCluster:
    clusterName: verify-image-20180614213641-8308a4cd
    config:
      gceClusterConfig:
        zoneUri: ZONE
      masterConfig:
        imageUri: **https://www.googleapis.com/compute/beta/projects/PROJECT_ID/global/images/CUSTOM_IMAGE_NAME**
...

INFO:__main__:Successfully built Dataproc custom image: CUSTOM_IMAGE_NAME
INFO:__main__:

#####################################################################
  WARNING: DATAPROC CUSTOM IMAGE 'CUSTOM_IMAGE_NAME'
           WILL EXPIRE ON 2018-07-14 21:35:44.133000.
#####################################################################

Custom image version labels (advanced usage)

When using the Managed Service for Apache Spark standard custom image tool, the tool sets a goog-dataproc-version label on the created custom image. The label reflects the feature capabilities and protocols used by Managed Service for Apache Spark to manage the software on the image.

Advanced usage: If you use your own process to create a custom Managed Service for Apache Spark image, you must add the goog-dataproc-version label manually to your custom image, as follows:

  1. Extract the goog-dataproc-version label from the base Managed Service for Apache Spark image used to create the custom image.

    gcloud compute images describe ${BASE_DATAPROC_IMAGE} \
        --project cloud-dataproc \
        --format="value(labels.goog-dataproc-version)"
    
  2. Set the label on the custom image.

    gcloud compute images add-labels IMAGE_NAME --labels=[KEY=VALUE,...]
    

Use a custom image

You specify the custom image when you create a Managed Service for Apache Spark cluster. A custom image is saved in Cloud Compute Images, and is valid to create a Managed Service for Apache Spark cluster for 365 days from its creation date (see Create a cluster with an expired custom image to use a custom image after its 365-day expiration date).

Custom image URI

You pass the imageUri of the custom image to the cluster creation operation. This URI can be specified in one of three ways:

  1. Full URI:
    https://www.googleapis.com/compute/beta/projects/PROJECT_ID/global/images/`gs://`BUCKET_NAME`
  2. Partial URI: projects/PROJECT_ID/global/images/CUSTOM_IMAGE_NAME
  3. Short name: CUSTOM_IMAGE_NAME

Custom images can also be specified by their family URI, which always chooses the most recent image within the image family.

Full URI:
https://www.googleapis.com/compute/beta/projects/PROJECT_ID/global/images/family/CUSTOM_IMAGE_FAMILY_NAME/var>
  • Partial URI: projects/PROJECT_ID/global/images/family/CUSTOM_IMAGE_FAMILY_NAME
  • Find the custom image URI

    Google Cloud CLI

    Run the following command to list the names of your custom images.

    gcloud compute images list
    

    Pass the name of your custom image to the following command to list the URI (selfLink) of your custom image.

    gcloud compute images describe custom-image-name
    

    Output snippet:

    ...
    name: CUSTOM_IMAGE_NAME
    selfLink: https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/images/CUSTOM_IMAGE_NAME
    ...
    

    Google Cloud console

    1. Open the Compute Engine→Images page in the Google Cloud console, and then click the image name. You can insert a query in the filter images field to limit the number of displayed images.
    2. The Images details page opens. Click Equivalent REST.
    3. The REST response lists additional information about the image, including the selfLink, which is the image URI.
      {
        ...
        "name": "my-custom-image",
        "selfLink": "projects/PROJECT_ID/global/images/CUSTOM_IMAGE_NAME",
        "sourceDisk": ...,
        ...
      }
      

    Create a cluster with a custom image

    create a cluster with using the gcloud CLI, the Dataproc API, or the Google Cloud console.

    Google Cloud console

    1. Open the Create cluster page.
    2. In the Define your cluster section, click Image type and version, select Custom image, then confirm or change the project and select an available custom image.

    gcloud CLI

    Create a Managed Service for Apache Spark cluster with a custom image using the dataproc clusters create command with the --image flag.

    Example:
    gcloud dataproc clusters create CLUSTER-NAME \
        --image=CUSTOM_IMAGE_URI \
        --region=REGION \
        ... other flags
    

    REST API

    Create a cluster with a custom image by specifying custom image URI in the InstanceGroupConfig.imageUri field in the masterConfig, workerConfig, and, if applicable, secondaryWorkerConfig object included in a cluster.create API request.

    Example: REST request to create a standard Managed Service for Apache Spark cluster (one master, two worker nodes) with a custom image.

    POST /v1/projects/PROJECT_ID/regions/REGION/clusters/
    {
      "clusterName": "CLUSTER_NAME",
      "config": {
        "masterConfig": {
          "imageUri": "projects/PROJECT_ID/global/images/CUSTOM_IMAGE_NAME"
        },
        "workerConfig": {
          "imageUri": "projects/PROJECT_ID/global/images/CUSTOM_IMAGE_NAME"
        }
      }
    }
      

    Override Managed Service for Apache Spark cluster properties with a custom image

    You can use custom images to overwrite cluster properties that are set during cluster creation. If you create a cluster with a custom image, and the cluster creation operation sets properties with values that are different from those that are set by your custom image, the property values set by your custom image will take precedence.

    To set cluster properties with your custom image:

    1. In your custom image customization script, create a dataproc.custom.properties file in /etc/google-dataproc, then set cluster property values in the file.

      dataproc.conscrypt.provider.enable=VALUE
      dataproc.logging.stackdriver.enable=VALUE
      
      cat <<EOF >/etc/google-managed-spark/dataproc.custom.properties
      dataproc.conscrypt.provider.enable=true
      dataproc.logging.stackdriver.enable=false
      EOF
      

    Create a cluster with an expired custom image

    By default, custom images expire 365 days from the date of creation of the image. You can create a cluster that uses an expired custom image by completing the following steps.

    1. Attempt to create a Managed Service for Apache Spark cluster with an expired custom image or a custom image that will expire within 10 days.

      gcloud dataproc clusters create CLUSTER-NAME \
          --image=CUSTOM-IMAGE-NAME \
          --region=REGION \
          ... other flags
      
    2. The gcloud CLI will issue an error message that includes the cluster dataproc:dataproc.custom.image.expiration.token property name and token value.

    dataproc:dataproc.custom.image.expiration.token=TOKEN_VALUE
    

    Copy the TOKEN_VALUE string to the clipboard.

    1. Use the gcloud CLI to create the Managed Service for Apache Spark cluster again, adding the copied TOKEN_VALUE as a cluster property.

      gcloud dataproc clusters create CLUSTER-NAME \
          --image=CUSTOM-IMAGE-NAME \
          --properties=dataproc:dataproc.custom.image.expiration.token=TOKEN_VALUE \
          --region=REGION \
          ... other flags
      

    Cluster creation with the custom image should succeed.