title: Spark on Kubernetes, pods crashing abruptly
body: <p>Below is the scenario being tested,</p>
  
  <p>Job :
  Spark SQK job is written in Scala, and to run on 1TB TPCDS BENCHMARK DATA which is in parquet,snappy format and hive tables created on top of it.</p>
  
  <p>Cluster manager :
  Kubernetes</p>
  
  <p>Spark sql configuration :</p>
  
  <p>Set 1 :</p>
  
  <pre><code>spark.executor.heartbeatInterval 20s
  spark.executor.cores 4
  spark.driver.cores 4
  spark.driver.memory 15g
  spark.executor.memory 15g
  spark.cores.max 220
  spark.rpc.numRetries 5
  spark.rpc.retry.wait 5
  spark.network.timeout 1800
  spark.sql.broadcastTimeout 1200
  spark.sql.crossJoin.enabled true
  spark.sql.starJoinOptimization true
  spark.eventLog.enabled true
  spark.eventLog.dir hdfs://namenodeHA/tmp/spark-history
  spark.sql.codegen true
  spark.kubernetes.allocation.batch.size 30
  </code></pre>
  
  <p>Set 2 :</p>
  
  <pre><code>spark.executor.heartbeatInterval 20s
  spark.executor.cores 4
  spark.driver.cores 4
  spark.driver.memory 11g
  spark.driver.memoryOverhead 4g
  spark.executor.memory 11g
  spark.executor.memoryOverhead 4g
  spark.cores.max 220
  spark.rpc.numRetries 5
  spark.rpc.retry.wait 5
  spark.network.timeout 1800
  spark.sql.broadcastTimeout 1200
  spark.sql.crossJoin.enabled true
  spark.sql.starJoinOptimization true
  spark.eventLog.enabled true
  spark.eventLog.dir hdfs://namenodeHA/tmp/spark-history
  spark.sql.codegen true
  spark.kubernetes.allocation.batch.size 30
  </code></pre>
  
  <p>Kryoserialiser is being used and with <code>spark.kryoserializer.buffer.mb</code> value of 64mb.
  50 executors are being spawned using spark.executor.instances=50 submit argument.</p>
  
  <p>Issues Observed:</p>
  
  <p>Spark SQL job is terminating abruptly and the drivers,executors are being killed randomly.
  driver and executors pods gets killed suddenly the job fails.</p>
  
  <p>Few different stack traces are found across different runs,</p>
  
  <p>Stack Trace 1:</p>
  
  <pre><code>"2018-05-10 06:31:28 ERROR ContextCleaner:91 - Error cleaning broadcast 136
  org.apache.spark.SparkException: Exception thrown in awaitResult:
          at org.apache.spark.util.ThreadUtils$.awaitResult(ThreadUtils.scala:205)"
  </code></pre>
  
  <p>File attached : StackTrace1.txt</p>
  
  <p>Stack Trace 2: </p>
  
  <pre><code>"org.apache.spark.shuffle.FetchFailedException: Failed to connect to /192.178.1.105:38039^M
          at org.apache.spark.storage.ShuffleBlockFetcherIterator.throwFetchFailedException(ShuffleBlockFetcherIterator.scala:442)^M
          at org.apache.spark.storage.ShuffleBlockFetcherIterator.next(ShuffleBlockFetcherIterator.scala:418)"
  </code></pre>
  
  <p>File attached : StackTrace2.txt</p>
  
  <p>Stack Trace 3:</p>
  
  <pre><code>"18/05/10 11:21:17 WARN KubernetesTaskSetManager: Lost task 3.0 in stage 48.0 (TID 16486, 192.178.1.35, executor 41): FetchFailed(null, shuffleId=29, mapId=-1, reduceId=3, message=^M
  org.apache.spark.shuffle.MetadataFetchFailedException: Missing an output location for shuffle 29^M
          at org.apache.spark.MapOutputTracker$$anonfun$org$apache$spark$MapOutputTracker$$convertMapStatuses$2.apply(MapOutputTracker.scala:697)^M
          at org.apache.spark.MapOutputTracker$$anonfun$org$apache$spark$MapOutputTracker$$convertMapStatuses$2.apply(MapOutputTracker.scala:693)"
  </code></pre>
  
  <p>File attached : StackTrace3.txt</p>
  
  <p>Stack Trace 4:</p>
  
  <pre><code>"ERROR KubernetesTaskSchedulerImpl: Lost executor 11 on 192.178.1.123: Executor lost for unknown reasons."
  </code></pre>
  
  <p>This is repeating constantly until the executors are dead completely without any stack traces.</p>
  
  <p>Also, we see 18/05/11 07:23:23 INFO DAGScheduler: failed: Set()
  what does this mean ? anything is wrong or it says failed set is empty that means no failure ?</p>
  
  <p>Observations or changes tried out :
  -  Monitored memory and CPU utilization across executors and none of them are hitting the limits.</p>
  
  <ul>
  <li><p>As per few readings and suggestions 
  <code>spark.network.timeout</code> was increased to 1800 from 600, but did not help.</p></li>
  <li><p>Also, driver and executor memory overhead was kept default in set 1 of the config and it was 0.1*15g=1.5gb.
  Increased this value also, explicitly to 4gb and reduced driver and executor memory values to 11gb from 15gb as per set 2.
  this did not yield any valuable results, same failures are being observed.</p></li>
  </ul>
  
  <p>Spark SQL is being used to run the queries,
  sample code lines :</p>
  
  <pre><code>val qresult = spark.sql(q)
      qresult.show()
  </code></pre>
  
  <p>No manual repartitioning is being done in the code.</p>