Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I modify container's environment variables without restarting pod using kubernetes

Tags:

I have a running pod and I want to change one of it's container's environment variable and made it work immediately. Can I achieve that? If I can, how to do that?

like image 384
Lucy Panda Avatar asked Jul 12 '17 06:07

Lucy Panda


People also ask

How do you change environment variables in running containers?

It is not possible to change the environment variables of a running process except from within that process itself. This could be (made to be) possible by allowing to update the environment variable config (via `docker update) and then restarting the container.

How do you change env pods?

When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the configuration file. In your shell, run the printenv command to list the environment variables. To exit the shell, enter exit .

How does Kubernetes set environment variables?

There are two ways to define environment variables with Kubernetes: by setting them directly in a configuration file, from an external configuration file, using variables, or a secrets file. This tutorial shows both options, and uses the Humanitec getting started application used in previous tutorials.

How do I set environment variables in Kubernetes pods?

Play with Kubernetes Define an environment variable for a container When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the configuration file.

How do I set an environment variable for a container?

Define an environment variable for a container When you create a Pod, you can set environment variables for the containers that run in the Pod. To set environment variables, include the env or envFrom field in the configuration file. In this exercise, you create a Pod that runs one container.

How does Kubernetes decide which node to place a pod on?

When you specify the resource request for containers in a Pod, the kube-scheduler uses this information to decide which node to place the Pod on. When you specify a resource limit for a container, the kubelet enforces those limits so that the running container is not allowed to use more of that resource than the limit you set.

How does Kubernetes handle restarting a container?

If that process is the container's PID 1, and the container is marked as restartable, Kubernetes restarts the container. The memory limit for the Pod or container can also apply to pages in memory backed volumes, such as an emptyDir. The kubelet tracks tmpfs emptyDir volumes as container memory use, rather than as local ephemeral storage.


2 Answers

Simply put and in kube terms, you can not.

Environment for linux process is established on process startup, and there are certainly no kube tools that can achieve such goal. For example, if you make a change to your Deployment (I assume you use it to create pods) it will roll the underlying pods.

Now, that said, there is a really hacky solution reported under Is there a way to change the environment variables of another process in Unix? that involves using GDB

Also, remember that even if you could do that, there is still application logic that would need to watch for such changes instead of, as it usually is now, just evaluate configuration from envs during startup.

like image 109
Radek 'Goblin' Pieczonka Avatar answered Oct 15 '22 08:10

Radek 'Goblin' Pieczonka


This worked with me

kubectl set env RESOURCE/NAME KEY_1=VAL_1 ... KEY_N=VAL_N

check the official documentation here

Another approach for runtime pods you can get into the Pod command line and change the variables in the runtime RUN kubectl exec -it <pod_name> -- /bin/bash Then Run export VAR1=VAL1 && export VAR2=VAL2 && your_cmd

like image 26
Ahmed Badawy Avatar answered Oct 15 '22 08:10

Ahmed Badawy