๋กœ์ปฌ์—์„œ Spark ํด๋Ÿฌ์Šคํ„ฐ ๊ตฌ์ถ•ํ•˜๊ธฐ โœŒ๏ธ Master Node๊ฐ€ Cluster Manager๊ฐ€ ๋˜๋Š” Standalon ๋ชจ๋“œ. Spark Application์„ ์‹คํ–‰ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•: Client ๋ชจ๋“œ, Cluster ๋ชจ๋“œ. Master vs. Worker ๊ทธ๋ฆฌ๊ณ  Driver vs. Executor

7 minute read

Databricks Certification ์ทจ๋“์„ ๋ชฉํ‘œ๋กœ Apache Spark๋ฅผ โ€œ์ œ๋Œ€๋กœโ€ ๊ณต๋ถ€ํ•ด๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํšŒ์‚ฌ์—์„  Databricks Unity Catalog๋ฅผ ๋„์ž…ํ•˜๋ ค๊ณ  ๋ถ„ํˆฌํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”. Spark์™€ ์ข€ ์นœํ•ด์งˆ ์ˆ˜ ์žˆ์„๊นŒ์š”? ๐ŸŽ‡ ์ „์ฒด ํฌ์ŠคํŠธ๋Š” Development - Spark์—์„œ ํ™•์ธํ•ด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

1ํŽธ์—์„œ๋Š” ๋‹จ์ผ ๋จธ์‹ ์—์„œ ์‹คํ–‰ํ•˜๋Š” โ€œLocal Modeโ€๋กœ Spark ์ž‘์—…์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜๋‹ค. 2ํŽธ์—์„œ๋Š” โ€œClient Modeโ€๋กœ Spark ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ , Spark ์ž‘์—…์„ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์ž.

Setup Spark Cluster

Setup Master Node

๋จผ์ € Master ๋…ธ๋“œ๋ฅผ ๋„์›Œ๋ณด์ž. pyspark๋กœ Spark๋ฅผ ์„ธํŒ…ํ–ˆ๋‹ค๋ฉด, ํ•จ๊ป˜ ์ œ๊ณตํ•˜๋Š” spark-class ๋ช…๋ น์–ด๋กœ Master ๋…ธ๋“œ๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ๋‹ค.

$ spark-class org.apache.spark.deploy.master.Master -h localhost
...
24/08/22 00:52:41 INFO Utils: Successfully started service 'sparkMaster' on port 7077.
24/08/22 00:52:41 INFO Master: Starting Spark master at spark://localhost:7077
24/08/22 00:52:41 INFO Master: Running Spark version 3.5.2
24/08/22 00:52:41 INFO JettyUtils: Start Jetty 0.0.0.0:8080 for MasterUI
24/08/22 00:52:41 INFO Utils: Successfully started service 'MasterUI' on port 8080.
24/08/22 00:52:41 INFO MasterWebUI: Bound MasterWebUI to 0.0.0.0, and started at http://172.30.1.16:8080
24/08/22 00:52:41 INFO Master: I have been elected leader! New state: ALIVE

๊ทธ๋ฆฌ๊ณ  http://localhost:8080์— ์ ‘์†ํ•˜๋ฉด, Spark UI๋„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

Master ๋…ธ๋“œ๋งŒ ๋„์šด ์ƒํƒœ

์ผ๋‹จ Master ๋…ธ๋“œ๋งŒ ๋„์šด ์ƒํƒœ์—์„œ ์š”๊ธฐ์— spark-hello.py ์ž‘์—…์„ spark-submit ํ•ด๋ณด์ž.

$ spark-submit \
  --master spark://localhost:7077 \
  ./hello-spark.py

์ด ์ƒํƒœ์—์„  Spark Application์€ ๋งŒ๋“ค์–ด์ง„๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ Spark App์„ ์‹คํ–‰ํ•  ์›Œ์ปค๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์‹คํ–‰์ด ๋˜์ง€ ์•Š๊ณ  ๊ณ„์† ๊ธฐ๋‹ค๋ฆฌ๊ธฐ๋งŒ ํ•œ๋‹ค.

WARN TaskSchedulerImpl: Initial job has not accepted any resources; check your cluster UI to ensure that workers are registered and have sufficient resources

์ฆ‰, Worker๊ฐ€ ์•„์ง ๋ถ™์ง€ ์•Š์•„์„œ Spark App์ด ์‹คํ–‰๋˜์ง€ ์•Š๊ณ  ์žˆ์—ˆ๋‹ค! ๊ทธ๋Ÿฌ๋ฉด ์ด์ œ ์›Œ์ปค ๋…ธ๋“œ๋ฅผ ๋ถ™์—ฌ๋ณด์ž.

Setup Worker Node

$ spark-class org.apache.spark.deploy.worker.Worker \
    spark://localhost:7077
---
...
24/08/22 13:00:57 INFO JettyUtils: Start Jetty 0.0.0.0:8081 for WorkerUI
24/08/22 13:00:57 INFO Utils: Successfully started service 'WorkerUI' on port 8081.
24/08/22 13:00:57 INFO WorkerWebUI: Bound WorkerWebUI to 0.0.0.0, and started at http://172.16.80.54:8081
24/08/22 13:00:57 INFO Worker: Connecting to master localhost:7077...
24/08/22 13:00:57 INFO TransportClientFactory: Successfully created connection to localhost/127.0.0.1:7077 after 11 ms (0 ms spent in bootstraps)
24/08/22 13:00:57 INFO Worker: Successfully registered with master spark://localhost:7077

Master ๋…ธ๋“œ์— Worker ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜ ๋ถ™์ธ ์ƒํƒœ

์ฐธ๊ณ ๋กœ Worker ๋…ธ๋“œ ์ž์ฒด์˜ Spark UI๋„ ์žˆ๋Š”๋ฐ, ์š”๊ฑด http://localhost:8081์—์„œ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋‹ค.

spark-submit again

์ด์ œ Master ๋…ธ๋“œ์™€ Worker ๋…ธ๋“œ ๋‘˜ ๋‹ค ๊ตฌ์„ฑ์„ ํ–ˆ์œผ๋‹ˆ, ๋‹ค์‹œ spark-submit์„ ํ•ด๋ณด์ž.

$ spark-submit \
  --master spark://localhost:7077 \
  ./hello-spark.py

spark-submit์„ ์‹คํ–‰ ํ–ˆ์„ ๋•Œ์˜ Master ๋…ธ๋“œ์˜ Spark UI

Spark App์ด ์‹คํ–‰๋˜๊ณ , Completed ์ƒํƒœ๋กœ ๋„˜์–ด๊ฐ„ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Worker ๋…ธ๋“œ์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ๋‚˜์˜จ๋‹ค.

spark-submit์„ ์‹คํ–‰ ํ–ˆ์„ ๋•Œ์˜ Worker ๋…ธ๋“œ์˜ Spark UI


Spark UI๋ฅผ ํ†ตํ•ด ์ง€ํ‘œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด, ๊ฐ„๋‹จํ•œ count ์ž‘์–ธ์ธ๋ฐ๋„ CPU core๋ฅผ ๋ฌด๋ ค 12 core๋‚˜ ์‚ฌ์šฉํ–ˆ๋‹ค! ์ด๊ฒƒ์€ spark-submit์„ ํ•  ๋•Œ, ๋”ฐ๋กœ ์‚ฌ์šฉํ•  Core์™€ Memory ์šฉ๋Ÿ‰์„ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

$ spark-submit \
  --master spark://localhost:7077 \
  --total-executor-cores 2 \
  --executor-memory 500m \
  ./hello-spark.py

Core ์ˆ˜์™€ Memory ์ˆ˜๋ฅผ ์กฐ์ •ํ•ด spark-submit์„ ์‹คํ–‰ํ•œ ๋ชจ์Šต

Spark Standalone ๋ชจ๋“œ

Master-Worker ๋…ธ๋“œ๋กœ Spark ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์ถ•ํ•œ ์ด ๋ฐฉ์‹์„ โ€œStandaloneโ€ ๋ชจ๋“œ๋ผ๊ณ  ํ•œ๋‹ค.

๋ณธ๋ž˜ Spark ํด๋Ÿฌ์Šคํ„ฐ๋Š” Master ๋…ธ๋“œ, Worker ๋…ธ๋“œ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ โ€œCluster Managerโ€๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ์–ด Spark ํด๋Ÿฌ์Šคํ„ฐ ์ƒํƒœ ๊ด€๋ฆฌ, ๋ฆฌ์†Œ์Šค ํ• ๋‹น, ์ž‘์—… ๋ถ„๋ฐฐ ๋“ฑ์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  • Memory Management
  • Fault Recovery
  • Task Scheduling

Standalone์˜ ๋œป์ธ โ€œ์ž๋ฆฝํ˜•/๋…๋ฆฝํ˜•โ€๋ผ๋Š” ๋œป์— ๋งž๊ฒŒ Standalone ๋ชจ๋“œ์—์„œ๋Š” Master ๋…ธ๋“œ๊ฐ€ Cluster Manager์˜ ์—ญํ• ๊นŒ์ง€ ์ˆ˜ํ–‰ํ•œ๋‹ค.

Spark ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ Prod ํ™˜๊ฒฝ์—์„œ ์šด์˜ํ•  ๋•Œ๋Š” YARN์ด๋‚˜ Mesos๋ฅผ Cluster Manager๋กœ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. spark-on-kubernetes๋กœ ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์šด์˜ํ•œ๋‹ค๋ฉด, Kubernetes๊ฐ€ Cluster Manager์˜ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ๋„ ํ•œ๋‹ค.

Client Mode์— ๋Œ€ํ•ด ์ข€๋” ์ž์„ธํžˆ

Spark Client Mode

์ง€๋‚œ ํฌ์ŠคํŠธ์—์„œ๋Š” โ€œLocal Modeโ€๋กœ Spark Application์„ ์‹คํ–‰ํ–ˆ๋‹ค๋ฉด, ์ด๋ฒˆ์—๋Š” โ€œClient Modeโ€๋กœ Spark App์„ ์‹คํ–‰ํ–ˆ๋‹ค. Client ๋ชจ๋“œ์—์„œ๋Š” Driver Process๊ฐ€ Client JVM ์œ„์—์„œ ์‹คํ–‰๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Worker JVM ์œ„์—์„œ Executor Process๋“ค์ด ์‹คํ–‰๋˜๋Š” ๊ตฌ์กฐ๋‹ค.

Master and Worker, Driver and Executor

์ด ๋ถ€๋ถ„์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์–ด๋–จ ๋•Œ๋Š” Master-Worker ๋˜๋Š” Driver-Worker๋ผ๊ณ  ๋ถ€๋ฅด๊ณ , ๋˜ ์–ด๋–จ ๋•Œ๋Š” Driver-Executor๋ผ๊ณ  ๋ถ€๋ฅด๋Š” ์šฉ์–ด๋“ค์ด ์ •๋ง ํ—ท๊ฐˆ๋ ธ๋‹ค.

๊ทธ๋ž˜์„œ ์ฐพ์•„๋ณด๋‹ˆ ์ด๋ ‡๊ฒŒ ์„ค๋ช…ํ•˜๋Š” ๊ณณ์ด ์žˆ์—ˆ๋‹ค.

  • Spark ํด๋Ÿฌ์Šคํ„ฐ ๊ด€์ ์—์„œ๋Š”
    • Master ๋…ธ๋“œ์™€ Worker ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
    • ๊ฐ ๋…ธ๋“œ๋Š” ๋ฌผ๋ฆฌ์  ๋จธ์‹ ์˜ ๊ฐœ๋…์ด๋‹ค.
  • Spark์˜ ๊ด€์ ์—์„œ๋Š”
    • Driver Process์™€ Executor Progress๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
    • Process ๋Œ€์‹  Program๋ผ๊ณ ๋„ ๋งํ•œ๋‹ค.

์ด๋•Œ, Driver Process๋Š” ๋ฐ˜๋“œ์‹œ Master ๋…ธ๋“œ์— ๋œจ๋Š” ๊ฑด ์•„๋‹ˆ๋‹ค. ์•ž์—์„œ ์‚ดํŽด๋ณธ Client Mode๋Š” Driver Process๊ฐ€ Client์˜ JVM์— ๋œฌ๋‹ค. ๋ฐ˜๋ฉด์—, ์•„์ง ์‹คํ—˜ํ•ด๋ณด์ง€ ์•Š์€ โ€œCluster Modeโ€์—์„œ๋Š” Driver Process๊ฐ€ ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€์—์„œ ๊ฒฐ์ •๋œ๋‹ค. (์ด๊ฒƒ๋„ Master ๋…ธ๋“œ์— ๋ฐ˜๋“œ์‹œ ๋œจ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ Worker ๋…ธ๋“œ ์ค‘ ํ•˜๋‚˜์— Driver Process๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๐Ÿ˜ตโ€๐Ÿ’ซ)

Spark Cluster Mode

์œ„์˜ Client Mode์—์„œ์˜ ๊ทธ๋ฆผ๊ณผ ์ž˜ ๋น„๊ตํ•ด๋ณด๋ฉด, Client JVM์ธ์ง€ Driver JVM์ธ์ง€ ํ‘œ๊ธฐ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋˜์–ด ์žˆ๋‹ค. ์•„๋ž˜๋Š” Spark ๊ณต์‹ ๋ฌธ์„œ์—์„œ ๋ฌ˜์‚ฌ๋œ Client ๋ชจ๋“œ์™€ Cluster ๋ชจ๋“œ์˜ ์ฐจ์ด์ ์ด๋‹ค.

Spark Deploy Mode: Distinguishes where the driver process runs. In โ€œclusterโ€ mode, the framework launches the driver inside of the cluster. In โ€œclientโ€ mode, the submitter launches the driver outside of the cluster. - Spark Doc

๋‹ค์Œ์—๋Š”

์•„์ง โ€œCluster Modeโ€๋ฅผ ์ œ๋Œ€๋กœ ์‚ดํŽด๋ณด์ง€ ๋ชป ํ–ˆ๋‹ค. ์‹คํ—˜์€ ํ•ด๋ดค๋Š”๋ฐ, ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋œจ๋ฉด, Cluster Mode์—์„œ๋Š” pyspark๋ฅผ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์—†๋‹ค๊ณ  ํ•œ๋‹ค.

Exception in thread โ€œmainโ€ org.apache.spark.SparkException: Cluster deploy mode is currently not supported for python applications on standalone clusters.

๊ทธ๋ž˜์„œ Cluster Mode๋Š” ๋‹ค์Œ์— scala ์‹คํ—˜์„ ํ•˜๊ฒŒ ๋  ๋•Œ ๋‹ค์‹œ ์‚ดํŽด๋ณผ ๊ฒƒ!


๋‹ค์Œ ํฌ์ŠคํŠธ์—์„  SparkSession๊ณผ SparkContext๋ฅผ ์‚ดํŽด๋ณด๊ณ  ๋‘˜์„ ๋น„๊ต ํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

๐Ÿ‘‰ Jump into Spark Sessions

Categories:

Updated: