This post is originally written here on my blog with images and code snippets -> https://easyontheweb.com/setting-environment-variables-in-kubernetes/
I was recently working on a micro-services based side project and I needed to implement authentication. I went with the JWT approach and while storing the JWT secret key I found myself stuck. Now, I don’t have much experience in Docker & Kubernetes as of now and this represented a great opportunity to learn something new – Setting up environment variables in Kubernetes.
If anyone is confused about what the above paragraph meant and have not worked with JWT, just think that I needed to save a secret value that I don’t want to be included with my code (the JWT secret key) , so I decided to save it as an environment variable but had no idea how to do it in Kubernetes.
Therefore, in this article we’ll be first discussing what environment variables are, how they are helpful and after that we’ll be discussing why it is a bit of an issue setting environment variables up on Kubernetes and then finally how it can be done.
What are environment variables ?
An environment variable is a variable whose value is stored not in the program we write but on an operating system level (or micro-service level). The advantage of using environment variables is that you no longer have to include sensitive information into your program code.
Say, for example you wanted to store some API’s secret key that you created for your application. Now, of course you store it directly as a string in your program code but this is something that is not safe and not really preferred. What is instead preferred is setting up an environment variable that hold the value of that secret. This environment variable is then available for us to access in the code.
So, just think of it like this – you are setting up a variable just like you would normally do in your code but it is not being saved with your program, instead you are setting it up on the operating system level and that variable is free for you to use in your code as long as your code runs in an environment that has the value for that variable.
There are numerous use cases of setting up environment variables like :-
Storing secret values like API secrets
Storing configuration values that are not likely to change again and again
Setting up and accessing environment variables
Well, setting up environment variable mostly depends on your operating system and different operating systems set it up in different ways. Most of the people make use of the terminal to set up environment variables temporarily and using a bash script if they want to set it up permanently.
How to set temporary environment variables is given here in this article => https://www.schrodinger.com/kb/1842
You can also set up environment variables with packages like dotenv which I really recommend or in your frontend using your package bundler like Webpack. Using dotenv is what I usually prefer and would recommend you to do the same.
When it comes to accessing environment variables different languages do that in different ways but in NodeJS the application automatically loads up all the environment variables available to it in the process.env object.
Therefore, if say I have created an environment variable for my JWT key called JWT_KEY then that value is available for me to use in my NodeJS code as process.env.JWT_KEY and just like any other simple variable the value will be replaced.
Environment variables and Kubernetes
The problem with setting up environment variables in Kubernetes is that all the pods or containers running in the cluster have their own environment, don’t they ? So, what can be done is that we can set up different environment variables for each of the containers in the cluster separately but that would just be too much of hassle, duplication and error-prone way of doing it. Sure, if there are environment variables used in just one container you can do that. But if we want to share an environment variables in the cluster across containers we need to do something else.
What we need is actually a mechanism in which we define environment variables on a cluster level and then be able to load those values in the different environments of different containers and of course then be able to access those values from the applications running inside those containers.
Thankfully, kubernetes has just the thing for us !
The ‘secret’ object
If you’re in this article and have reached this point reading it , I assume you know about how clusters are set up in Kubernetes and what objects are in the cluster.
So, Kubernetes provides us with a special type of object called “secret” that we can set up in our cluster. This secret object can hold our environment variable and then we can contact with this secret object from the various pods that we have created.
The command above needs to be run from the root of your entire project and what it does is create the “secret” type object for us with the name my-secret and it stores an environment variable called my that stores the value amit123.
Step 1 is done, we have set up a secret object that now we need to refer from within different deployments for the variable to be loaded into the environment of the containers managed by the deployment (or directly into the pod config if you use that method of setting up your pods).
Above is a part from my deployment.yaml file for a certain auth service. Now, I really don’t like explaining .yaml files and I think it does any good even doing that. What we can figure out though is that we’re setting up the env for this particular container and setting up the MY_SECRET environment variable value using the secret my-secret and the key my in that object.
What this does is exactly what I told before and include the variable MY_SECRET in the environment variables of this container. Now, whatever application is running inside this container can make use of this MY_SECRET value inside it using whatever language the application is written in.
I hope you understood the uses of environment variables in this article and it helped you in figuring out how you can set up environment variables in your kubernetes cluster to use from inside your containers.
Please join this facebook group of web developers including me for discussions and other cool articles -> https://www.facebook.com/groups/503230450489995
And for other devOps articles on my blog please follow this link -> https://easyontheweb.com/category/devops/