1

I want to setup my Django app in kubernetes environment in such a way that while creating the app container, environment variables are passed such that those environment variables are used to initiate the containers. For example on starting the app container I want to issue management commands such as

python manage.py createuser --lastname lname --firstname --fname --number --num "

and so on. How to pass these variable values such as lname and fname above inside the container in a generic way such that every time new values can be passed depending on the user credentials and they do not need to be hard coded everytime?

devcloud
  • 391
  • 5
  • 18
  • You should be able to do this as a script that runs as container startup time, possibly an entrypoint wrapper script. What have you already tried? (Outside of Docker, could you write a script that did what you wanted and then launched the Django application?) – David Maze Nov 04 '20 at 14:38
  • yes i run a shell script that quries the database pod as: kubectl exec -it database-pod -- bash -c "psql -U postgres -c \"Select json_build_object('first_name',first_name,'last_name',last_name,'email',email,'date_joined',date_joined) – devcloud Nov 04 '20 at 15:12
  • I want to use the values returned by this shell script to initialize the container such as lastname etc – devcloud Nov 04 '20 at 15:14

1 Answers1

1

You can pass environment variables in your deployment manifest, for example:

spec:
  containers:
  - name: envar-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: DEMO_GREETING
      value: "Hello from the environment"
    - name: DEMO_FAREWELL
      value: "Such a sweet sorrow"

You can also invoke them in container by using command and argument object, the same way as running a command in container

  containers:
  - name: env-print-demo
    image: bash
    env:
    - name: GREETING
      value: "Warm greetings to"
    - name: HONORIFIC
      value: "The Most Honorable"
    - name: NAME
      value: "Kubernetes"
    command: ["echo"]
    args: ["$(GREETING) $(HONORIFIC) $(NAME)"]

It is also possible to pass variables from ConfigMap or Secrets.

kool
  • 3,214
  • 1
  • 10
  • 26
  • In my case i do not know the environment variables beforehand. The values need to be set as result of a bash script that gets values from querying a database pod. How to set the environment variable values dynamically? – devcloud Nov 04 '20 at 15:20