<p>I am working on containerizing a bunch of applications that have the following structure at a high level:-</p>

<ol>
<li>Read from a DB/File system</li>
<li>Extract the data or do some parsing (Business logic)</li>
<li>Write back the crunched data to datastore.</li>
</ol>

<p>Let name these 3 steps as s1, s2, and s3.</p>

<p>What is the best way to have a balance between code reuse and making the solution overly complexity? In other terms what is the best design pattern/practice to implement this kind of solutions that are industry-accepted?</p>

<p>Few approached that I could pen own are as follows:-</p>

<ol>
<li>Separate pods for each application with one container each, having s1, s2, and s3 as part of same code.

<ul>
<li><strong>Benefits</strong>: Simple and compact code base. No interprocess/pod communication</li>
<li><strong>Limitation</strong>: No Code reuse</li>
</ul></li>
<li>Separate pods for each application, with each pod having 3 container doing separate functionality as s1, s2, and s3.

<ul>
<li><strong>Benefits</strong>: Code reuse.</li>
<li><strong>Limitation</strong>: Interprocess communication may increase processing latency.</li>
</ul></li>
<li>Separate <strong>group of pods</strong> for s1, s2, and s3 says sg1, sg2, and sg3 respectively running independently. From an application perspective, we create a new pod that talks to the mentioned 3 pod groups to get work done.

<ul>
<li><strong>Benefits</strong>: Code reuse.</li>
<li><strong>Limitation</strong>: Interprocess communication may increase processing latency. Also, maintaining pod groups is an add-on overhead. Increase in complexity</li>
</ul></li>
</ol>

<p>Request to suggest any other alternative if suitable.</p>

Edu Tracker
<p>If your applications are monolithic, the most obvious way is to package each application as a single process in a container image, and deploy the containers to Kubernetes as pods (one container per pod) managed by a Deployment resource (allows you to replicate the pods and do rolling updates). This corresponds to <strong>approach 1</strong> of your list.</p>

<p>If the components of your applications are already loosely coupled, you could go for a microservices type of architecture. For example, you could extract the common logic of <em>s1</em>, <em>s2</em>, and <em>s3</em> for all applications into separate microservices, package each of them as a container image, and run them on Kubernetes as pods (one container per pod, managed by a Deployment). The core of each application would then be its own "microservice", packaged as a container image and deployed to Kubernetes as pods managed by a Deployment. These "core" pods would then use the services provided by the <em>s1</em>, <em>s2</em>, and <em>s3</em> pods as clients. This would correspond to <strong>approach 3</strong> in your list.</p>

<p>Regarding <strong>approach 2</strong>, this isn't a best practice. In most cases, a pod contains only a single container. In some cases, there are multiple containers in a pod, but then one of them is the main container doing the main job and the other ones are tightly coupled sidecar containers that do auxiliary jobs.</p>

<hr>

<p><strong>Summary</strong></p>

<p>If you have a lot of common logic across your applications, then approach 3 makes sense, to avoid code duplication. Also it provides the finest granularity for scaling. Groups of pods are managed by Deployment resources, which you will use anyway, even if you deploy each application as a single pod, so this is no overhead.</p>

<p>If the common logic is not so big, then approach 1 is the simplest solution.</p>
