<p>I have a setup with a webserver (NGINX) and a react-based frontend that uses webpack to build the final static sources.</p>

<p>The webserver has its own kubernetes <code>deployment</code> + <code>service</code>.</p>

<p>The frontend needs to be build before the webserver can serve the static html/js/css files - but after that, the <code>pod</code>/<code>container</code> can stop.</p>

<p>My idea was to share a <code>volume</code> between the webserver and the frontend <code>pod</code>. The frontend will write the generated files to the <code>volume</code> and the webserver can serve them from there. Whenever there is an update to the frontend sourcecode, the files need to be regenerated.</p>

<p>What is the best way to accomplish that using kubernetes tools?
Right now, I'm using a <code>init-container</code> to build - but this leads to a restart of the webserver <code>pod</code> as well, which wouldn't be neccessary.</p>

<p>Is this the best/only solution to this problem or should I use kubernetes' <code>jobs</code> for this kind of tasks?</p>

<p>There are multiple ways to do this. Here's how I think about this:</p>

<p><strong>Option 1: The static files represent built source code</strong></p>

<p>In this case, the static files that you want to serve should actually be packaged and built into the docker image of your nginx webserver (in the html directory say). When you want to update your frontend, you update the version of the image used and update the pod.</p>

<p><strong>Option 2: The static files represent state</strong></p>

<p>In this case, your approach is correct. Your 'state' (like a database) is stored in a folder. You then run an init container/job to initialise 'state' and then your webserver pod works fine.</p>

<p>I believe option 1 to be better for 2 reasons:</p>

<ol>
<li>You can horizontally scale your webserver trivially by increasing the pod replica number. In option 2, you're actually dealing with state so that's a problem when you want to add more nodes to your underlying k8s cluster (you'll have to copy files/folders from one volume/folder to another).</li>
<li>The static files are actually the source code of your app. These are not uploaded media files or similar. In this case, it absolutely makes sense to make them a part of your docker image. Otherwise, it kind of defeats that advantage of containerising and deploying.</li>
</ol>
