Now that we learned about the crucial
feature of volumes,
persistent volumes,
and persistent volume claims.
The last topic I wanna cover in this course section
would be environment variables.
Because environment variables
of course matter
in a lot of applications.
Let's say in this application here.
The application we've been working on all the time.
This story folder,
in which we stored a text text file
is of course hard-coded in the code.
Now actually,
the fact that
we do store data in a story folder
is however configured
in our deployment yaml file, here.
At least that is the folder
which we save
with help of a volume.
So we might prefer
to set up that exact folder name here
in our yaml files,
and use it through an environment variable
inside of app JS.
And we learned about applying environment variables
with Docker and Docker compose.
Of course, Kubernetes
also supports environment variables.
And for that,
let's start the code.
Here I will replace
the hard-coded story
folder name
with process.env.STORY_FOLDER.
This name is up to you.
It's your program,
it's your environment,
the names are up to you.
But of course now we need to ensure
that this environment variable
is set to a folder name
we wanna use
when we deploy our cluster.
And therefore we need to provide a value
for this environment variable.
And the easiest way of doing that
is to go to the file where you define your pod,
and therefore the container,
which is executed in the pod.
So in our case, the deployment.yaml file,
and in there,
in your containers config,
next to volume mounts,
and image,
and so on,
you can add a env key.
And below that,
so indented below the env key
you can set up all your environment key value pairs
which you might need.
Simply by adding a list of environment variables
by using dashes here.
So my first,
and in this case only environment variable
which I wanna set
has a name,
and that's something you need to provide
the name of the environment variable
of story underscore, folder.
So that name you chose here in your code
that's the name
for the environment variable
you wanna define here.
And then of course, besides the name,
we also need,
a value.
So that's the second key
you need to provide for every environment variable.
So name
of the environment variable,
and value
for that environment variable.
In this case, story.
But of course you could choose a different name here,
but then
you should also bind a different folder
with your
volume here.
But with that we now externalized this name,
and we make it more flexible.
Now, if we ever want to go for a different folder name
we only need to make changes here
in our
containers pack,
and we don't need to make any changes to app JS anymore
because we are utilizing an environment variable here.
Hence here now,
if I
update my image tag,
or we set the image pull policy
to always pull the updated image,
which I don't have here,
hence I changed the tag.
Then we just need to
build this image again,
with a tag of academind/kub-data-demo,
and then
a tag of 2,
so colon 2 here,
hit enter,
and once this is built, of course,
docker push academind kub-data-demo,
with tag 2.
And now this is pushed to Docker Hub,
so that now
this will
be pulled, and used
if we apply our changes to the deployment.
So let's wait for this push process to finish.
And then let's apply this updated deployment.yaml file,
so that the deployment is updated.
Our old pods will be
terminated eventually
here you can see it,
and the new pods are up and running.
Now getting the data still works,
and everything still works.
But now under the hood,
we are using our environment variables.