Chapter 3. Using the Kubeflow Spark Operator
You can run Spark data processing applications in a distributed environment on Red Hat OpenShift AI by using the Kubeflow Spark Operator. You can create custom resources (CRs) for specifying, running and surfacing Spark applications.
The following procedure explain how to create a custom Spark image and a SparkApplication custom resource (CR) application.
Prerequisites
- You have installed OpenShift 4.19 or newer.
- You have cluster administrator privileges.
- You have installed the Kubeflow Spark Operator on your OpenShift AI instance.
Procedure
Create a
ContainerFilecalledspark-imagewith the following parameters:FROM apache/spark:4.0.1 LABEL description="Apache Spark image compatible with OpenShift restricted-v2 SCC" USER root RUN chgrp -R 0 /opt/spark && \ chmod -R g=u /opt/spark && \ mkdir -p /opt/spark/work-dir /opt/spark/logs && \ chgrp -R 0 /opt/spark/work-dir /opt/spark/logs && \ chmod -R 775 /opt/spark/work-dir /opt/spark/logs RUN chmod 1777 /tmp ENV HOME=/home/spark RUN mkdir -p /home/spark && \ chgrp -R 0 /home/spark && \ chmod -R g=u /home/spark && \ chmod 775 /home/sparkBuild and tag the image for your registry with the following command:
$ podman build -t quay.io//spark-image:4.0.1 -f spark-imagePush your
spark-imageto the registry:$ podman push quay.io//spark-image:4.0.1The
SparkApplicationcustom resource definition (CRD) allows you to define Spark applications using YAML manifests.$ oc apply -f sparkapplication-example.yamlExample
SparkApplicationcustom resource (CR)apiVersion: sparkoperator.k8s.io/v1beta2 kind: SparkApplication metadata: name: ${APP_NAME} namespace: ${APP_NAMESPACE} spec: type: Scala mode: cluster image: <your-custom-spark-image> imagePullPolicy: IfNotPresent mainClass: org.apache.spark.examples.SparkPi mainApplicationFile: local:///opt/spark/examples/jars/spark-examples.jar arguments: - "1000" sparkVersion: "4.0.1" restartPolicy: type: Never driver: cores: 1 coreLimit: "1200m" memory: "512m" labels: version: 4.0.1 serviceAccount: spark-operator-spark securityContext: {} executor: cores: 1 instances: 1 memory: "512m" labels: version: 4.0.1 securityContext: {}Expand Table 3.1. SparkApplication CR reference Field Description namespaceThe namespace to run the Spark application, the default is the
redhat-ods-applicationsnamespace. If you want to run a Spark application on a custom namespace, you need to adjust the role binding to match the roles of the default namespace. For more informationtypeThe language of the application. For example, Python, Scala, etc.
modeDeployment mode, example fields include,
clusterorclient. In cluster mode, the driver runs in a pod.imageThe container image to use for the driver and executors. This image must include the Spark v4.0.1 runtime.
mainApplicationFileThe entry point path. For example,
local:///app/scripts/run_spark_job.py.sparkVersionThe version of Spark to use, the version included must match the image.
restartPolicyHandling of failures. Example fields include:
Never,OnFailure,Always.driver/executorResource requests (cores, memory), labels, service accounts, and security contexts.
volumes/volumeMountsPVCs for input and output data.
serviceAccountThe spark-operator-spark service account is created automatically by the Spark Operator installation and includes the necessary RBAC permissions for Spark drivers to create and manage executor pods.
When using Spark example YAMLs in OpenShift AI, the runAsGroup and runAsUser parameters must be removed, they may cause jobs crashed in your environment.
3.1. Using the Kubeflow Spark Operator in a custom namespace Copy linkLink copied to clipboard!
You can utilize the Kubeflow Spark Operator in a custom namespace. The following documentation explains how to view the Role, RoleBinding, and ServiceAccount resources in the default redhat-ods-applications and add them to a custom namespace.
Prerequisite
- You have installed OpenShift 4.19 or newer.
- You have cluster administrator privileges.
- You have installed the Kubeflow Spark Operator on your OpenShift AI instance.
-
You have installed the OpenShift CLI (
oc).
Procedure
To get the
ServiceAccountresource YAML, run the following command:$ oc get serviceaccount spark-operator-spark -n redhat-ods-applications -o yamlThen, update the
namespace:field to your custom namespace.To get the
Roleresource YAML, run the following command:$ oc get role spark-operator-role -n redhat-ods-applications -o yamlThen, update the
namespace:field to your custom namespace.To get the
RoleBindingresource YAML, run the following command:$ oc get rolebinding spark-operator-rolebinding -n redhat-ods-applications -o yamlThen, update the two
namespace:fields to your custom namespace.-
Save the new
Role,RoleBinding, andServiceAccountresources to a file or multiple files. Apply the changes to your custom namespace with the following command:
$ oc apply -f <resources-file(s).yaml> -n <custom-namespace>