Kubernetes: Infinite Workload Using One Pod Per Task

53 Views Asked by At

I’m trying to create something similar to Lambda functions on kubernetes. I need to run each request on a separate pod. Once the container terminates successfully, the response is returned and the pod should be deleted, and a new one should be created. I want to reduce the latency of start-up time, so I want to create the pods in advance and let them wait for requests. Scalability isn’t a concern at the moment, I’m fine with having only 5 pods listening in parallel at any given time.

I thought about using a deployment controller but it doesn’t allow “restartPolicy: never”, which means that after a pod terminates it will be restarted rather than recycled. Then I found out about Jobs, and I was able to achieve the desired behavior by creating a job with finite completions, and a parallelism of 5. The problem is the finite completions part. If I change completions to “null” (infinite), the job will stop creating new pods after one pod terminates successfully. So, essentially, I’m trying to use the logic of a finite Job controller but for an infinite number of completions.

  • is there a better way to do it than using kubernetes jobs?
  • is there a way to reset the completions of a job periodically to achieve the desired behavior with my current implementation?
  • is there a way to create a job with infinite completions where terminated pods are constantly replaced?

I have seen other s/o posts with similar requirements where it was recommended to use a job with infinite completions and make the pods terminate with error on purpose to make the job replace them with new ones, but I don’t think it’s a good idea, especially monitoring wise.

Thanks for your help.

1

There are 1 best solutions below

0
Harsh Manvar On

You have a specific requirement to answer the first question based on considering similar to Lambda.

Can checkout the Kubeless (No more managed by VMware) & Knative (CNCF project) if it fits your requirements.

If the above does not solve your exact issue you can fall back to your suggested approach of using Jobs.

There is always the option to extend the behavior of existing K8s resources with Custom Controller/CRD in K8s.

Idea is to extend the behavior of with Controller like here doing Daemon Job, i wanted to schedule JOB pod it on all available Nodes and one complete POD should be removed unless be there in restart/complete state. My Example Daemonset Job article - GitHub Code

To write the Controller/Operator you have multiple option