We have a PHP-based application running in Kubernetes and uses settings from a
/app/.env
file plus environment variables.
The problem is that application running in a Docker container can’t see an $TEST_VAR
variable although it’s present in the Deployment:
... containers: - name: application-dev-web image: bttrm-application:119 ... - name: TEST_VAR valueFrom: secretKeyRef: name: bttrm-app-secret key: test_var ...
And its value is set via Kubernetes Secrets:
apiVersion: v1 kind: Secret metadata: name: bttrm-app-secret namespace: application-dev-ns type: Opaque data: test_var: dGVzdF92YWwK
In a pod, this variable also accessible and can be found with the env
command:
Still, if create a php-file with the phpinfo()
function – in the resulted Environment block we will see only two variables – $USER
and $HOME
:
Solution #1: env[TEST_VAR]
The first solution can be to add an explicit variable export:
... env[TEST_VAR] = $TEST_VAR
Deploy and check:
Okay, it’s working but you’ll have to add such a string for each variable used.
Solution #2: clear_env = no
Another solution can be by setting the clear_env
PHP-FPM’s parameter no.
Update the /etc/php7/php-fpm.d/www.conf
file and add the following:
... ;env[DB_PASSWORD] = $DB_PASSWORD clear_env = no
Deploy. check:
Now we can access all the environment variables.
Готово.
(not)Solution #3: variables_order
in the php.ini
PHP also has a parameter which defines an order of the variables sources, see the
variables_order
string Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to “SP” then PHP will create the
superglobals $_SERVER and$_POST , but not create$_ENV ,$_GET , and$_COOKIE . Setting to “” means nosuperglobals will be set.
Check the current value:
The suggestion was that the issue can be fixed by adding the E (Environment) – but setting variables_order = "EGPCS"
didn’t help.