<p>For every 10K new users, we need to spawn a new application that processes that new set of users, based on some <code>env</code> variable (i.e. <code>offset: n</code>)</p>
<p>I tried using <code>docker-compose</code>, but it looks like I manually have to create <code>yml</code> processes for each container, which does not help much.</p>
<p>Can I use k8s, or some other tool, so that I can dynamically deploy an application with a dynamically set <code>env</code> property, so that my application will know which block of users to serve?</p>
<p>Ideally, I would like to have a master node (could be a lambda function, or an EC2 instance) that based on some registration event figures out if a new instance needs to be deployed, and it notifies the orchestrator to build a new deployment with some specific <code>env</code> variable.</p>

AlexDev
<p>A Kubernetes <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a> can do something similar to this.  Each replica of the StatefulSet has a unique hostname, of the form <code>name-0</code>, <code>name-1</code>, and so on (guaranteed to begin with 0 and to be in order).  You could then find this in something like an entrypoint wrapper script:</p>
<pre class="lang-bash prettyprint-override"><code>#!/bin/sh
# (run this script as your image's ENTRYPOINT)

if [ -z &quot;$OFFSET&quot; ]; then
  OFFSET=$(hostname | sed -ne 's/.*-\([0-9][0-9]*\)$/\1/p')
fi
if [ -z &quot;$OFFSET&quot; ]; then
  echo could not determine shard offset &gt;&amp;2
  exit 1
fi

exec &quot;$@&quot;
</code></pre>
<p>A StatefulSet comes with other properties which you may or may not want; for example, updating an <code>image:</code> will cause the underlying Pods to be recreated in a very specific order.</p>
<p>A StatefulSet does include a way to <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#stable-network-id" rel="nofollow noreferrer">send a request to a specific replica</a> but Kubernetes in and of itself will not know how to route requests for you; your client code would have to do this on its own.</p>
