Persist Spark job logs and view completed job details, such as stages, tasks, and resource usage, through Spark History Server.
Prerequisites
-
The ack-spark-operator add-on is deployed.
A kubectl client is connected to the ACK cluster. For more information, see Connect to an ACK cluster using kubectl.
Step 1: Deploy the ack-spark-history-server add-on
Log on to the ACK console. In the left navigation pane, click .
-
On the Marketplace page, click the App Catalog tab, then search for and select ack-spark-history-server.
-
On the ack-spark-history-server page, click Deploy.
-
In the Create panel, select a cluster and namespace, then click Next.
-
On the Parameters page, set the parameters and click OK.
The following parameters configure log storage, environment variables, and the Service. For all options, see the ConfigMaps tab on the ack-spark-history-server page.
NoteWhen deploying ack-spark-history-server, specify backend storage for logs, such as Object Storage Service (OSS), a persistent volume claim (PVC), or HDFS.
-
(Required) Configure backend storage for logs
Choose OSS, a PVC, or HDFS as backend storage.
OSS
Set the following parameters.
ImportantSpark job logs are uploaded only after the job completes. You cannot view logs from running jobs in real time.
Parameter
Description
Example
spark.history.fs.logDirectoryThe log directory URL. Create the directory, such as
spark/spark-events, before deploying the add-on. See Managing Directories.oss://<Bucket name>/spark/spark-eventsstorage.oss.enableEnables OSS or OSS-HDFS as backend storage for logs.
truestorage.oss.endpointThe OSS endpoint.
oss-cn-beijing-internal.aliyuncs.comstorage.oss.existingSecret(Recommended) The name of an existing Secret with OSS access credentials. See Example of an AccessKey Secret YAML file.
spark-oss-secretstorage.oss.createSecretIf no existing Secret is specified, a new Secret is created automatically to store OSS access credentials.
Not applicable
storage.oss.createSecret.accessKeyIdThe AccessKey ID of your Alibaba Cloud account.
yourAccessKeyID
storage.oss.createSecret.accessKeySecretThe AccessKey Secret of your Alibaba Cloud account.
yourAccessKeySecret
PVC
Set the following parameters.
Parameter
Description
Example
spark.history.fs.logDirectoryThe URL of the log directory.
file:///mnt/spark/spark-eventsstorage.pvc.enableEnables a PVC as backend storage for logs.
truestorage.pvc.nameThe name of an existing PVC. Ensure the PV and PVC are created and bound in the
ack-spark-history-servernamespace. See Storage-CSI."<PVC name>"storage.pvc.mountPathThe mount path of the PVC in the container.
"/mnt"HDFS
Set the following parameter.
Parameter
Description
Example
spark.history.fs.logDirectoryThe URL of the log directory.
hdfs://namenode:port/spark/spark-events -
(Optional) Service type and port
A Service exposes the History Server web UI by default. Configure the type and port with
service.typeandservice.port.Parameter
Description
Default
service.typeThe type of the Service. Valid values:
-
ClusterIP -
NodePort -
LoadBalancer
ClusterIPservice.portThe port for accessing the web UI.
18080 -
-
(Optional) Environment variables
Add environment variables in
envto configure the History Server.Environment variable
Description
Default
SPARK_DAEMON_MEMORYThe memory allocated to Spark History Server.
1gSPARK_DAEMON_JAVA_OPTSThe JVM options for Spark History Server.
""SPARK_DAEMON_CLASSPATHThe classpath for the Spark History Server.
""SPARK_PUBLIC_DNSThe public address of Spark History Server. If unset, the application history uses the internal address, which can cause broken links.
""SPARK_HISTORY_OPTSA set of
spark.history.*configuration properties.""Add configurations to
sparkConfto customize the History Server. Common configurations:Property
Description
Default
spark.history.fs.update.intervalThe interval to check for log updates.
10sspark.history.fs.retainedApplicationsThe maximum number of application UIs to cache.
50spark.history.ui.portThe Spark History Server port.
18080spark.history.fs.cleaner.enabledSpecifies whether to periodically clean up event logs.
falsespark.history.fs.cleaner.intervalThe event log cleanup interval when
spark.history.fs.cleaner.enabled=true.1dspark.history.fs.cleaner.maxAgeMaximum age of event logs before deletion when
spark.history.fs.cleaner.enabled=true.7dspark.history.fs.cleaner.maxNumMaximum number of log files to retain when
spark.history.fs.cleaner.enabled=true. Excess files are deleted during cleanup.Int.MaxValuespark.history.fs.driverlog.cleaner.enabledSpecifies whether to periodically clean up driver event logs.
spark.history.fs.cleaner.enabledspark.history.fs.driverlog.cleaner.intervalThe driver event log cleanup interval when
spark.history.fs.driverlog.cleaner.enabled=true.spark.history.fs.cleaner.intervalspark.history.fs.driverlog.cleaner.maxAgeMaximum age of driver event logs before deletion when
spark.history.fs.driverlog.cleaner.enabled=true.spark.history.fs.cleaner.maxAgespark.history.fs.numReplayThreadsThe number of threads for processing log files.
25% of the available CPU cores.
spark.history.store.maxDiskUsageThe maximum disk space for the application history cache.
10g
-
Step 2: Access the web UI
By default, the Service type is ClusterIP. Forward a local port to access the web UI. To use an existing SLB instance instead, see Expose an application by using a Service that is associated with an existing SLB instance.
kubectl port-forward is for test verification only and is not suitable for production due to security risks.
-
Configure local port forwarding:
RELEASE_NAME=spark-history-server RELEASE_NAMESPACE=spark-operator SERVICE_NAME=${RELEASE_NAME}-service SERVICE_PORT=$(kubectl get service ${SERVICE_NAME} --namespace ${RELEASE_NAMESPACE} -o jsonpath="{.spec.ports[0].port}") echo "Now you can go to http://127.0.0.1:18080 to visit spark history server." kubectl port-forward --namespace ${RELEASE_NAMESPACE} services/${SERVICE_NAME} 18080:${SERVICE_PORT}Expected output:
Now you can go to http://127.0.0.1:18080 to visit spark history server. Forwarding from 127.0.0.1:18080 -> 18080 Forwarding from [::1]:18080 -> 18080 -
Go to http://127.0.0.1:18080 to view the Spark History Server web UI.

Step 3: Enable event logging
Enable event logging in your Spark jobs by configuring the following parameters.
|
Parameter |
Description |
Example |
|
|
Enables event logging. Valid values:
|
|
|
|
The storage path for event logs. Examples:
|
|
Example: Configure OSS for event logging
The following example configures OSS as backend storage for event logging.
-
Build a Spark container image
Community Spark images lack the OSS JAR packages. Build a custom image with the Hadoop OSS SDK JARs shown below, then push it to your repository. See Build an image by using an Enterprise Edition instance. Use dependency JARs matching your Spark version's Hadoop version.
ARG SPARK_IMAGE=registry-cn-hangzhou.ack.aliyuncs.com/dev/spark:3.5.2 FROM ${SPARK_IMAGE} # Add dependency for Hadoop Aliyun OSS support ADD --chmod=644 https://repo1.maven.org/maven2/org/apache/hadoop/hadoop-aliyun/3.3.4/hadoop-aliyun-3.3.4.jar ${SPARK_HOME}/jars ADD --chmod=644 https://repo1.maven.org/maven2/com/aliyun/oss/aliyun-sdk-oss/3.17.4/aliyun-sdk-oss-3.17.4.jar ${SPARK_HOME}/jars ADD --chmod=644 https://repo1.maven.org/maven2/org/jdom/jdom2/2.0.6.1/jdom2-2.0.6.1.jar ${SPARK_HOME}/jars -
Create a Secret
Create a Secret in your Spark job namespace to store OSS access credentials.
-
Create the following manifest file
spark-oss-secret.yaml:apiVersion: v1 kind: Secret metadata: name: spark-oss-secret namespace: default stringData: # The AccessKey ID of your Alibaba Cloud account. OSS_ACCESS_KEY_ID: "" # The AccessKey Secret of your Alibaba Cloud account. OSS_ACCESS_KEY_SECRET: "" -
Create the Secret:
kubectl apply -f spark-oss-secret.yamlExpected output:
secret/spark-oss-secret created
-
-
Submit the Spark job
Modify the following parameters in the SparkApplication manifest for your environment:
Parameter
Description
Example
imageThe custom Spark container image address.
registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/spark:3.5.2-ossfs.oss.endpointThe OSS endpoint.
oss-cn-beijing-internal.aliyuncs.comspark.eventLog.dirThe log storage path. The path must exist; otherwise the job fails at runtime.
oss://<Bucket name>/spark/spark-events-
Create the following SparkApplication manifest and save it as
spark-pi.yaml.apiVersion: sparkoperator.k8s.io/v1beta2 kind: SparkApplication metadata: name: spark-pi-oss namespace: default spec: type: Scala mode: cluster image: registry-cn-hangzhou.ack.aliyuncs.com/ack-demo/spark:3.5.2-oss mainApplicationFile: local:///opt/spark/examples/jars/spark-examples_2.12-3.5.2.jar mainClass: org.apache.spark.examples.SparkPi sparkVersion: 3.5.2 hadoopConf: fs.AbstractFileSystem.oss.impl: org.apache.hadoop.fs.aliyun.oss.OSS fs.oss.impl: org.apache.hadoop.fs.aliyun.oss.AliyunOSSFileSystem # The OSS endpoint. fs.oss.endpoint: oss-cn-beijing-internal.aliyuncs.com fs.oss.credentials.provider: com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider sparkConf: spark.eventLog.enabled: "true" # The path where logs are stored. spark.eventLog.dir: oss://<Bucket name>/spark/spark-events driver: cores: 1 coreLimit: 1200m memory: 512m serviceAccount: spark-operator-spark envFrom: - secretRef: name: spark-oss-secret executor: instances: 1 cores: 1 coreLimit: 1200m memory: 512m envFrom: - secretRef: name: spark-oss-secret restartPolicy: type: Never -
Submit the Spark job. After the job completes, view its history at http://127.0.0.1:18080.
kubectl apply -f spark-pi.yamlExpected output:
sparkapplication.sparkoperator.k8s.io/spark-pi created
-