<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wenyi Xu on Medium]]></title>
        <description><![CDATA[Stories by Wenyi Xu on Medium]]></description>
        <link>https://medium.com/@wenyixu101?source=rss-17da1327ca18------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bR0SEtwRGmu0-0KH-wLL1Q.jpeg</url>
            <title>Stories by Wenyi Xu on Medium</title>
            <link>https://medium.com/@wenyixu101?source=rss-17da1327ca18------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 00:05:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wenyixu101/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploying Spark Application to Google Kubernetes Engine]]></title>
            <link>https://medium.com/@wenyixu101/deploying-spark-application-to-google-kubernetes-engine-8470cc7650b2?source=rss-17da1327ca18------2</link>
            <guid isPermaLink="false">https://medium.com/p/8470cc7650b2</guid>
            <category><![CDATA[spark]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[gcp]]></category>
            <dc:creator><![CDATA[Wenyi Xu]]></dc:creator>
            <pubDate>Wed, 20 Dec 2023 08:58:09 GMT</pubDate>
            <atom:updated>2023-12-20T08:58:09.241Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Welcome to my fist blog where I delve into the world of cloud computing and big data processing by sharing my journey of deploying a Spark application on Google Kubernetes Engine (GKE).</p><p>Spark, renowned for its powerful data processing capabilities, combined with the scalability and robustness of Kubernetes, particularly GKE, offers a fascinating platform for handling large-scale data workloads.</p><p>Throughout this post, I will walk you through the key steps, challenges, and solutions I encountered while setting up my Spark application in the cloud.</p><h3>Process</h3><h4>Environment Preparation</h4><p>To kickstart this project, you’ll need access to a few key resources and tools. Here’s what we’ll use:</p><ol><li>GCP Account: A Google Cloud Platform account is your gateway to accessing various cloud services, including GKE and Cloud Storage. You can <a href="https://cloud.google.com/">sign up here</a> if you don’t have an account yet.</li><li>Google Kubernetes Engine (GKE): GKE is a managed service that makes it easy to run and manage Docker containers in the cloud using Kubernetes. It abstracts away much of the complexity of managing Kubernetes clusters.</li><li>Google Cloud Storage: This is where our data will reside. Cloud Storage offers a secure and scalable object storage solution, perfect for handling big data.</li><li>Helm: Think of Helm as a package manager for Kubernetes. It simplifies the deployment of applications (like Spark) on Kubernetes.</li><li>Docker: Essential for creating containerized applications, Docker packages an application and its dependencies into a container that can run on any Linux machine.</li></ol><h4>Kubernetes Components</h4><p>To create an environment conducive for our Spark application, we’ll utilize several Kubernetes components:</p><ul><li>Namespace: This is like a workspace in Kubernetes. It allows us to isolate our resources from others in the same cluster.</li><li>Role and RoleBinding: These are part of Kubernetes’ Role-Based Access Control (RBAC) system. A Role defines permissions within a namespace, and a RoleBinding grants those permissions to users or processes.</li><li>Service Account: This is an identity for processes running in a pod. It allows our Spark application to interact securely with other services in the Kubernetes cluster.</li></ul><h4>Spark Application</h4><p>To bring these concepts to life, we’ll use a simple yet illustrative example: a word count application:</p><pre>import org.apache.spark.sql.SparkSession<br>import java.lang.Thread.sleep<br><br>object WordCount {<br>  def main(args: Array[String]) {<br>    val inputPath = args(0)<br>    val outputPath = args(1)<br><br>    val spark = SparkSession<br>      .builder<br>      .appName(&quot;Word Count&quot;)<br>      .getOrCreate()<br><br>    import spark.implicits._<br><br>    val textFile = spark.read.text(inputPath).as[String]<br><br>    val words = textFile.flatMap(line =&gt; line.split(&quot;\\W+&quot;))  // Split on non-word characters<br><br>    val wordCounts = words.groupBy(&quot;value&quot;).count()<br><br>    wordCounts<br>      .write<br>      .format(&quot;csv&quot;)<br>      .option(&quot;path&quot;, outputPath)<br>      .save()<br><br><br>    spark.stop()<br>  }<br>}</pre><h4>Integrating Google Cloud Storage with Spark</h4><p>A crucial aspect of our project involves accessing data stored in Google Cloud Storage (GCS) for input and output operations. To enable seamless interaction between our Spark application and GCS, it’s essential to include the gcs-connector artifact as a dependency in our project.</p><pre>&lt;dependency&gt;<br>  &lt;groupId&gt;com.google.cloud.bigdataoss&lt;/groupId&gt;<br>  &lt;artifactId&gt;gcs-connector&lt;/artifactId&gt;<br>  &lt;version&gt;${gcs.connector.version}&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p>When integrating the gcs-connector into a Spark application, a common challenge encountered is version conflicts with Guava, a set of core libraries that Google uses for Java projects. These conflicts typically arise because Spark and gcs-connector might depend on different versions of Guava. To resolve this issue, we can use a shaded jar.</p><pre>&lt;plugin&gt;<br>    &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;<br>    &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;<br>    &lt;version&gt;3.2.4&lt;/version&gt; &lt;!-- Use the latest version --&gt;<br>    &lt;executions&gt;<br>        &lt;execution&gt;<br>            &lt;phase&gt;package&lt;/phase&gt;<br>            &lt;goals&gt;<br>                &lt;goal&gt;shade&lt;/goal&gt;<br>            &lt;/goals&gt;<br>            &lt;configuration&gt;<br>                &lt;relocations&gt;<br>                    &lt;!-- Relocate the Guava classes --&gt;<br>                    &lt;relocation&gt;<br>                        &lt;pattern&gt;com.google.common&lt;/pattern&gt;<br>                        &lt;shadedPattern&gt;shaded.com.google.common&lt;/shadedPattern&gt;<br>                    &lt;/relocation&gt;<br>                &lt;/relocations&gt;<br>                &lt;filters&gt;<br>                    &lt;filter&gt;<br>                        &lt;!-- Exclude signature files to avoid security issues --&gt;<br>                        &lt;artifact&gt;*:*&lt;/artifact&gt;<br>                        &lt;excludes&gt;<br>                            &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;<br>                            &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;<br>                            &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;<br>                        &lt;/excludes&gt;<br>                    &lt;/filter&gt;<br>                &lt;/filters&gt;<br>            &lt;/configuration&gt;<br>        &lt;/execution&gt;<br>    &lt;/executions&gt;<br>&lt;/plugin&gt;</pre><h4>Source Data in GCS</h4><p>Having Google Cloud Storage (GCS) ready is a prerequisite for this project. Before we dive into the Spark application, let’s ensure that our input data is accessible in the cloud. We’ll do this by creating a dedicated GCS bucket and uploading our example input data for the word count application.</p><p>Once your bucket is ready, you can upload the text file that will serve as the input for our word count application:</p><ol><li>Prepare the Text File: Create a text file containing the data you want to process. This could be any text - a poem, a paragraph from a book, or even a series of random words.</li><li>Upload the File: In the GCS console, navigate to your newly created bucket and upload the text file. Alternatively, you can use the gsutil command-line tool:</li></ol><p>gsutil cp path/to/your/inputfile.txt gs://your-bucket-name/</p><p>Replace path/to/your/inputfile.txt with the path to your local file, and your-bucket-name with the name of your GCS bucket.</p><p>After this, you should be able to check your example input file from the GCS UI:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zBaZx4bSicMcuH0XJgQFNg.png" /></figure><h4>Submit Spark Application</h4><p>With our environment configured and our Spark application ready, we can now submit it to run on Google Kubernetes Engine. Below is a sample command to submit the Spark application:</p><pre>spark-submit \<br>  --class $MAIN_CLASS \<br>  --master k8s://$KUBERNETES_API_SERVER_HOST:$KUBERNETES_API_SERVER_PORT \<br>  --deploy-mode cluster \<br>  --driver-cores 1 \<br>  --driver-memory 1g \<br>  --num-executors 1 \<br>  --executor-cores 1 \<br>  --executor-memory 1g \<br>  --name $APP_NAME \<br>  --conf spark.kubernetes.driver.image=$DOCKER_IMAGE \<br>  --conf spark.kubernetes.container.image=$DOCKER_IMAGE \<br>  --conf spark.kubernetes.namespace=$GKE_NAMESPACE \<br>  --conf spark.kubernetes.authenticate.driver.serviceAccountName=$GKE_SA_NAME \<br>  --conf spark.kubernetes.authenticate.executor.serviceAccountName=$GKE_SA_NAME \<br>  --conf spark.kubernetes.file.upload.path=$FILE_UPLOAD_PATH \<br>  --conf spark.kubernetes.driver.label.app=spark \<br>  --conf spark.kubernetes.driver.label.name=$APP_NAME \<br>  --conf &quot;spark.hadoop.fs.gs.impl=com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem&quot; \<br>  --conf &quot;spark.hadoop.fs.AbstractFileSystem.gs.impl=com.google.cloud.hadoop.fs.gcs.GoogleHadoopFS&quot; \<br>  --conf &quot;spark.hadoop.fs.gs.auth.service.account.enable=true&quot; \<br>  local://$APP_JAR \<br>  $input \<br>  $output</pre><ul><li>--class $MAIN_CLASS: Specifies the main class of your application.</li><li>--master k8s://...: The Kubernetes API server where your GKE cluster is running.</li><li>--deploy-mode cluster: Deploys your application in the cluster mode.</li><li>--driver-cores, --driver-memory: The amount of resources allocated to the driver.</li><li>--num-executors, --executor-cores, --executor-memory: Configurations for the executors.</li><li>--name $APP_NAME: The name of your Spark application.</li><li>--conf spark.kubernetes.driver.image=...: The Docker image for the driver.</li><li>--conf spark.kubernetes.container.image=...: The Docker image for executors.</li><li>--conf spark.kubernetes.namespace=...: The Kubernetes namespace to use.</li><li>--conf spark.kubernetes.authenticate.driver.serviceAccountName=...: Service account for the driver.</li><li>--conf spark.kubernetes.authenticate.executor.serviceAccountName=...: Service account for the executors.</li><li>--conf spark.kubernetes.file.upload.path=...: Path for uploading files needed by the job.</li><li>local://$APP_JAR: Path to your application&#39;s jar file.</li><li>$input $output: Arguments to your application, typically input and output paths for processing.</li></ul><h4>What to Expect</h4><p>After submission, Spark deploys your application on GKE, utilizing the resources and configurations specified. You can monitor the progress and performance of your application using GKE and Spark UI tools. Once the job is complete, check the output in the specified Google Cloud Storage location.</p><h3>Reference</h3><p>The full code example could be found here: <a href="https://github.com/xuwenyihust/Data-Platform">https://github.com/xuwenyihust/Data-Platform</a>. Please take a look if you are interested.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8470cc7650b2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>