Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Launch a dask-gateway cluster

This guide shows you how to launch a Dask gateway cluster for parallel and distributed computing.

What is Dask Gateway?

Dask Gateway allows users to launch clusters for scaling computations efficiently with more CPU and memory on cloud resources, without requiring direct access to the underlying Kubernetes backend of the 2i2c hub. Configuration, such as efficient cluster resourcing, authentication and security settings, is automatically handled for users to provide a consistent user experience across the hub.

Usage

This section roughly follows the Dask Gateway Docs – Usage together with the Training on Large Datasets example.

Connect to a dask-gateway server

Create a gateway client to communicate with the dask-gateway server.

from dask_gateway import Gateway
gateway = Gateway()  # Uses values configured for the 2i2c Dask hub (recommended)

Configure cluster options

Specify options for your gateway cluster with the options widget.

options = gateway.cluster_options()
options
Screenshot of an interactive options widget to configure gateway cluster options.

Cluster options

Instance type running worker containers
This defaults to the machine type n2-highmem-16 for Google Cloud and r5.4xlarge for AWS, with a maximum of 16 CPUs available.
Resources per worker container
Select 1/2/4/8/16 CPUs and corresponding memory requests from a dropdown menu.
Image
This defaults to the user image deployed on the Dask hub.
Environment variables (YAML)
Set environment variables for both the workers and schedulers using YAML, e.g. ENV_VAR: my_environment_variable.
Idle cluster terminated after (minutes)
This defaults to 30 minutes. Consider cloud computing resources and costs for your hub when setting this value.

Create and scale gateway cluster

Pass the cluster options to a new gateway cluster.

cluster = gateway.new_cluster(options)
cluster

Manual scaling

Manually scale the cluster size to a fixed number of workers.

  1. Expand the Manual scaling dropdown in the cluster widget.

  2. Select the number of workers.

  3. Click Scale to confirm.

Screenshot of an interactive cluster widget to configure manual worker scaling.

Adaptive scaling

Adapt the cluster size dynamically based on current load. This helps to scale up the number of workers when necessary but scale it down and save resources when not actively computing.

  1. Expand the Adaptive scaling dropdown in the cluster widget.

  2. Select the minimum and maximum number of workers.

  3. Click Adapt to confirm.

Screenshot of an interactive cluster widget to configure adaptive worker scaling.

Connect to the gateway cluster

Connect to the gateway cluster to start doing work with your workers.

client = cluster.get_client()
client

Note the dashboard address of the form /services/dask-gateway/clusters/... to connect to the Dask dashboard later.

Connect Dask dashboard to Dask JupyterLab extension

Connect to a Dask dashboard to monitor computations with the JupyterLab extension.

  1. Copy the dashboard address from Connect to the gateway cluster or from running the command client.dashboard_link

  2. Click the Dask icon Dask icon in the left sidebar.

  3. In the search box at the top of the panel, paste in the full dashboard URL of the form

    https://<hub-name>.<community-name>.2i2c.cloud/<dashboard-address>
  4. Select the diagnostic plots to visualize, e.g. workers memory, CPU, task stream.

Run computations on the cluster

Import the Python packages for the Train Models on Large Datasets example.

import dask_ml.datasets
import dask_ml.cluster
import matplotlib.pyplot as plt

Generate random datasets for k-means clustering analysis.

X, y = dask_ml.datasets.make_blobs(n_samples=10000000,
                                   chunks=1000000,
                                   random_state=0,
                                   centers=3,)
X = X.persist()
X

Group the clusters with the k-means algorithm.

km = dask_ml.cluster.KMeans(n_clusters=3, init_max_iter=2, oversampling_factor=10)
km.fit(X)

Video showing dask dashboard while computing k-means clustering.

Plot the results.

fig, ax = plt.subplots()
ax.scatter(X[::10000, 0], X[::10000, 1], marker='.', c=km.labels_[::10000],
           cmap='viridis', alpha=0.25);
Plot of the k-means clustering results.

Shut down the cluster

Shut down the cluster when not in use to minimize the waste of computational resources and costs.

cluster.close()

FAQs