<p>I previously created a Flask server that spawns Docker containers using the Docker Python SDK. When a client hits a specific endpoint, the server would generate a container. It would maintain queues, and it would be able to kill containers that didn't respond to requests. </p>

<p>I want to migrate towards Kubernetes, but I am starting to think my current server won't be able to "spawn" jobs as pods automatically like in docker. </p>

<pre><code>docker.from_env().containers.run('alpine', 'echo hello world')  
</code></pre>

<p>Is Docker Swarm a better solution for this, or is there a hidden practice that is done in Kubernetes? Would the Kubernetes Python API be a logical solution for automatically generating pods and jobs, where the Flask server is a pod that manages other pods within the cluster? </p>

<p>'Kubectl run' is much like 'docker run' in that it will create a Pod with a container based on a docker image (e.g. <a href="https://stackoverflow.com/questions/34601650/how-do-i-run-curl-command-from-within-a-kubernetes-pod">How do i run curl command from within a Kubernetes pod</a>). See <a href="https://kubernetes.io/docs/reference/kubectl/docker-cli-to-kubectl/" rel="nofollow noreferrer">https://kubernetes.io/docs/reference/kubectl/docker-cli-to-kubectl/</a> for more comparison. But what you run with k8s are Pods/Jobs that contain containers rather than running containers directly so this will add an extra layer of complexity for you.</p>

<p>Kubernetes is more about orchestrating services rather than running short-lived jobs. It has some features and can be used to run jobs but that isn't its central focus. If you're going in that direction you may want to look at knative (and knative build) or kubeless as what you describe sounds rather like the serverless concept. Or if you are thinking more about Jobs then perhaps brigade (<a href="https://brigade.sh" rel="nofollow noreferrer">https://brigade.sh</a>). (For more see <a href="https://www.quora.com/Is-Kubernetes-suited-for-long-running-batch-jobs" rel="nofollow noreferrer">https://www.quora.com/Is-Kubernetes-suited-for-long-running-batch-jobs</a>) If you are looking to run web app workloads that serve requests then note that you don't need to kill containers that fail to respond on k8s as k8s will monitor and restart them for you. </p>

<p>I don't know swarm well enough to compare. I suspect it would be a bit easier for you as it is aimed more centrally at docker (the k8s API is intended to support other runtimes) but perhaps somebody else can comment on that. Whether using swarm instead helps you will I guess depend on your motivations. </p>
