1
00:00:02,050 --> 00:00:03,880
We need the IP address,

2
00:00:03,880 --> 00:00:07,110
which has generated for this auth-service.

3
00:00:07,110 --> 00:00:08,450
Because as I mentioned,

4
00:00:08,450 --> 00:00:11,700
with services, you get stable IP addresses.

5
00:00:11,700 --> 00:00:14,480
And one service has it's own IP address,

6
00:00:14,480 --> 00:00:16,930
and that IP address will not change,

7
00:00:16,930 --> 00:00:19,100
and through that IP address,

8
00:00:19,100 --> 00:00:22,220
the pods that are controlled by that service,

9
00:00:22,220 --> 00:00:23,750
can be reached.

10
00:00:23,750 --> 00:00:26,600
So that means we need to find out which IP address

11
00:00:26,600 --> 00:00:28,233
this service has.

12
00:00:29,110 --> 00:00:32,740
Now one way of doing that, is of course,

13
00:00:32,740 --> 00:00:34,930
to go into our kubernetes folder,

14
00:00:34,930 --> 00:00:39,930
and to apply the -f=auth-dash-service.yaml file.

15
00:00:40,730 --> 00:00:45,423
And also maybe already the -f=auth-deployment.yaml file.

16
00:00:47,240 --> 00:00:50,130
Apply that and with that of course,

17
00:00:50,130 --> 00:00:51,530
we got a brand new deployment

18
00:00:51,530 --> 00:00:53,310
and a brand new service.

19
00:00:53,310 --> 00:00:56,800
And hence, if we run kubectl get services,

20
00:00:56,800 --> 00:00:59,550
we see that new service here.

21
00:00:59,550 --> 00:01:01,840
It's this auth-service.

22
00:01:01,840 --> 00:01:03,920
And it's this IP address,

23
00:01:03,920 --> 00:01:06,890
which is now available inside of the cluster.

24
00:01:06,890 --> 00:01:09,970
So you will not be able to use that IP address

25
00:01:09,970 --> 00:01:11,410
on your local machine.

26
00:01:11,410 --> 00:01:13,623
It's only available inside of the cluster.

27
00:01:13,623 --> 00:01:17,290
And we could use this IP address here,

28
00:01:17,290 --> 00:01:19,343
as a value instead of local host.

29
00:01:20,570 --> 00:01:22,170
So let's do that.

30
00:01:22,170 --> 00:01:25,030
Let's insert this IP address here,

31
00:01:25,030 --> 00:01:28,290
as a value for AUTH-ADDRESS.

32
00:01:28,290 --> 00:01:31,963
And therefore let's now apply that changed.

33
00:01:34,092 --> 00:01:36,530
f=users-deployment.yaml file,

34
00:01:36,530 --> 00:01:38,443
which uses this new IP address.

35
00:01:40,060 --> 00:01:41,600
And if you now get your pods,

36
00:01:41,600 --> 00:01:45,330
you see the old two container pod is terminating,

37
00:01:45,330 --> 00:01:48,180
and we got two different pods with one container

38
00:01:48,180 --> 00:01:49,410
each up and running.

39
00:01:49,410 --> 00:01:52,623
Because we also got these two deployments.

40
00:01:53,750 --> 00:01:54,960
So that's working.

41
00:01:54,960 --> 00:01:56,510
And if we now try to send

42
00:01:56,510 --> 00:01:59,980
this login request it still succeeds,

43
00:01:59,980 --> 00:02:02,700
the same as true for the signup request,

44
00:02:02,700 --> 00:02:05,100
because we're using this IP address,

45
00:02:05,100 --> 00:02:08,152
which we found for the service we created,

46
00:02:09,180 --> 00:02:10,833
this IP address.

47
00:02:11,800 --> 00:02:14,270
But of course manually getting this IP address,

48
00:02:14,270 --> 00:02:15,600
is a bit annoying.

49
00:02:15,600 --> 00:02:17,940
The good news is that it's stable,

50
00:02:17,940 --> 00:02:21,400
it won't change all the time, so we could do that.

51
00:02:21,400 --> 00:02:24,590
But still as I just said it is a bit annoying.

52
00:02:24,590 --> 00:02:27,580
There is a more convenient way.

53
00:02:27,580 --> 00:02:31,040
Kubernetes will give you automatically generated

54
00:02:31,040 --> 00:02:32,760
environment variables.

55
00:02:32,760 --> 00:02:36,700
In your programs, with information about all the services,

56
00:02:36,700 --> 00:02:38,690
which are running in your cluster.

57
00:02:38,690 --> 00:02:41,430
So here we got the auth and users-service

58
00:02:41,430 --> 00:02:42,690
up and running basically,

59
00:02:42,690 --> 00:02:45,950
and we automatically get environment variables.

60
00:02:45,950 --> 00:02:50,520
In our code, by kubernetes, if we got these services.

61
00:02:50,520 --> 00:02:52,780
And through these environment variables,

62
00:02:52,780 --> 00:02:55,730
kubernetes will automatically give us information,

63
00:02:55,730 --> 00:02:59,283
like the IP addresses of the different services.

64
00:03:00,350 --> 00:03:03,490
So here in users-api, we could, for example,

65
00:03:03,490 --> 00:03:06,513
take advantage of the automatically generated,

66
00:03:07,540 --> 00:03:11,260
environment variables by accessing process.env

67
00:03:11,260 --> 00:03:14,550
here in node js to use an environment variable,

68
00:03:14,550 --> 00:03:16,310
but by using a different name,

69
00:03:16,310 --> 00:03:20,170
a name automatically generated by kubernetes.

70
00:03:20,170 --> 00:03:24,310
And that is your service name,

71
00:03:24,310 --> 00:03:28,360
all caps with dashes replaced by underscores.

72
00:03:28,360 --> 00:03:30,968
So in my case, since I wanna reach out to the auth-service

73
00:03:30,968 --> 00:03:31,850
here,

74
00:03:31,850 --> 00:03:36,850
it would be auth-service transformed to AUTH_SERVICE

75
00:03:37,920 --> 00:03:39,720
all caps,

76
00:03:39,720 --> 00:03:41,760
and then _SERVICE_HOST.

77
00:03:45,940 --> 00:03:49,480
And this pattern, environment variables of this pattern,

78
00:03:49,480 --> 00:03:52,090
are created for all your services.

79
00:03:52,090 --> 00:03:56,435
So the AUTH would be a USERS_SERVICE_SERVICE_HOST

80
00:03:56,435 --> 00:03:58,000
variable here,

81
00:03:58,000 --> 00:04:02,420
because we've got this users-service up and running as well.

82
00:04:02,420 --> 00:04:04,140
But of course we don't need that here,

83
00:04:04,140 --> 00:04:07,840
we need the auth-service environment variable.

84
00:04:07,840 --> 00:04:09,980
And this is automatically generated

85
00:04:09,980 --> 00:04:11,830
and configured by kubernetes.

86
00:04:11,830 --> 00:04:13,870
And this will hold the IP address,

87
00:04:13,870 --> 00:04:17,490
which was automatically assigned for this service,

88
00:04:17,490 --> 00:04:19,572
for this AUTH_SERVICE and this case.

89
00:04:20,810 --> 00:04:21,642
Now, of course,

90
00:04:21,642 --> 00:04:25,360
this kind of hurts us when we try to run

91
00:04:25,360 --> 00:04:27,530
this with docker-compose,

92
00:04:27,530 --> 00:04:29,270
we simply would have to make sure

93
00:04:29,270 --> 00:04:31,840
that we use the same environment variable name here,

94
00:04:31,840 --> 00:04:35,340
but ultimately of course, that's a problem we can solve.

95
00:04:35,340 --> 00:04:37,110
but we then have the advantage

96
00:04:37,110 --> 00:04:40,840
of getting the generated IP address automatically.

97
00:04:40,840 --> 00:04:44,150
And here I will only replace it in the login route,

98
00:04:44,150 --> 00:04:48,260
and not in the signup route so that we see both in action.

99
00:04:48,260 --> 00:04:53,133
Our manual environment variable with the manually set value,

100
00:04:54,380 --> 00:04:55,680
this one here.

101
00:04:55,680 --> 00:04:59,240
And the automatically generated environment variable,

102
00:04:59,240 --> 00:05:01,810
generated by kubernetes.

103
00:05:01,810 --> 00:05:04,810
Again, to make sure that this runs with docker,

104
00:05:04,810 --> 00:05:07,330
you would have to add this kubernetes

105
00:05:07,330 --> 00:05:09,330
generated environment variable,

106
00:05:09,330 --> 00:05:11,653
to your environment variables here,

107
00:05:12,840 --> 00:05:15,060
in the docker-compose file.

108
00:05:15,060 --> 00:05:17,250
Because of course in the docker-compose world,

109
00:05:17,250 --> 00:05:20,378
this is not generated automatically.

110
00:05:20,378 --> 00:05:25,378
With that though, if we redeploy our updated users image,

111
00:05:26,040 --> 00:05:29,470
and then restart our deployment in the end,

112
00:05:29,470 --> 00:05:30,870
it should again work

113
00:05:30,870 --> 00:05:34,373
but now with the auto-generated environment variable.

114
00:05:35,650 --> 00:05:39,270
Hence here, I'll switch into the users-api folder,

115
00:05:39,270 --> 00:05:41,050
and rebuild the image

116
00:05:41,050 --> 00:05:45,843
with the name of academind/kub-demo-users in my case.

117
00:05:46,752 --> 00:05:51,660
And of course I should give that name with the -t flag.

118
00:05:51,660 --> 00:05:53,963
And once this was built, we push it,

119
00:05:56,380 --> 00:05:58,760
using the latest tag like this,

120
00:05:58,760 --> 00:06:01,570
and now this updated code will be pushed.

121
00:06:01,570 --> 00:06:06,570
And now if we go out of this into the kubernetes folder,

122
00:06:07,360 --> 00:06:12,360
we can run kubctl apply -f=users-deployment.yaml file,

123
00:06:14,750 --> 00:06:16,680
and to ensure that this happens here,

124
00:06:16,680 --> 00:06:21,330
I will simply delete my currently running deployment,

125
00:06:21,330 --> 00:06:22,793
and then apply it again.

126
00:06:24,150 --> 00:06:27,150
Because otherwise this deployment wouldn't do anything,

127
00:06:27,150 --> 00:06:28,800
because the file didn't change

128
00:06:28,800 --> 00:06:32,700
and then kubernetes will not re-pull the image.

129
00:06:32,700 --> 00:06:34,860
For that you would have to use a different tag,

130
00:06:34,860 --> 00:06:36,810
which we change all the time.

131
00:06:36,810 --> 00:06:39,483
But with that, I'm also recreating this.

132
00:06:41,360 --> 00:06:43,640
The old pod is terminating and the new one is up

133
00:06:43,640 --> 00:06:44,670
and running.

134
00:06:44,670 --> 00:06:48,580
And now we should be able to still send both the login

135
00:06:48,580 --> 00:06:50,270
and the signup request.

136
00:06:50,270 --> 00:06:52,380
Though, of course, you should keep in mind,

137
00:06:52,380 --> 00:06:54,230
that for the login request,

138
00:06:54,230 --> 00:06:58,460
we are using this auto-generated environment variable,

139
00:06:58,460 --> 00:07:01,670
which holds the automatically assigned IP address

140
00:07:01,670 --> 00:07:03,783
for this AUTH_SERVICE.

141
00:07:04,620 --> 00:07:07,080
So a signup, of course, it still works.

142
00:07:07,080 --> 00:07:10,880
But if we now test login, that also still works.

143
00:07:10,880 --> 00:07:14,120
Which proves that this auto-generated IP address

144
00:07:14,120 --> 00:07:15,543
does its job.

145
00:07:16,520 --> 00:07:19,070
And that's of course a very useful feature,

146
00:07:19,070 --> 00:07:23,190
since it makes this whole gets the IP address process.

147
00:07:23,190 --> 00:07:25,063
Very, very dynamic.

