<p>Right now I have a Python Application which runs 50 threads to process data.  It takes an xlsx file and will process a list of values, and will output a simple csv.</p>

<p>I said to myself, since this is a simple Python App with 50 threads, How can I create a cluster to distribute data-processing even more?  FOR EXAMPLE:  Have each Worker node process a subset given to it by the master.   Well that sounds easy, just take the master app slice up the dataset generated and then push it to the workers with load balancing.</p>

<p>How do I get the results though?  I would want to take all results (out.csv in this case) and return them to the master and merge them to create 1 master_out.csv</p>

<p>At first I was thinking a Docker swarm, but no one i know uses them, everything beyond a simple docker container is offloaded to K8.</p>

<p>Right now, i have a simple file structure:</p>

<pre><code>app/
  __init__.py (everything is in this file)
  dataset.xlxs
  out.csv
</code></pre>

<p>I was thinking to create a docker image so that way I could move this app into the image, update/upgrade, install python3 if it isnt already, and then just run this application.</p>

<p>I started getting deeper into processing, and realized that there is likely some built in ways to handle this.  create a flask app to handle ingestion, and then a flask app on master to accept files at completion, etc.... But then master needs to know all the workers etc.</p>

<ul>
<li>I was thinking to create a cluster.</li>
<li>Master node has access to a volume which contains the file i need to process.</li>
<li>Load balancing pushes parts of each file ( ROWS / NUM_WORKERS) to each node.</li>
<li>After WORKERS FINISH, Master Aggregates the resulting csv files to make a master file.</li>
<li>Master_OUT.csv will exist in the folder for consumption.</li>
</ul>

<p>So the cluster would turn on and when ready will run everything, then tare down at the end. Since they want the cluster to likely be distributed, I am not sure how that would work though as processing has IP Address limitations.  It seems like this will not work on a local cluster because to machines being used to reference will hit a cloudflare (or similar) wall after enough requests, so im trying to think of a UNIQUE IP Solution.</p>

<p>I have an idea for architecture, but im not sure if i should create a dockerfile for this, and then figure out the way kube can handle all of this for me.  Though i think in the kube config files we can put remote aws instance login creds so it will spin up all the remote servers.</p>

<p>While I have been doing some stuff with Swarms, It seems that kube is where the real work is done, as swarms seem to be better suited for other things.</p>

<p>Im trying to think of how I would approach this from a kube (or swarm) perspective.</p>

<p>Given the information, this concept reminds me less of load balancing because of the data aggregation and more of like Kubeflow, where you create a CLOUD specifically for ML, but instead of ML it would be ANY distributed processing.</p>

Fallenreaper
<p>The interesting problems in this question have nothing to do with Docker; let's put that aside for now.</p>

<p>You expect you'll have a bunch of computers that are all processing a chunk of this big data set.  You've already structured the problem so that you can do work on small pieces of the input and produce small pieces of the output.  The main problems you need to design around are:</p>

<ol>
<li>Where do you keep the input so that the tasks can read it, if they need to?</li>
<li>How do you pass on units of work to the workers?  What happens if a worker fails?</li>
<li>How do you communicate the outputs?  Where do you store them?  Do they need to be in the same order as the input?</li>
</ol>

<p>A useful tool here is a work queue; <a href="https://www.rabbitmq.com" rel="nofollow noreferrer">RabbitMQ</a> is a popular open-source implementation.  You'd run this as a separate server, and workers can connect to it and read and write messages from queues.  So long as everyone can contact the RabbitMQ server, none of the individual workers or other processes in the system actually need to know about each other.</p>

<p>For some scales of problem, a straightforward approach is to say the original input and final output is single files on a single system.  You break this up into pieces that are small enough that they can fit in a message payload, and the responses also fit in message payloads.  Run one process to read the input and populate the work queues; run some number of workers, and run a process to read back the outputs.</p>

<pre><code>Input handler      +------+ --&gt; worker --&gt; +------+
dataset.xlsx  ---&gt; +------+ --&gt; worker --&gt; +------+ --&gt; Output handler
                   +------+ --&gt; worker --&gt; +------+     out.csv
                   +  ... +      ...       + ...  +
</code></pre>

<p>If you're using Python as an implementation language, also consider <a href="http://www.celeryproject.org" rel="nofollow noreferrer">Celery</a> as a framework to manage this.</p>

<p>To run this, you need to run three separate processes.</p>

<pre class="lang-sh prettyprint-override"><code>export RABBITMQ_HOST=localhost RABBITMQ_PORT=5672
./input_handler.py dataset.xlsx
./output_handler.py out.csv
./worker.py
</code></pre>

<p>You can run multiple workers; RabbitMQ will take care of ensuring that tasks get distributed across the workers, and that a task gets retried if a worker fails.  There's no particular requirement that all of these run on the same host, so long as they can all reach the RabbitMQ broker.</p>

<p>If you can't keep the inputs or outputs in the message, you'll need some sort of shared storage that all of the nodes can reach.  If you're in a cloud environment an object-store service like Amazon's S3 is a popular choice.  In the input and output messages you would then put the path of the relevant file in S3 instead of the data.</p>

<p>How would Docker or Kubernetes fit into this picture?  It's important to note that neither technology provides anything like a work queue, and shared filesystems can be spotty.  Still, where I referred to the three different processes above, you could package those into three Docker images, and you could deploy those in Kubernetes.  Where I said you don't have to run just one worker, a Kubernetes Deployment will let you run 5 or 10 or 50 identical copies of the worker, and RabbitMQ will take responsibility for making sure they all have work to do.</p>
