Apache Spark
Run Spark batch jobs on OpenShift with the Spark operator, read Parquet and Iceberg, write Iceberg tables, and size drivers, executors and GPUs.
Spark is the batch transform we run on OpenShift. A job is a driver pod plus a set of executor pods that exist for that run and then go away. Nothing waits on a Hadoop cluster between the nightly accounts merge and the next one. The files it reads were landed by the ingestion lab. The tables it writes are Iceberg tables in the lakehouse.
Two ways start the same job. spark-submit talks to the Kubernetes API and creates the pods itself. The Kubeflow Spark operator takes a SparkApplication from Git and does that work for you. We keep the YAML. We use spark-submit when we are debugging a job from a workstation that already has oc access.
Prerequisites
- OpenShift 4.x,
oc, and a namespacedata-pipeline - The Spark operator installed, with permission for its service account to watch
SparkApplicationresources - A service account
sparkthat may create pods indata-pipeline - The lab image
spark-iceberg:3.5in thedata-pipelineimage stream: Spark 3.5, the Iceberg Spark runtime for that line, and Hadoop's S3A libraries. Bake those JARs into the image. A job that downloads them at start will fail on a cluster that cannot reach the internet, which is the normal case for the air-gapped platforms we build. - The
lakehousesecret (S3 keys) and a REST catalog aticeberg-rest.data-pipeline.svc:8181, as in the lakehouse lab
oc auth can-i create pods --as=system:serviceaccount:data-pipeline:spark -n data-pipeline
oc get crd sparkapplications.sparkoperator.k8s.io
Both commands should succeed before you apply a job. The first one is the usual reason a driver sits in Pending with a message about being unable to create pods.
A job is two kinds of pod
The driver plans the query, talks to the catalog and schedules tasks. It needs enough memory for the plan, not for the table. The executors read, shuffle and write. Add executors when the shuffle is the slow part. Add memory when a task is killed for its heap. Each job gets its own pods, and they exit when the job does.
Submit with the operator
accounts_silver.py reads one day's Parquet (or the CSV extract, which Spark reads the same way) and merges it into an Iceberg table. The path comes from the orchestrator, so the same image runs any date.
import sys
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
path = sys.argv[1] if len(sys.argv) > 1 else "s3a://lakehouse/bronze/accounts/"
changes = (
spark.read.option("header", True).option("encoding", "UTF-8")
.csv(path)
)
changes.createOrReplaceTempView("account_changes")
spark.sql("""
CREATE TABLE IF NOT EXISTS silver.accounts (
id bigint,
account_no string,
holder_name string,
balance decimal(18, 2),
updated_at timestamp
) USING iceberg
""")
spark.sql("""
MERGE INTO silver.accounts AS t
USING (
SELECT CAST(id AS bigint) AS id,
account_no,
holder_name,
CAST(balance AS decimal(18, 2)) AS balance,
CAST(updated_at AS timestamp) AS updated_at
FROM account_changes
) AS s
ON t.id = s.id
WHEN MATCHED AND s.updated_at >= t.updated_at THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *
""")
CSV columns arrive as strings, so the merge casts them. UPDATE SET * rewrites the matched row. Running the same day twice leaves the same rows, which is the property the rest of the platform depends on. holder_name stays Unicode: a Bangla name is a string, not something to transliterate on the way in.
The catalog and S3 settings live on the application, not in the script, so a workstation test and a cluster run can differ without editing code. Build the endpoint from the bucket claim (BUCKET_HOST, BUCKET_PORT). In-cluster RGW in our lab is plain HTTP. If you point at the external route instead, switch the scheme to https and set fs.s3a.connection.ssl.enabled to true.
apiVersion: sparkoperator.k8s.io/v1beta2
kind: SparkApplication
metadata:
name: accounts-silver
namespace: data-pipeline
spec:
type: Python
mode: cluster
image: image-registry.openshift-image-registry.svc:5000/data-pipeline/spark-iceberg:3.5
imagePullPolicy: IfNotPresent
mainApplicationFile: local:///opt/spark/jobs/accounts_silver.py
arguments:
- "s3a://lakehouse/bronze/accounts/dt=2026-10-29/"
sparkVersion: "3.5.3"
restartPolicy:
type: OnFailure
onFailureRetries: 1
onFailureRetryInterval: 30
sparkConf:
spark.sql.extensions: org.apache.iceberg.spark.extensions.IcebergSparkSessionExtensions
spark.sql.defaultCatalog: lake
spark.sql.catalog.lake: org.apache.iceberg.spark.SparkCatalog
spark.sql.catalog.lake.type: rest
spark.sql.catalog.lake.uri: http://iceberg-rest.data-pipeline.svc:8181
spark.sql.catalog.lake.warehouse: s3://lakehouse/warehouse
spark.hadoop.fs.s3a.endpoint: http://BUCKET_HOST:BUCKET_PORT
spark.hadoop.fs.s3a.path.style.access: "true"
spark.hadoop.fs.s3a.connection.ssl.enabled: "false"
spark.hadoop.fs.s3a.aws.credentials.provider: com.amazonaws.auth.EnvironmentVariableCredentialsProvider
driver:
cores: 1
memory: 2g
serviceAccount: spark
envFrom:
- secretRef:
name: lakehouse
executor:
cores: 2
instances: 2
memory: 4g
serviceAccount: spark
envFrom:
- secretRef:
name: lakehouse
local:///opt/spark/jobs/accounts_silver.py is a file inside the image (three slashes). That is what we ship, so a run does not depend on uploading code to the bucket first. Replace the S3 endpoint with the host and port from oc get cm lakehouse. If the catalog requires a client credential, set spark.sql.catalog.lake.credential from a secret. Do not paste a token into the YAML in Git.
oc apply -f accounts-silver.yaml
oc get sparkapplications -n data-pipeline
oc describe sparkapplication accounts-silver -n data-pipeline
oc get pods -n data-pipeline -l sparkoperator.k8s.io/app-name=accounts-silver
oc logs -n data-pipeline \
-l sparkoperator.k8s.io/app-name=accounts-silver,spark-role=driver --tail=80
describe shows the state moving through SUBMITTED, RUNNING and COMPLETED. The driver log is where a SQL error lands. The executor pods disappear after the run; the SparkApplication stays so you can read that state.
The same job from a workstation, if you would rather not use the operator for a one-off:
spark-submit \
--master "k8s://$(oc whoami --show-server)" \
--deploy-mode cluster \
--name accounts-silver \
--conf spark.kubernetes.namespace=data-pipeline \
--conf spark.kubernetes.authenticate.driver.serviceAccountName=spark \
--conf spark.kubernetes.container.image=image-registry.openshift-image-registry.svc:5000/data-pipeline/spark-iceberg:3.5 \
--conf spark.executor.instances=2 \
--conf spark.executor.memory=4g \
local:///opt/spark/jobs/accounts_silver.py \
s3a://lakehouse/bronze/accounts/dt=2026-10-29/
Add the same spark.sql.catalog.* and spark.hadoop.fs.s3a.* settings the YAML holds. The workstation needs a kubeconfig that can create pods, and it needs to reach the API server. Day to day, prefer the SparkApplication: it is the object Airflow submits and the object you review in Git.
Read Parquet and Iceberg, write Iceberg
Partner files that are already columnar land as Parquet under bronze. Read them with spark.read.parquet. Tables written earlier are Iceberg, and you read those through the catalog so you see the current snapshot, not a folder of files you happened to list.
transactions = spark.read.parquet("s3a://lakehouse/bronze/transactions/dt=2026-10-29/")
transactions.writeTo("silver.transactions").overwritePartitions()
balances = spark.table("silver.accounts")
overwritePartitions replaces only the partitions in that DataFrame, which suits a day's facts. An accounts dimension is the MERGE above, because one account is updated in place. spark.table("silver.accounts") asks the REST catalog for the current snapshot, and Trino in the lakehouse lab asks the same catalog. Bronze is never the target of a rebuild.
Resources
| Piece | Starting point | Raise it when |
|---|---|---|
| Driver memory | 2g |
The driver is killed, or a broadcast or a collect is part of the plan |
| Executor memory | 4g |
Tasks fail with an out-of-memory error or spill for the whole run |
| Executor cores | 2 |
The node has CPU left and each task's share of the heap is still comfortable |
| Executor instances | 2 |
The shuffle dominates and the quota has room for more pods |
restartPolicy |
one retry | The failure was a node eviction. Read the driver log before raising the count |
If executors stay Pending, oc describe pod on one of them. The message is quota, a missing service account right, or a request the node cannot meet.
GPUs
Most of these jobs are I/O and shuffle, and they do not need a GPU. We attach one only when a job does heavy joins or aggregations and we have built it on the RAPIDS accelerator image from the Spark RAPIDS lab. A small dimension merge does not get faster because a GPU happens to be on the node.
executor:
cores: 2
instances: 2
memory: 8g
gpu:
name: nvidia.com/gpu
quantity: 1
sparkConf:
spark.plugins: com.nvidia.spark.SQLPlugin
spark.rapids.sql.enabled: "true"
That image is the RAPIDS one, and the cluster needs the GPU operator so nvidia.com/gpu exists. Keep it on its own SparkApplication.
When a job fails
| What you see | Likely cause | What to do |
|---|---|---|
Executor OOMKilled |
The partition is too large for the heap | Raise executor memory, or repartition before the wide join |
Driver Pending for lack of pods |
spark cannot create pods |
Re-run the oc auth can-i check and fix the RoleBinding |
AccessDenied from S3 |
The secret did not reach the executor | envFrom on both driver and executor, keys AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY |
| Catalog connection refused | Wrong URI, or the catalog pod is down | oc get svc iceberg-rest and compare with spark.sql.catalog.lake.uri |
| Duplicates after a rerun | The write was an append | Change it to MERGE or overwritePartitions, then rerun that date |
| Bangla names corrupted | The CSV was read with the wrong encoding | option("encoding", "UTF-8"), and reject the file back in ingestion |
A failed SparkApplication keeps its status. Fix the cause, delete the object or change the name, and apply it again. The retry policy is for a pod that was evicted, not for a query that is wrong.
What's next
- Orchestration: run this
SparkApplicationevery day after the extract, and again when someone rebuilds a date. - Data lakehouse: namespaces, partitioning, time travel and compaction of the tables this job writes.
- Ingestion: the bronze files and the Debezium topics this job consumes.
- Spark on RAPIDS: the GPU path, when a job earns it.
- Data quality and governance: checks that fail the run when silver does not match bronze. The pipeline overview is why bronze stays, and why this job can be thrown away and started again.