<p>Thanks in advance to all those who help.</p>

<p>Hello, I have somewhat of a unique problem, its rather lengthy to explain but I think if solved we can expand the use-cases of Kubernetes. I think I know how to solve it, but I'm not sure if Kubernetes Stateful Sets supports the solution. Let me elaborate the domain of the problem, the problem itself, and then some of my sample solutions and maybe someone can help fill the gaps.</p>

<p><strong>The Domain Space</strong>:</p>

<ul>
<li>I have a set of Accounts (external to kubernetes) {Account_A, Account_B, Account_C, etc.}</li>
<li>Accounts can be active or inactive at anytime (Important: in NO PARTICULAR ORDER).</li>
<li>If activated, a pod is deployed which serves that account, and keeps a persistent volume with all of that accounts work-space/data. That account is interacted with by its unique pod identifier, and IP.</li>
<li>If deactivated, the pod is removed but the data persists so that the next time it is activated, it will be bound to the same persistent-volume-claim and therefore have access to its previous data.</li>
<li>If reactivated, a pod is redeployed that uses the previous persistent-volume-claim to resume working on the data from previous sessions</li>
</ul>

<p>Obviously, looking at the available Kubernetes tools/objects, a stateful-set with headless-service is the ideal way of approaching this. It supports unique pods, which are assigned unique IPs, and supports persistent volumes. It also supports dynamically provisioning persistent-volumes through</p>

<p><strong>The Problem:</strong></p>

<p>As mentioned in the domain, accounts can be active in any order, but stateful-set pods are ordinal, meaning pod_1 has to be active for pod_2 to be active for pod_3 to be active, etc. We can't have pod_1 active and pod_3 active while pod_2 is inactive. This means if I enable Account_A, then Account_C, a pod named pod_1 will be created, and then a pod named pod_2 will be created.</p>

<p>Now you might say that this isn't a problem. We just keep a map that maps each account to the relative pod_number. For example, Account_A -> pod_1 and Account_C -> pod_2</p>

<p>Why is this a problem? Because when specifying a volumeClaimTemplate in the stateful-set, persistent-volume-claims use the pod's name as their identifier when being created. Which means that only the pod with the same name can access the same data. The data(volumes) is bound based on a pod's name, rather than the account. This creates a disconnect between accounts and their persistent volumes. Any pod with name pod_2 will always have the same data that pod_2 has always had, regardless of which account was "mapped" to pod_2.</p>

<p>Let me further illustrate this with an example:</p>

<pre><code>1. Account_A=disabled, Account_B=disabled, Account_C=disabled (Start state, all accs disabled)
2. Account_A=enabled, Account_B=enabled, Account_C=enabled -&gt; (All accounts are enabled)
    pod_1 is created (with volume_1) and mapped to Account_A
    pod_2 is created (with volume_2) and mapped to Account_B
    pod_3 is created (with volume_3) and mapped to Account_C
3. Account_A=disabled, Account_B=disabled, Account_C=disabled (All Accounts are disabled)
    pod_1 is deleted, volume_1 persists
    pod_2 is deleted, volume_2 persists
    pod_3 is deleted, volume_3 persists
4. Account_A=enabled, Account_B=disabled, Account_C=enabled (re-enable A and C but leave B disabled)
    pod_1 is created (with volume_1) and mapped to Account_A (THIS IS FINE)
    pod_2 is created (with **volume_2**) and mapped to Account_C (THIS IS **NOT** FINE)
</code></pre>

<p>Can you see the issue? Account_C is now using the data-store that should belong to Account_B (volume_2 was created and used by account_b not Account_C), because of the fact that volumes/claims are mapped by name to pod names, and pods have to be ordinal i.e. pod_1 then pod_2.</p>

<p><strong>Potential Solutions</strong></p>

<ol>
<li><p>Be able to support custom non-ordinal names for pods in a stateful-set. (Simplest and most effective)</p>

<p>This solves everything, and keeps the benefits and tools of statefulsets. I can name my pods what I want when launched, so that when an account is 
enabled I just launch a pod with that accounts name, and the volume that is created is mapped to any 
pod with that same name. I've looked and can't seem to find a way to do this.</p>

<p>(p.s.) I know that stateful-sets are supposed to be ordinal for ordering 
guarantees, but you can turn this off with "podManagementPolicy: Parallel"</p></li>
<li><p>Some way to do this with labels and selectors instead?</p>

<p>I'm rather new to Kubernetes, and I still don't fully understand all the moving parts. Maybe there's some way to use labels in my volumeClaimtemplate, to have volume claims attach to volumes with a certain label. i.e. Account_C mapped to pod_2 can request volume_3 because volume_3 has a label with: account=Account_C. I'm currently looking into this. If it helps, my persistent volumes are provisioned dynamically using this tool: <a href="https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client" rel="nofollow noreferrer">https://github.com/kubernetes-incubator/external-storage/tree/master/nfs-client</a>
Maybe I can somehow modify it so that it adds certain labels to the persistent-volumes it creates.</p></li>
<li><p>Ditch statefulsets and deployments and just add pods manually to the cluster</p>

<p>This is not a great solution since according to docs, pods shouldn't really exist without a statefulset or deployment as a parent, and it also removes all the built-in functionality of persistent-volumes and dynamic volume provisioning, etc. For me the dealbreaker is not having volumeClaimTemplates which create or bind to an existing volumeClaim when deployed. If I could recreate this somehow, this solution would work.</p></li>
<li><p>Create custom Kubernetes object to do this for me</p>

<p>This is unideal, since it would be a lot of work and I wouldn't even know where to begin. I would also be recreating the exact same thing as a stateful-set except without the ordinal-mapping. I would have to figure out how to writeoperators and replicasets, etc. Seems like overkill for a rather simple problem.</p></li>
<li>Have the persistent storage be mounted from within the pod's container
This is a last resort since it completely removes the need for kubernetes. It also means I have to send the connection information to the container within the pod, and opens up an entire can of worms with security and authentication there.</li>
</ol>

<p>I will update with anything else I find or think of. Thanks to all who help.</p>

user2896438
<p>It seems to me that you're convinced that <code>StatefulSets</code> is a step in the right direction but that's not entirely true.</p>

<p><code>StatefulSets</code> have ordinality due to two reasons:</p>

<ul>
<li>Creating ordered <code>PersistentVolumeClaims</code></li>
<li>Being able to create FQDN endpoints for individual pods (using a headless service)</li>
</ul>

<p>In your case, neither seems to be true. You just need stable storage per account. While you think that #4 from your potential solutions is most unideal, it is the most "Kubernetes native" way to do it.</p>

<h2>Solution</h2>

<p>You need to write a component that manages a StatefulSet or even a Deployment per account. I say deployment because you don't need stable network identifiers for each pod. A <code>ClusterIP</code> service per account will be adequate for communication.</p>

<p>In the Kubernetes world, these components are called controllers (without custom objects) and operators (with custom objects/manages applications).</p>

<p>You can start by looking into <a href="https://github.com/operator-framework/operator-sdk" rel="nofollow noreferrer">operator-sdk</a> and <a href="https://github.com/kubernetes-sigs/controller-runtime" rel="nofollow noreferrer">controller-runtime</a>. Operator SDK aggregates commonly used functionalities on top of <code>controller-runtime</code> as a framework. It also makes developers' life easier by incorporating <a href="https://github.com/kubernetes-sigs/kubebuilder" rel="nofollow noreferrer">kubebuilder</a> which is used to generate CRD and K8S API code for custom objects. All you need to define is <code>struct</code>s for your custom object and a controller.</p>

<p>Take a look at Operator SDK, you'll find that creating and managing custom objects is not that hard.</p>

<h3>Custom object based flow for your problem</h3>

<p>This is how I imagine the flow of your operator from what I understood in your write up.</p>

<ul>
<li>One <code>Account</code> object maps to one account. Each object has unique metadata that maps it to its account. It should also have an <code>active: boolean</code> in its spec.</li>
<li>Watch for custom <code>Account</code> objects</li>
<li><p>Whenever you need to create a new account, use Kubernetes APIs to create a new <code>Account</code> object (will trigger an Add event in the controller) and then your controller should</p>

<ul>
<li>Create/Update a <code>PersistentVolumeClaim</code> for the account</li>
<li>Create/Update the <code>Deployment</code> with the volume from created <code>PVC</code> specified in the Pod template</li>
<li><em>Catch: Add events are also received for old objects when controller restarts. So the action taken should be "Create or Update".</em> </li>
</ul></li>
<li><p>Set the <code>active</code> field in your custom object to <code>false</code> for deactivating the account (a Modify event in the controller) and then your controller should</p>

<ul>
<li>Delete the deployment without touching the volume at all.</li>
</ul></li>
<li>Set the <code>active</code> field to <code>true</code> for reactivating the account. (modify event again)

<ul>
<li>Recreate the deployment with the same volume specified in the Pod template</li>
</ul></li>
<li>Delete the <code>Account</code> object to clean up underlying resources.</li>
</ul>

<p>While all of this might not make perfect sense right away, I would still suggest you to go through operator-sdk's docs and examples. IMO, that would be a leap in the right direction.</p>

<p><em>Cheers!</em></p>
