1
00:00:02,090 --> 00:00:04,190
Now what if you don't want to set up

2
00:00:04,190 --> 00:00:08,100
your environment variables in this way.

3
00:00:08,100 --> 00:00:09,300
You don't want to add them

4
00:00:09,300 --> 00:00:12,170
to your container specification like this.

5
00:00:12,170 --> 00:00:14,970
Instead you wanna have your key value pairs

6
00:00:14,970 --> 00:00:18,383
in a separate file or a separate resource

7
00:00:18,383 --> 00:00:22,040
and therefore for entity in your cluster.

8
00:00:22,040 --> 00:00:25,260
Maybe also such that different containers specs

9
00:00:25,260 --> 00:00:26,620
from different pods

10
00:00:26,620 --> 00:00:29,520
can utilize the same environment variables.

11
00:00:29,520 --> 00:00:31,280
This would be a common use case

12
00:00:31,280 --> 00:00:35,610
and something you can also easily implement with Kubernetes.

13
00:00:35,610 --> 00:00:38,090
For this, I'll add a new YAML file

14
00:00:38,090 --> 00:00:42,397
and that will be my environment.yamlfile.

15
00:00:42,397 --> 00:00:45,090
But the name as always is up to you.

16
00:00:45,090 --> 00:00:48,100
It doesn't have to be named environment.yaml

17
00:00:49,180 --> 00:00:52,213
In this file, we will create a so-called ConfigMap.

18
00:00:53,140 --> 00:00:55,900
For this, we first of all need to set the apiVersion

19
00:00:55,900 --> 00:00:59,773
to v1 again and set the kind to ConfigMap.

20
00:01:00,830 --> 00:01:02,993
This is simply another resource,

21
00:01:02,993 --> 00:01:05,990
another object Kubernetes understands.

22
00:01:05,990 --> 00:01:07,680
And as the name suggests,

23
00:01:07,680 --> 00:01:10,520
it creates a map of configurations.

24
00:01:10,520 --> 00:01:14,770
So a key value pair list basically.

25
00:01:14,770 --> 00:01:17,550
We can and should give it some metadata.

26
00:01:17,550 --> 00:01:21,690
For example a name by which we later can then use it.

27
00:01:21,690 --> 00:01:26,350
We could name this data-store-env

28
00:01:26,350 --> 00:01:28,440
because we'll store the environment variables

29
00:01:28,440 --> 00:01:30,260
for storing data in here.

30
00:01:30,260 --> 00:01:32,263
But ultimately this name is up to you.

31
00:01:33,350 --> 00:01:36,440
Besides the metadata, we then need to add the data

32
00:01:36,440 --> 00:01:39,520
which we do wanna set up in this ConfigMap.

33
00:01:39,520 --> 00:01:43,600
And here we don't do this with a spec but with a data key.

34
00:01:43,600 --> 00:01:45,650
And below that it's super simple.

35
00:01:45,650 --> 00:01:48,800
You have a bunch of key value pairs,

36
00:01:48,800 --> 00:01:53,800
for example, folder and then story,

37
00:01:54,190 --> 00:01:56,580
because I wanna store that story's string

38
00:01:56,580 --> 00:01:58,520
in an environment variable

39
00:01:58,520 --> 00:02:01,563
or in this case, simply a key named folder.

40
00:02:02,490 --> 00:02:04,210
Now this is a ConfigMap

41
00:02:04,210 --> 00:02:07,450
and we could have multiple key value pairs here

42
00:02:07,450 --> 00:02:08,560
if we needed it.

43
00:02:08,560 --> 00:02:10,800
But here I only need one key value payer.

44
00:02:10,800 --> 00:02:12,490
But you can have as many as you need

45
00:02:12,490 --> 00:02:14,340
and you don't have dashes here.

46
00:02:14,340 --> 00:02:18,280
Instead, these are all simply nested key value pairs

47
00:02:18,280 --> 00:02:20,400
inside of this data object here

48
00:02:20,400 --> 00:02:23,500
in this ConfigMap configuration.

49
00:02:23,500 --> 00:02:25,580
Now we can apply this.

50
00:02:25,580 --> 00:02:29,423
We can apply this file the environment.yamlfile.

51
00:02:32,080 --> 00:02:34,670
And this creates this ConfigMap.

52
00:02:34,670 --> 00:02:37,717
As always with kubectl get configmap.

53
00:02:38,770 --> 00:02:41,100
You can see all the ConfigMaps you have.

54
00:02:41,100 --> 00:02:44,853
In this case, one ConfigMap with one piece of data in there.

55
00:02:45,890 --> 00:02:48,440
Now we wanna utilize this ConfigMap

56
00:02:48,440 --> 00:02:51,963
to set the environment variables for this container.

57
00:02:52,810 --> 00:02:55,240
And to do that, the env key here

58
00:02:55,240 --> 00:02:59,020
supports a special syntax so to say.

59
00:02:59,020 --> 00:03:00,970
Instead of setting a value

60
00:03:00,970 --> 00:03:03,920
for your environment variable like this,

61
00:03:03,920 --> 00:03:08,920
you can also replace value with a value from key.

62
00:03:10,150 --> 00:03:12,710
You still need the name because you still need

63
00:03:12,710 --> 00:03:15,550
to define the name of the environment variable

64
00:03:15,550 --> 00:03:17,690
by which it is exposed in your code.

65
00:03:17,690 --> 00:03:19,890
So that's still set up here,

66
00:03:19,890 --> 00:03:23,890
but we now have value from instead of value

67
00:03:23,890 --> 00:03:27,150
and with value from you now can add

68
00:03:27,150 --> 00:03:32,150
a nested ConfigMapKeyRef, which stands for a key reference.

69
00:03:33,520 --> 00:03:37,130
Which allows you to point at a specific ConfigMap

70
00:03:37,130 --> 00:03:40,940
and there in that map a specific data key,

71
00:03:40,940 --> 00:03:44,480
which holds the value you wanna set as a value

72
00:03:44,480 --> 00:03:47,233
for this environment variable in this container.

73
00:03:48,430 --> 00:03:52,640
For this ConfigMapKeyRef takes another nested object

74
00:03:52,640 --> 00:03:55,710
where you specified a name of the ConfigMap.

75
00:03:55,710 --> 00:04:00,710
So in my case, that is this name here, data-store-env,

76
00:04:02,450 --> 00:04:07,450
and then the key name, which value you wanna set as a value

77
00:04:07,530 --> 00:04:10,340
for a story folder in this case.

78
00:04:10,340 --> 00:04:12,693
And in my case to key name is folder.

79
00:04:13,860 --> 00:04:17,700
So I will set folder here as a key name.

80
00:04:17,700 --> 00:04:21,610
And this will now pull the value out of the ConfigMap

81
00:04:21,610 --> 00:04:25,323
and set it as a value for this story folder.

82
00:04:26,380 --> 00:04:31,380
So now if we run kubectl apply-f=deployment.yaml,

83
00:04:32,550 --> 00:04:36,530
We apply the updated deployment, which now pulls the value

84
00:04:36,530 --> 00:04:39,460
for the story folder environment variable

85
00:04:39,460 --> 00:04:43,283
out of that ConfigMap which we created and applied before.

86
00:04:44,150 --> 00:04:47,090
Hence with get deployments,

87
00:04:47,090 --> 00:04:50,540
we see this deployment is up and running,

88
00:04:50,540 --> 00:04:51,990
and if we run get pods

89
00:04:51,990 --> 00:04:54,430
we again, see the old pods are being terminated

90
00:04:54,430 --> 00:04:57,540
and the new pods are getting started.

91
00:04:57,540 --> 00:05:00,980
And of course we can still work with our application

92
00:05:00,980 --> 00:05:04,310
and send our requests there as before.

93
00:05:04,310 --> 00:05:09,230
This all still works because our application didn't change.

94
00:05:09,230 --> 00:05:12,790
We just changed how we get the name off the folder

95
00:05:12,790 --> 00:05:14,620
in which our data is stored.

96
00:05:14,620 --> 00:05:17,830
And as you can see, you can use a ConfigMap

97
00:05:17,830 --> 00:05:22,110
instead of hard coding to value here If you wanna do that.

98
00:05:22,110 --> 00:05:25,780
And that's basically for environment variables already.

99
00:05:25,780 --> 00:05:28,360
With that again, you got full flexibility.

100
00:05:28,360 --> 00:05:31,630
You can use a file which you create as a ConfigMap

101
00:05:31,630 --> 00:05:34,940
and apply, and then get the values from there

102
00:05:34,940 --> 00:05:36,990
or as I showed you before,

103
00:05:36,990 --> 00:05:41,440
you directly add a value here like this.

104
00:05:41,440 --> 00:05:43,570
Both together, of course does not work

105
00:05:43,570 --> 00:05:46,310
you should either set the value here

106
00:05:46,310 --> 00:05:50,570
in your container specification or point at a ConfigMap

107
00:05:50,570 --> 00:05:53,130
from which you wanna get the value.

108
00:05:53,130 --> 00:05:56,810
But with that, we know how to set environment variables.

109
00:05:56,810 --> 00:05:59,090
And of course before that, we also learned

110
00:05:59,090 --> 00:06:00,970
how we can work with volumes.

111
00:06:00,970 --> 00:06:03,690
And that's a lot of important information

112
00:06:03,690 --> 00:06:07,093
about working with data in Kurbenetes clusters.

