<p>I am developing an application that allows users to play around in their own sandboxes with a finite life time. Conceptually, it can be thought as if users were playing games of Pong. Users can interact with a web interface hosted at main/ to start a game of Pong. Each game of Pong will exist in its own pod. Since each game has a finite lifetime, the pods are dynamically created on-demand (through the Kubernetes API) as Kubernetes jobs with a single pod. There is therefore a one-to-one relationship between games of Pong and pods. Up to this point I have it all figured out.</p>

<p>My problem is, how do I set up an ingress to map dynamically created URLs, for example main/game1, to the corresponding pods? That is, if a user starts a game through the main interface, I would like him to be redirected to the URL of the corresponding pod where his game is hosted.</p>

<p>I could pre-allocate a set of urls, check if they have active jobs, and redirect if they do not, but the does not scale well. I am thinking dynamically assigning URLs is a common pattern in Kubernetes, so there must be a standard way to do this. I have looked at using nginx-ingress, but that is not a requirement.</p>

<p>Furthermore the comment, I created for you a little demo on <code>minikube</code> providing a working <em>Ingress Class</em> controller (enabled via <code>minikube addons enable ingress</code>).</p>

<p>Replicating the multiple <em>Deployment</em> that simulates the games.</p>

<pre><code>kubectl create deployment deployment-1 --image=nginxdemos/hello
kubectl create deployment deployment-2 --image=nginxdemos/hello
kubectl create deployment deployment-3 --image=nginxdemos/hello
kubectl create deployment deployment-4 --image=nginxdemos/hello
kubectl create deployment deployment-5 --image=nginxdemos/hello
</code></pre>

<p>Same for <em>Services</em> resources:</p>

<pre><code>kubectl create service clusterip deployment-1 --tcp=80:80
kubectl create service clusterip deployment-2 --tcp=80:80
kubectl create service clusterip deployment-3 --tcp=80:80
kubectl create service clusterip deployment-4 --tcp=80:80
kubectl create service clusterip deployment-5 --tcp=80:80
</code></pre>

<p>Finally, it's time for the <em>Ingress</em> one but we have to be quite hacky since we don't have the subcommand <code>create</code> available.</p>

<pre><code>for number in `seq 5`; do echo "
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: deployment-$number
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
 rules:
 - host: hello-world.info
   http:
     paths:
     - path: /game$number
       backend:
         serviceName: deployment-$number
         servicePort: 80
" | kubectl create -f -; done
</code></pre>

<p>Now you have <em>Pod</em>, <em>Service</em> and <em>Ingress</em>: obviously, you have to replicate the same result using Kubernetes API but, as I suggested in the comment, you should create a <em>single</em> <em>Ingress</em> resource and update accordingly <em>Path</em> subkey in a dynamic way.</p>

<p>However, if you try to simulate the cURL call faking the <code>Host</code> header, you can see the working result:</p>

<pre><code># curl `minikube ip`/game2 -sH 'Host: hello-world.info'|grep -i server
&lt;p&gt;&lt;span&gt;Server&amp;nbsp;address:&lt;/span&gt; &lt;span&gt;172.17.0.5:80&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Server&amp;nbsp;name:&lt;/span&gt; &lt;span&gt;deployment-2-5b98b954f6-8g5fl&lt;/span&gt;&lt;/p&gt;

# curl `minikube ip`/game4 -sH 'Host: hello-world.info'|grep -i server
&lt;p&gt;&lt;span&gt;Server&amp;nbsp;address:&lt;/span&gt; &lt;span&gt;172.17.0.7:80&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Server&amp;nbsp;name:&lt;/span&gt; &lt;span&gt;deployment-4-767ff76774-d2fgj&lt;/span&gt;&lt;/p&gt;
</code></pre>

<p>You can see the <em>Pod</em> IP and name as well.</p>
