This example shows how use dstack to spin up a Ray cluster and run Ray jobs on it.
First create a fleet for the Ray cluster. We'll use one instance for a master node and three instances for worker nodes:
type: fleet
name: ray-fleet
nodes: 4
placement: cluster
backends: [gcp]
resources:
cpu: 8..
memory: 32GB..
gpu: 1dstack apply -f fleet.dstack.yamlThe following dstack task launches Ray master and worker nodes.
dstack makes the Ray dashboard available at localhost:8265.
type: task
name: ray-cluster
nodes: 4
commands:
- pip install -U "ray[default]"
- >
if [ $DSTACK_NODE_RANK = 0 ]; then
ray start --head --port=6379;
else
ray start --address=$DSTACK_MASTER_NODE_IP:6379
fi
ports:
- 8265 # ray dashboard port
resources:
shm_size: 8GBdstack apply -f cluster.dstack.yamlInstall Ray locally:
pip install rayNow you can submit Ray jobs to the cluster available at localhost:8265:
RAY_ADDRESS='http://localhost:8265' ray job submit \
--working-dir . \
--runtime-env-json='{"pip": ["ray[train]", "torch", "torchvision", "tqdm", "filelock"]}' \
-- python tasks/pytorch-mnist.pySee more examples in the Ray docs.
Using Ray via dstack is a powerful way to get access to the rich Ray ecosystem while benefiting from dstack's provisioning capabilities.