Submit jobs to a Ray cluster
Ray clusters in EMR Serverless Spark workspaces provide a distributed computing framework for Python-native distributed computing, machine learning model training, and inference. You can create and start a Ray cluster, then submit Ray jobs through interactive development, the SDK, or the command line.
Create a Ray cluster
-
Log in to the EMR Serverless Spark console and go to your target workspace.
-
In the left-side navigation pane, click Cluster Management, and then click the Ray clusters tab.
-
Click Create Ray cluster. In the panel that appears, configure the following parameters and click Create.
-
Cluster name: Enter a name for the cluster.
-
Engine version: Select an engine version. The current default is
emr-1.0.1 (Ray 2.47.1, Python 3.12). -
Node groups: If you need to use a mix of CPU and GPU resources, you can create separate CPU and GPU node groups. Click + Add worker node group and configure the following parameters.
-
Node group name: Enter a unique name for the node group.
-
Resource queue: Select a resource queue.
-
Resource specification: Select a resource specification for the nodes.
-
Number of nodes: Set the number of worker nodes.
-
-
(Optional) Network connection: If you need to access services within the same VPC from the Ray cluster, select an existing network connection.
-
(Optional) Mount managed file directory: Select an existing managed file directory to enable your Ray jobs to read from and write to files within that directory. Mounting is a common data access method that maps paths from CPFS, NAS, and OSS to a local directory, allowing your programs to access data using local paths. To create a new managed file directory, see Manage Managed File Directories.
-
(Optional) Advanced cluster configurations: Configure advanced parameters in JSON format. For more information, see Advanced cluster configurations.
-
The console does not currently support configuring auto scaling for worker nodes. To configure this feature, use the API. For more information, see CreateRayCluster.
Start the Ray cluster
-
In the list of Ray clusters, find your target cluster and click Start.
-
Wait for the cluster status to change to running.
NoteStarting a Ray cluster for the first time in a workspace creates additional components and can take 2 to 3 minutes. Subsequent starts are significantly faster.
-
After the cluster starts, click invocation information to view the endpoint addresses and token required for submitting Ray jobs.
WarningIn the current version, the token for a Ray cluster does not expire. Keep your token secure to prevent unauthorized access.
Click Dashboard to open the Ray cluster's monitoring interface, where you can view cluster resource usage.
Submit a Ray job
Method 1: Interactive development
Ideal for quick starts and debugging. You can connect to a Ray cluster directly from the Notebook environment in the EMR Serverless Spark console without additional network configuration. To connect from your local machine, ensure it has network connectivity to the Ray cluster and a Python 3.12 environment.
-
Install the Ray client:
pip install ray[client]==2.47.1 -
On the invocation information page of the cluster, get the gRPC address and token.
-
Use the following sample code to connect to the Ray cluster for interactive development:
import ray import os def get_metadata(): headers = {"ray-token": "<your_token>"} return [(key.lower(), value) for key, value in headers.items()] ray.init(address="<your_gRPC_address>", _metadata=get_metadata()) import time @ray.remote def square(x): time.sleep(0.1) return x * x futures = [square.remote(i) for i in range(10)] results = ray.get(futures) print("Square results:", results)
Method 2: SDK submission
Suitable for embedding Ray job submissions in your application code. Your local environment must have Python 3.12 installed.
-
Install the Ray job submission client library:
pip install "ray[default]==2.47.1" -
On the invocation information page of the cluster, get the public endpoint and token.
-
Use the following sample code to connect to the Ray cluster and submit a job:
import time from ray.job_submission import JobSubmissionClient custom_headers = { "ray-token": "<your_token>" } client = JobSubmissionClient("<your_public_endpoint>", headers=custom_headers) # Alternatively, you can use the internal network. This requires the client to be in the same region and VPC as the cluster. # Submitting over the internal network is recommended for improved stability. # client = JobSubmissionClient("http://emr-spark-ray-gateway-cn-beijing-internal.spark.emr.aliyuncs.com", headers=custom_headers) job_id = client.submit_job( entrypoint="python -c 'print(\"Hello from Ray Client!\")'" ) print(f"Submitted job with ID: {job_id}") while True: status = client.get_job_status(job_id) print(f"Job status: {status}") if status.is_terminal(): break time.sleep(1) logs = client.get_job_logs(job_id) print("Job logs:") print(logs)
-
(Optional) To access files in a mounted directory from your job, mount a managed file directory when you create the Ray cluster. Then, reference the files using their mount path when you submit the job. The following example assumes you have mounted an OSS directory to the
/mnt/myosspath:import time from ray.job_submission import JobSubmissionClient custom_headers = { "ray-token": "<your_token>" } client = JobSubmissionClient("<your_public_endpoint>", headers=custom_headers) # Use the mount path to directly reference a script file in OSS as the entrypoint. job_id = client.submit_job( entrypoint="python /mnt/myoss/main.py" ) print(f"Submitted job with ID: {job_id}") while True: status = client.get_job_status(job_id) print(f"Job status: {status}") if status.is_terminal(): break time.sleep(1) logs = client.get_job_logs(job_id) print("Job logs:") print(logs)NoteIn your Ray job code, you can also read from and write to files directly using their mount paths. For example, you can read from
/mnt/myoss/test.txtor write a file to the/mnt/myoss/directory. Files written to this directory are synchronized to the corresponding OSS path.The following example shows how to read from and write to files in a mounted directory within a Ray job:
# The following code is submitted to the Ray cluster. import ray import time ray.init() @ray.remote def read_from_mount_path(i): with open('/mnt/myoss/test.txt', 'r', encoding='utf-8') as f: content = f.read() return content @ray.remote def write_to_mount_path(i): content = "Hello world " + str(i) with open('/mnt/myoss/output' + str(i) + '.txt', 'w', encoding='utf-8') as f: f.write(content) return 'Write succeeded' print("--- Starting remote tasks ---") start_time = time.time() obj_refs = [read_from_mount_path.remote(i) for i in range(4)] obj_refs2 = [write_to_mount_path.remote(i) for i in range(4)] results = ray.get(obj_refs) results2 = ray.get(obj_refs2)
Method 3: Command-line submission
Ideal for integrating with external systems. Your local environment must have Python 3.12 installed.
-
Install the Ray job submission client library:
pip install "ray[default]==2.47.1" -
On the invocation information page of the cluster, get the public endpoint and token.
-
Run the following commands to submit a Ray job:
# You must set the RAY_ADDRESS and RAY_JOB_HEADERS environment variables before you run `ray job submit`. export RAY_ADDRESS='<your_public_endpoint>' export RAY_JOB_HEADERS='{"ray-token": "<your_token>"}' ## Submit the job ### Example: Submit a job that performs a distributed sort. ray job submit --working-dir "." -- python test-ray-core-sort.py ### Supports reading from and writing to OSS. ray job submit --working-dir "." -- python test-saving-data-test.py ### Supports reading from and writing to OSS-HDFS. ray job submit --working-dir "." -- python test-saving-data-oss-hdfs-test.py ### The code supports reading from and writing to mounted directories. ray job submit --working-dir "." -- python test-saving-data-test-mount.py
For more command-line parameters, see the Ray job submission CLI official documentation.
Advanced cluster configurations
Specify advanced cluster configurations in JSON format. All parameters are optional.
|
Parameter |
Required |
Description |
|
|
Optional |
Specify the OSS files to download to the head and worker nodes at cluster startup. OSS and OSS-HDFS paths are supported. Separate multiple paths with a comma (,). The files are downloaded to the |
|
|
Optional |
The |