<p>I would like to be able to limit the amount of jobs of a given &quot;type&quot; that run at the same time (maybe based on their label, e.g. no more than N jobs with label <code>mylabel</code> may run at the same time).</p>
<p>I have a long running computation that requires a license key to run.  I have N license keys and I would like to limit the amount of simultaneously running jobs to N. Here's how I imagine it working: I label the jobs with some special <code>tag</code>.  Then, I schedule N + K jobs, then at most N jobs may be in state &quot;running&quot; and K jobs should be in the queue and may only transition to &quot;running&quot; state when the total number of running jobs labeled <code>mytag</code> is less or equal to N.</p>
<p>[UPDATE]</p>
<ul>
<li>The jobs are independent of each other.</li>
<li>The execution order is not important, although I would like them to be FIFO (time wise).</li>
<li>The jobs are scheduled on user requests. That is, there is no fixed amount of work known in advance that needs to be processed, the requests to run a job with some set of parameters (configuration file) come sporadically in time.</li>
</ul>

<p>Unfortunately there is no built-in feature to do it using labels in k8s. But since your jobs are scheduled based on unpredictable user requests, you can achieve your goal like this:</p>
<ul>
<li>create a new namespace <code>kubectl create namespace quota-pod-ns</code></li>
<li>create a ResourceQuota</li>
</ul>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ResourceQuota
metadata:
  name: pod-max-number
  namespace: quota-pod-ns
spec:
  hard:
    pods: &quot;5&quot;
</code></pre>
<p>This  will limit max number of pods to 5 in the namespace quota-pod-ns.</p>
<ul>
<li>create k8s jobs in the quota-pod-ns namespace.</li>
</ul>
<p>When you want to run the 6th job in that namespace, k8s will try to create the 6th pod and will fail to do that. But once one of the running pods is Completed, job-controller will create that new pod within a max limit.</p>
