Updating environment variables on running pods isn't supported in Kubernetes.
However, if you deploy your pods into Kubernetes as deployment.apps
, you can edit the environment variable on the deployment, which will cause Kubernetes to deploy new instances of you pod with the new environment variable's value, and terminate the older pods with the older variable value.
For example, if you deploy a pod using the Pod
api like this:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
env:
- name: TEST_VAR
value: "123"
You will not be able to kubectl edit pod nginx-pod
to update the value set for TEST_VAR
. If you ever want to update the TEST_VAR
env variable, you'll need to delete the pod and recreate it with an updated value "manually".
You should instead deploy the pod using the Deployment.app
api, like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
env:
- name: TEST_VAR
value: "123"
Which will allow you do kubectl edit deployments.apps nginx-deployment
, or even easier, kubectl set env deployment/nginx-deployment TEST_VAR="456"
, to update the TEST_VAR
value. Kubernetes will delete and recreate the pods for you.