As an alternative to running a workflow on a
[managed cluster](https://docs.cloud.google.com/managed-spark/docs/concepts/workflows/using-workflows#configuring_or_selecting_a_cluster), you can use a
cluster selector to choose an existing cluster for your workflow. At the
conclusion of the workflow, the selected cluster is not deleted.

Selectors specify one or more
[Managed Service for Apache Spark user labels](https://docs.cloud.google.com/managed-spark/docs/concepts/labels).
Clusters in same
[region](https://docs.cloud.google.com/compute/docs/regions-zones.md.txt#available) as the workflow whose labels
match all of the selector labels are eligible to run workflow jobs. If multiple
clusters match the selector, Managed Service for Apache Spark will
choose the cluster with the most free YARN memory.

## Add a cluster selector to a template

You can add a cluster selector to a workflow template using the
Google Cloud CLI or the Dataproc API.

### gcloud command

```
gcloud dataproc workflow-templates set-cluster-selector template-id \
    --region=region \
    --cluster-labels=name=value[[,name=value]...]
```

### REST API

See [WorkflowTemplatePlacement.ClusterSelector](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.locations.workflowTemplates.md.txt#clusterselector). This field is provided as part of a completed [WorkflowTemplate](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.workflowTemplates.md.txt#resource-workflowtemplate) submitted with a [workflowTemplates.create](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.workflowTemplates/create.md.txt) or [workflowTemplates.update](https://docs.cloud.google.com/managed-spark/docs/reference/rest/v1/projects.regions.workflowTemplates/update.md.txt) request.

### Console

You can view existing workflow templates and instantiated workflows from
the Managed Service for Apache Spark
[**Workflows**](https://console.cloud.google.com/dataproc/workflows/instances) page in Google Cloud console.

## Use automatically applied labels

You can point a cluster selector to an existing cluster by using
one of the following [automatically-applied cluster labels](https://docs.cloud.google.com/managed-spark/docs/concepts/labels#automatically-applied_labels):

- `goog-dataproc-cluster-name`
- `goog-dataproc-cluster-uuid`

**Example:**

```
gcloud dataproc workflow-templates set-cluster-selector template-id \
    --region=region \
    --cluster-labels=goog-dataproc-cluster-name=my-cluster
```

<br />

## Select from a cluster pool

You can let Managed Service for Apache Spark choose a cluster from a pool of clusters.
The cluster pools can be defined with labels.

**Example:**

```
gcloud dataproc clusters create cluster-1 --labels cluster-pool=pool-1 \
    --region=region
gcloud dataproc clusters create cluster-2 --labels cluster-pool=pool-1 \
    --region=region
gcloud dataproc clusters create cluster-3 --labels cluster-pool=pool-2 \
    --region=region
```

After cluster creation ...

```
gcloud dataproc workflow-templates create my-template \
    --region=region
gcloud dataproc workflow-templates set-cluster-selector my-template \
    --region=region \
    --cluster-labels=cluster-pool=pool-1
```

The workflow will be run on either cluster-1 or cluster-2, but not on cluster-3.