Can I have a K8s pod per user/firm?

11/4/2019

Is there a way we can have a K8s pod per user/per firm? I realise, per user/per firm grouping is mixing up the business level semantics with infrastructure but say I had this need for regulatory reasons, etc to keep things separate. Then is there a way to create a pod on the fly when a user logs in for the first time and hold this pod reference and route any further requests to the relevant pod which will host a set of containers each running an instance of one of the modules.

  1. Is this even possible?
  2. If possible, what are those identifiers that can be injected into the pod on the fly that I could use to identify that this is USER-A-POD vs USER_B_POD or FIRM_A_POD vs FIRM_B_POD ? Effectively, I need to have a pod template that helps me create identical pods of 1 replica but the only way they differ is they are serving traffic related to one user/one firm only.
-- Chetya
kubernetes
kubernetes-pod

2 Answers

11/5/2019

Yes, you can create multiple virtual clusters for each user with namespaces.

https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/

Namespaces are the way to divide cluster between users.

-- kgm
Source: StackOverflow

11/5/2019

Generally, if you want to send traffic to a specific pod say from a Kubernetes Service you would use Labels and Selectors. For example, using the selector app: usera-app in the Service:

apiVersion: v1
kind: Service
metadata:
  name: usera-service
spec:
  selector:
    app: usera-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Then say if the Deployment for your pods, using the label app: usera-app:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: usera-deployment
spec:
  selector:
    matchLabels:
      app: usera-app
  replicas: 2
  template:
    metadata:
      labels:
        app: usera-app
    spec:
      containers:
      - name: myservice
        image: nginx
        ports:
        - containerPort: 80

More info here

How you assign your pods and deployments is up to you and whatever configuration you may use. If you'd like to force create some of the labels in deployments/pods you can take a look at MutatingAdminssionWebhooks.

If you are looking at projects to facilitate all this you can take a look at:

Other tools that can help you with attestation and admission mechanism (would have to be adapted for labels):

-- Rico
Source: StackOverflow