<p>I'm trying to figure out which tools from GKE stack I should apply to my use case which is a dynamic deployment of stateful application with dynamic HTTP endpoints.</p>
<p>Stateful in my case means that I don't want any replicas and load-balancing (because the app doesn't scale horizontally at all). I understand though that in k8s/gke nomenclature I'm still going to be using a 'load-balancer' even though it'll act as a reverse proxy and not actually balance any load.</p>
<p>The use case is as follows. I have some web app where I can request for a 'new instance' and in return I get a dynamically generated url (e.g. <a href="http://random-uuid-1.acme.io" rel="nofollow noreferrer">http://random-uuid-1.acme.io</a>). This domain should point to a newly spawned, single instance of a container (Pod) hosting some web application. Again, if I request another 'new instance', I'll get a <a href="http://random-uuid-2.acme.io" rel="nofollow noreferrer">http://random-uuid-2.acme.io</a> which will point to another (separate), newly spawned instance of the same app.</p>
<p>So far I figured out following setup. Every time I request a 'new instance' I do the following:</p>
<ul>
<li>create a new Pod with dynamic name <code>app-${uuid}</code> that exposes HTTP port</li>
<li>create a new Service with NodePort that &quot;exposes&quot; the Pod's HTTP port to the Cluster</li>
<li>create or update (if exists) Ingress by adding a new http rule where I specify that domain X should point at NodePort X</li>
</ul>
<p>The Ingress mentioned above uses a LoadBalancer as its controller, which is automated process in GKE.</p>
<p>A few issues that I've already encountered which you might be able to help me out with:</p>
<ol>
<li>While Pod and NodePort are separate resources per each app, Ingress is shared. I am thus not able to just create/delete a resource but I'm also forced to keep track of what has been added to the Ingress to be then able to append/delete from the yaml which is definitely not the way to do that (i.e. editing yamls). Instead I'd probably want to have something like an Ingress to monitor a specific namespace and create rules automatically based on Pod labels. Say I have 3 pods with labels, <code>app-1</code>, <code>app-2</code> and <code>app-3</code> and I want Ingress to automatically monitor all Pods in my namespace and create rules based on the labels of these pods (i.e. app-1.acme.io -&gt; reverse proxy to Pod <code>app-1</code>).</li>
<li>Updating Ingress with a new HTTP rule takes around a minute to allow traffic into the Pod, until then I keep getting 404 even though both Ingress and LoadBalancer look as 'ready'. I can't figure out what I should watch/wait for to get a clear message that the Ingress Controller is ready for accepting traffic for newly spawned app.</li>
<li>What would be the good practice of managing such cluster where you can't strictly define Pods/Services manifests because you are creating them dynamically (with different names, endpoints or rules). You surely don't want to create bunch of yaml-s for every application you spawn to maintain. I would imagine something similar to <a href="https://github.com/hashicorp/consul-template" rel="nofollow noreferrer">consul templates</a> in case of Consul but for k8s?</li>
</ol>

<p>I participated in a similar project and our decision was to use <a href="https://kubernetes.io/docs/reference/using-api/client-libraries/" rel="nofollow noreferrer">Kubernetes Client Library</a> to spawn instances. The instances were managed by a simple web application, which took some customisation parameters, saved them into its database, then created an instance. Because of the database, there was no problem with keeping track of what have been created so far. By querying the database we were able to tell if such deployment was already created or update/delete any associated resources.</p>
<p>Each instance consisted of:</p>
<ul>
<li>a deployment (single or multi-replica, depending on the instance);</li>
<li>a <code>ClusterIp</code> service (no reason to reserve machine port with <code>NodePort</code>);</li>
<li>an ingress object for shared ingress controller;</li>
<li>and some shared configMaps.</li>
</ul>
<p>And we also used <a href="https://github.com/kubernetes-sigs/external-dns" rel="nofollow noreferrer">external DNS</a> and <a href="https://cert-manager.io/docs/installation/kubernetes/" rel="nofollow noreferrer">cert manager</a>, one to manage DNS records and another to issue SSL certificates for the ingress. With this setup it took about 10 minutes to deploy a new instance. The pod and ingress controller were ready in seconds but we had to wait for the certificate and it's readiness depended on whether issuer's DNS got our new record. This problem might be avoided by using a wildcard domain but we had to use many different domains so it wasn't an option in our case.</p>
<p>Other than that you might consider writing a <a href="https://helm.sh/" rel="nofollow noreferrer">Helm chart</a> and make use of <code>helm list</code> <a href="https://helm.sh/docs/helm/helm_list/" rel="nofollow noreferrer">command</a> to find existing instances and manage them. Though, this is a rather 'manual' solution. If you want this functionality to be a part of your application - better use a client library for Kubernetes.</p>
