How to handle recurring short-lived tasks with Kubernetes

11/22/2016

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

The webserver has its own kubernetes deployment + service.

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

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

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

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

-- pkyeck
kubernetes

2 Answers

11/22/2016

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

Option 1: The static files represent built source code

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.

Option 2: The static files represent state

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.

I believe option 1 to be better for 2 reasons:

  1. 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).
  2. 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.
-- iamnat
Source: StackOverflow

11/23/2016

Jobs, Init containers, or alternatively a gitRepo type of Volume would work for you.

http://kubernetes.io/docs/user-guide/volumes/#gitrepo

It is not clear in your question why you want to update the static content without simply re-deploying / updating the Pod.

Since somewhere, somehow, you have to build the webserver Docker image, it seems best to build the static content into the image: no moving parts once deployed, no need for volumes or storage. Overall it is simpler.

If you use any kind of automation tool for Docker builds, it's easy. I personally use Jenkins to build Docker images based on a hook from git repo, and the image is simply rebuilt and deployed whenever the code changes.

Running a Job or Init container doesn't gain you much: sure the web server keeps running, but it's as easy to have a Deployment with rolling updates which will deploy the new Pod before the old one is torn down and you server will always be up too.

Keep it simple...

-- MrE
Source: StackOverflow