<p>How would I start a specific number of replicas of the same image, when that number is defined at startup?</p>
<p>On startup I need to call an API endpoint which returns a number. I then want to use this number to deploy that number of replicas of a pod (with each pod being aware of what order it was started in, even after restarts, etc).</p>
<p>For example the API endpoint returns 15, and 15 replicas are started with each having an 'order' / index number of 1 - 15, and maintaining always having a single pod with an 'order' number for each number between 1-15.</p>
<p>I was thinking of using an init container to call the API endpoint, I can't find how to then start that number of replicas and pass the 'order' to the pod.</p>

AnotherCat
<p>Your problem can be solved in several ways. You can use <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">Statefulset</a> to <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity" rel="nofollow noreferrer">identify your pods</a>, but you won't be able to number them from 1 to 15. Statefulset behaves slightly differently. Take a look at the <a href="https://github.com/kubernetes/kubernetes/blob/12f36302f9ee98093d0a01910e345825f1d3a86e/pkg/controller/statefulset/stateful_set_control.go#L319" rel="nofollow noreferrer">source code</a>:</p>
<pre><code>// for any empty indices in the sequence [0,set.Spec.Replicas) create a new Pod at the correct revision
for ord := 0; ord &lt; replicaCount; ord++ {
    if replicas[ord] == nil {
        replicas[ord] = newVersionedStatefulSetPod(
            currentSet,
            updateSet,
            currentRevision.Name,
            updateRevision.Name, ord)
    }
}
</code></pre>
<p>For a StatefulSet with X replicas, the numbering will start from 0 up through X-1 (see: <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#ordinal-index" rel="nofollow noreferrer">Ordinal Index</a>).</p>
<p>I think you might be interested in using <a href="https://kubernetes.io/docs/concepts/workloads/controllers/cron-jobs/" rel="nofollow noreferrer">Cronjob</a> that runs your custom script periodically on a given schedule. This script can use the Discord Gateway Bot endpoint to determine the recommended number of shards and automatically scale up your bot when Discord recommends it. A good example of such an approach is <a href="https://github.com/Auttaja-OpenSource/Marver" rel="nofollow noreferrer">Marver - a K8s StatefulSet autoscaler</a>. Just take into account that this was made in 2018, so it will need to change to accommodate your Kubernetes version. Additionally, in order to use Marver (or other similar tools), the following requirements must be met:</p>
<blockquote>
<p>This project requires that you already be using Kubernetes, and assume you have some understand of how Kubernetes works. It also assumes that you have your bot set up to handle changes in the StatefulSet's replica count gracefully. Meaning: if we scale up, all existing shards will need to re-identify with Discord to present the new shard count, and update their local cache as necessary.</p>
</blockquote>
<p>Of course you can also use an <a href="https://kubernetes.io/docs/concepts/extend-kubernetes/operator/" rel="nofollow noreferrer">operator</a> as <a href="https://stackoverflow.com/users/4216641/turing85" title="15,090 reputation">Turing85</a> has mentioned in the comment:</p>
<blockquote>
<p>Sounds like a job for an  <a href="https://kubernetes.io/docs/concepts/extend-kubernetes/operator/" rel="nofollow noreferrer">operator</a>. I would, however, highly advice against the approach of assigning an id to each pod since this, in essence, gives each pod an identity. This would, in return, mean that we have to use a  StatefulSet, not a  Deployment. Remember: pods are  <a href="https://devops.stackexchange.com/questions/653/what-is-the-definition-of-cattle-not-pets">cattle, not pets</a>.</p>
</blockquote>
