Can i create set of unique docker container with different environment by kubernetes?

11/18/2019

I have 20 users. I need to use individual container for every user. I want to pass 'user_id' by environments. When i receive message, i need to create another one container with 'user_id', which i received. how to organize it in kubernetes by the best way

-- Влад Концевич
docker
kubernetes

3 Answers

11/19/2019

So you are trying to run a pod from your application with specific env variable.

To do this you need to send a request to k8s using either raw rest api in kubernetes or one of kubernetes client libraries.

You also need to propperly set a service account for your application pod so it's allowed to create new pods.

Read more here about roles and authorisation in kubernetes.

When creating new pod, just add an env you want.

Let me know if it was helpful.

-- HelloWorld
Source: StackOverflow

11/21/2019

You can achieve it similar to example below:

kubectl run nginx1 --image=nginx --env=user_id=1

kubectl run nginx2 --image=nginx --env=user_id=2

kubectl run will be deprecated. Equivalent kubectl create can be used with deployment yml.

Another approach is to create helm chart and set userid env via set for helm install command to container env in deployment.yml.

-- techuser soma
Source: StackOverflow

11/18/2019

If you have several users, I would suggest identifying them with service accounts.

Once you've created service accounts for every user, you can assign them to Pods with the spec.serviceAccountName keyword. This field is available inside Pods using the Downward Api. For example:

apiVersion: v1
kind: Pod
metadata:
  name: pod-name
spec:
  containers:
    - name: container-name
      image: busybox
      command: [ "sh", "-c", "echo $SERVICE_ACCOUNT"]
      env:
        - name: SERVICE_ACCOUNT
          valueFrom:
            fieldRef:
              fieldPath: spec.serviceAccountName
-- Alassane Ndiaye
Source: StackOverflow