Does Kubernetes provide a colocated Job container?

2/6/2017

I wonder how would one implement a colocated auxiliary container in a Pod within a Deployment which does not provide a service but rather a job/batch workload?

Background of my questions is, that I want to deploy a scalable service at which each instance needs configuration after its start. This configuration is done via a HTTP POST to its local colocated service instance. I've implemented a auxiliary container for this in order to benefit from the feature of colocation. So the auxiliary container always knows which instance needs to be configured.

Problem is, that the restartPolicy needs to be defined at the Pod level. I am looking for something like restart policy always for the service and a different restart policy onFailurefor the configuration job.

I know that k8s provides the Job resource for such workloads. But is there an option to colocate those jobs to Pods?

Furthermore I've stumbled across the so called init containers which might be defined via annotations. But these suffer the drawback, that k8s ensures that the actual Pod is only started after the init container did run. So for my very scenario it seems unsuitable.

-- Fabian Dörk
google-cloud-platform
google-kubernetes-engine
kubernetes

1 Answer

2/8/2017

As I understand you need your service running to configure it.

Your solution is workable and you can set restartPolicy: always you just need a way to tell your one off configuration container that it already ran. You could create and attach an emptyDir volume to your configuration container, create a file on it to mark your configuration successful and check for this file from your process. After your initialization you enter sleep in a loop. The downside is that some resources will be taken up by that container too.

Or you can just add an extra process in the same container and do the configuration (maybe with the file mentioned above as a guard to avoid configuring twice). So write a simple shell script like this and run it instead of your main process:

#!/bin/sh

(
  [ -f /mnt/guard-vol/stamp ] && exit 0
  /opt/my-config-process parameters && touch /mnt/guard-vol/stamp
) &

exec /opt/my-main-process "$@"

Alternatively you could implement a separate pod that queries the kubernetes API for pods of your service with label configured=false. Configure it and remove the label with the API. You should also modify your Service to select configured=true pods.

-- Janos Lenart
Source: StackOverflow