Managing Environment Variables with kubectl

kubernetes kubectl environment variable set unset

2 min read | by Jordi Prats

In today's highly dynamic and containerized environments, managing environment variables is crucial for configuring applications effectively. With the Kubernetes command-line tool, kubectl, you can imperatively set or remove environment variables for your existing objects.

Using kubectl set env we can patch variables to several objects: Pod, Replicationcontroller, Deployment, Daemonset, Statefulset, Cronjob and Replicaset.

Setting Environment Variables with kubectl

If we want to set the environment variable DEMO_ENV_VAR to 1 for the demo Deployment we can use the following kubectl set env command:

kubectl set env deployment/demo DEMO_ENV_VAR="1"

Removing Environment Variables with kubectl

If we want to remove some environment variable, setting it to an empty variable can work just as well, but if we really need to unset it we can use the following syntax:

kubectl set env deployment/demo DEMO_ENV_VAR-

The final dash is telling kubectl to unset the environment variable from the object.

Posted on 21/05/2023