What is the best way to set environment variables at pod creation?

9/7/2018

I'm running Openshift Container Platform 3.9 where I'm deploying three containers; a postgres database container, a qpid message broker container, and a server that needs to connect to both.

I need to set environment variables at pod creation in order to allow all three containers to connect. For example, I need to set DB_HOST and BROKER_HOST variables with the corresponding pod addresses. I was going to use pod presets to accomplish this, but per the documentation, As of OpenShift Container Platform 3.7, pod presets are no longer supported.

What is the best method to set these type of addresses during pod creation?

-- Dillon
docker
kubernetes
openshift

2 Answers

9/7/2018

the quick answer is: you don't

If you want to consume some service, define a Service object for it so you get a fixed dns name you can use to refer to that service. And thenm you know the values of DB_HOST or BROKER_HOST in advance and set them in Pod as any other

-- Radek 'Goblin' Pieczonka
Source: StackOverflow

9/10/2018

After looking through the documentation more thoroughly I found that there are actually a few ways to accomplish this.

First, when generating applications from a template, source, or an image, you can use the -e|--env argument to pass environment variables to the application container at run time:

$ oc new-app openshift/postgresql-92-centos7 \
-e POSTGRESQL_USER=user \
-e POSTGRESQL_DATABASE=db \
-e POSTGRESQL_PASSWORD=password

The variables can also be read from file using the --env-file argument:

$ cat postgresql.env
POSTGRESQL_USER=user
POSTGRESQL_DATABASE=db
POSTGRESQL_PASSWORD=password
$ oc new-app openshift/postgresql-92-centos7 --env-file=postgresql.env

This is useful, but a more convenient way to do set configuration data at pod creation is with a ConfigMap. Per the documentation,

The ConfigMap object provides mechanisms to inject containers with configuration data while keeping containers agnostic of OpenShift Enterprise. A ConfigMap can be used to store fine-grained information like individual properties or coarse-grained information like entire configuration files or JSON blobs.

Openshift 3.9 Developer Guide

-- Dillon
Source: StackOverflow