Integration with Kubeflow Notebooks¶
If you work in Kubeflow Notebooks and want to run distributed PySpark, the
Kubeflow SDK talks to the Spark
Operator directly. You write a few lines of Python in your notebook and get back an
ordinary SparkSession — no SparkApplication manifests, and no gateway component in
between.
How it works¶
Your notebook calls
SparkClient().connect().The SDK creates a
SparkConnectcustom resource in your namespace.The Spark Operator provisions the driver and executor pods.
The SDK returns a connected
SparkSessionpointed at the Spark Connect server.
The session is a normal Spark Connect client, so everything on the Using Spark Connect page applies.
Prerequisites¶
Spark Operator installed, with the
SparkConnectCRD and the operator watching your user namespacesA notebook image with the Kubeflow SDK installed (
kubeflow[spark])Permission to create
SparkConnectresources in your namespace
Getting started¶
Install the SDK, then pin the client to the Spark version it provisions. The spark extra installs a pyspark-connect release that does not currently match
DEFAULT_SPARK_VERSION, so the second step is required:
pip install "kubeflow[spark]"
SPARK_VERSION="$(python -c \
'from kubeflow.spark.backends.kubernetes import constants; print(constants.DEFAULT_SPARK_VERSION)')"
pip install "pyspark-connect==${SPARK_VERSION}"
Then, from the notebook:
from kubeflow.common.types import KubernetesBackendConfig
from kubeflow.spark import Name, SparkClient
client = SparkClient(backend_config=KubernetesBackendConfig(namespace="my-namespace"))
spark =