Spark
This article explains how to run Spark applications in the MLOps environment, including applications stored on S3, access to the Driver Web UI, state persistence on S3, and the use of Spark from Jupyter notebooks through the Spark Operator (Python) kernel.
Overview
Spark workloads in this environment can be run in two main ways:
as SparkApplication resources managed by the Spark Operator,
from Jupyter notebooks through PySpark.
The source material focuses especially on S3 integration. To make Spark work correctly with S3, you must configure:
the application path,
the required Hadoop and AWS libraries,
the S3 endpoint,
access credentials,
and, if needed, persistence and UI exposure settings.
Important requirement
The serviceAccount must be set to default-editor.
This is a required setting for the SparkApplication configuration in this environment.
Run applications located on S3
To run an application stored on S3, you must add the appropriate S3 settings to the SparkApplication resource definition.
At minimum, you need to define the application path and main class correctly.
Example
mainApplicationFile: "s3a://spark-s3/spark-jobs/example.jar"
mainClass: "org.apache.spark.examples.SparkPi"
This tells Spark to load the application JAR directly from S3.
Add S3 libraries and credentials
To enable S3 access, you must extend the sparkConf, hadoopConf, driver, and executor sections. You also need a volume for the downloaded library files.
Example configuration snippets
hadoopConf:
"fs.s3a.impl": "org.apache.hadoop.fs.s3a.S3AFileSystem"
"fs.s3a.endpoint": "https://url.S3.com"
sparkConf:
"spark.driver.extraClassPath": "/opt/spark/custom-jars/*"
"spark.executor.extraClassPath": "/opt/spark/custom-jars/*"
"spark.hadoop.fs.s3a.impl": "org.apache.hadoop.fs.s3a.S3AFileSystem"
volumes:
- name: jar-volume
emptyDir: {}
driver:
cores: 1
memory: "512m"
initContainers:
- name: download-jars
image: curlimages/curl
command: ["/bin/sh", "-c"]
args:
- |
curl -L -o /data/hadoop-aws-3.3.4.jar https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-aws/3.3.4/hadoop-aws-3.3.4.jar;
curl -L -o /data/aws-java-sdk-bundle-1.12.696.jar https://repo1.maven.org/maven2/com/amazonaws/aws-java-sdk-bundle/1.12.696/aws-java-sdk-bundle-1.12.696.jar;
volumeMounts:
- name: jar-volume
mountPath: /data
volumeMounts:
- name: jar-volume
mountPath: /opt/spark/custom-jars
env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: s3-creds
key: accesskey
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: s3-creds
key: secretkey
executor:
instances: 1
cores: 1
memory: "512m"
deps:
initContainers:
- name: download-jars
image: curlimages/curl
command: ["/bin/sh", "-c"]
args:
- |
curl -L -o /data/hadoop-aws-3.3.4.jar https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-aws/3.3.4/hadoop-aws-3.3.4.jar;
curl -L -o /data/aws-java-sdk-bundle-1.12.696.jar https://repo1.maven.org/maven2/com/amazonaws/aws-java-sdk-bundle/1.12.696/aws-java-sdk-bundle-1.12.696.jar;
volumeMounts:
- name: jar-volume
mountPath: /data
volumeMounts:
- name: jar-volume
mountPath: /opt/spark/custom-jars
env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: s3-creds
key: accesskey
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: s3-creds
key: secretkey
How this works
The configuration above downloads the required S3-related JAR files before the driver and executor start.
You must also define the correct secret containing the S3 credentials:
AWS_ACCESS_KEY_ID for the S3 access key,
AWS_SECRET_ACCESS_KEY for the S3 secret key.
In addition, update the S3 endpoint inside the hadoopConf section so that it points to your actual S3 service.
Warning
Secrets must be configured for both the driver and the executor. If they are defined only for one of them, the Spark application may fail when reading from or writing to S3.
Access the Driver Web UI
To make the Driver Web UI available, add the following section to the SparkApplication resource:
spec:
sparkUIOptions:
serviceType: ClusterIP
ingressAnnotations:
kubernetes.io/ingress.class: istio
Once this is configured, the Driver Web UI is available at:
https://{applicationName}-{namespace}.domain.tld
This is useful for monitoring job execution and checking Spark internals during runtime.
Persist SparkApplication state
SparkApplication state can be persisted so that a failure does not always require restarting the whole processing flow from the beginning.
This can also be done by using S3.
Add the following to the sparkConf section:
sparkConf:
"spark.driver.recoveryMode": "FILESYSTEM"
"spark.driver.recoveryDirectory": "s3a://spark-s3/spark-recovery"
Warning
To enable state persistence on S3, you must first configure S3 access correctly, as described in the section on running applications located on S3.
Use Spark from Jupyter Notebook
To use Spark from a Jupyter notebook through PySpark, select the Spark Operator (Python) kernel.
Selecting the Spark Operator (Python) kernel in Jupyter Notebook.
Once the kernel is loaded, import PySpark in the notebook:
from pyspark.sql import SparkSession
This gives you access to standard Spark session creation and DataFrame operations inside the notebook.
Access S3 from a Jupyter Notebook
When using the Spark Operator (Python) kernel, you can read and write data from and to S3 by configuring the Spark session with the required S3 parameters.
Required values
You need to provide:
the S3 access key,
the S3 secret key,
the S3 endpoint.
Example Spark session for S3
S3_ENDPOINT = "https://URL.do.S3"
S3_BUCKET = "kfp-s3"
ACCESS_KEY = "AccessKey"
SECRET_KEY = "SecretKey"
spark = SparkSession.builder \
.appName("S3-Test") \
.config("spark.hadoop.fs.s3a.endpoint", S3_ENDPOINT) \
.config("spark.hadoop.fs.s3a.access.key", ACCESS_KEY) \
.config("spark.hadoop.fs.s3a.secret.key", SECRET_KEY) \
.config("spark.hadoop.fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem") \
.config("spark.hadoop.fs.s3a.path.style.access", "true") \
.config("spark.hadoop.fs.s3a.connection.ssl.enabled", "true") \
.getOrCreate()
Note
Depending on whether your S3 service uses SSL, set spark.hadoop.fs.s3a.connection.ssl.enabled to true or false.
Once the Spark session is created this way, you can read and write S3 objects from within that session.
Example SparkApplication using code from S3
The following example combines the S3 application path, custom JAR loading, UI exposure, restart behavior, and recovery configuration into one SparkApplication definition:
apiVersion: "sparkoperator.k8s.io/v1beta2"
kind: SparkApplication
metadata:
name: s3-spark-job
spec:
type: Java
mode: cluster
image: "spark:3.5.7"
mainApplicationFile: "s3a://s3/spark-jobs/example.jar"
mainClass: "org.apache.spark.examples.SparkPi"
hadoopConf:
"fs.s3a.impl": "org.apache.hadoop.fs.s3a.S3AFileSystem"
"fs.s3a.endpoint": "https://url.s3.com"
sparkConf:
"spark.driver.extraClassPath": "/opt/spark/custom-jars/*"
"spark.executor.extraClassPath": "/opt/spark/custom-jars/*"
"spark.hadoop.fs.s3a.impl": "org.apache.hadoop.fs.s3a.S3AFileSystem"
"spark.driver.recoveryMode": "FILESYSTEM"
"spark.driver.recoveryDirectory": "s3a://s3/spark-recovery"
sparkUIOptions:
serviceType: ClusterIP
ingressAnnotations:
kubernetes.io/ingress.class: istio
volumes:
- name: jar-volume
emptyDir: {}
restartPolicy:
type: OnFailure
onFailureRetries: 3
onFailureRetryInterval: 15
driver:
cores: 1
memory: "512m"
initContainers:
- name: download-jars
image: curlimages/curl
command: ["/bin/sh", "-c"]
args:
- |
curl -L -o /data/hadoop-aws-3.3.4.jar https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-aws/3.3.4/hadoop-aws-3.3.4.jar;
curl -L -o /data/aws-java-sdk-bundle-1.12.696.jar https://repo1.maven.org/maven2/com/amazonaws/aws-java-sdk-bundle/1.12.696/aws-java-sdk-bundle-1.12.696.jar;
volumeMounts:
- name: jar-volume
mountPath: /data
volumeMounts:
- name: jar-volume
mountPath: /opt/spark/custom-jars
env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: s3-creds
key: accesskey
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: s3-creds
key: secretkey
executor:
instances: 1
cores: 1
memory: "512m"
deps:
initContainers:
- name: download-jars
image: curlimages/curl
command: ["/bin/sh", "-c"]
args:
- |
curl -L -o /data/hadoop-aws-3.3.4.jar https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-aws/3.3.4/hadoop-aws-3.3.4.jar;
curl -L -o /data/aws-java-sdk-bundle-1.12.696.jar https://repo1.maven.org/maven2/com/amazonaws/aws-java-sdk-bundle/1.12.696/aws-java-sdk-bundle-1.12.696.jar;
volumeMounts:
- name: jar-volume
mountPath: /data
volumeMounts:
- name: jar-volume
mountPath: /opt/spark/custom-jars
env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: s3-creds
key: accesskey
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: s3-creds
key: secretkey
Example notebook code for reading and writing S3 data
The following example creates a Spark session, writes test data to S3, and then reads it back:
import os
from pyspark.sql import SparkSession
S3_ENDPOINT = "https://s3.url.com"
S3_BUCKET = "s3"
ACCESS_KEY = "accessKey"
SECRET_KEY = "secretKey"
print("⏳ Initializing Spark Session...")
spark = SparkSession.builder \
.appName("S3-Test") \
.config("spark.hadoop.fs.s3a.endpoint", S3_ENDPOINT) \
.config("spark.hadoop.fs.s3a.access.key", ACCESS_KEY) \
.config("spark.hadoop.fs.s3a.secret.key", SECRET_KEY) \
.config("spark.hadoop.fs.s3a.impl", "org.apache.hadoop.fs.s3a.S3AFileSystem") \
.config("spark.hadoop.fs.s3a.path.style.access", "true") \
.config("spark.hadoop.fs.s3a.connection.ssl.enabled", "true") \
.getOrCreate()
TEST_PATH = f"s3a://{S3_BUCKET}/spark-jobs/"
print(f"\n📝 Testing S3 write to: {TEST_PATH}")
# Create test data
test_data = [
("Hadoop", "3.3.1"),
("AWS-SDK", "1.11.901"),
("Status", "Compatible")
]
columns = ["Component", "Version"]
df = spark.createDataFrame(test_data, columns)
print("📋 Test data created:")
df.show()
# Write to S3
print("🚀 Writing to S3...")
df.write.mode("overwrite").csv(TEST_PATH)
print("✅ S3 WRITE successful!")
# Read back from S3
print("📖 Reading back from S3...")
read_df = spark.read.csv(TEST_PATH)
row_count = read_df.count()
print(f"✅ S3 READ successful! Found {row_count} rows:")
read_df.show()
This is a practical end-to-end test for confirming that both S3 writes and reads work correctly in the notebook session.
What to do next
After configuring Spark with S3 and notebook access, the next useful topics are:
creating production-ready SparkApplication manifests,
storing real Spark job artifacts on S3,
exposing and monitoring the Driver Web UI,
validating restart and recovery behavior,
building reusable notebook examples for S3-backed Spark workloads.