1
00:00:02,230 --> 00:00:04,019
So let's now move on

2
00:00:04,019 --> 00:00:08,610
to letting the users-api communicate with the auth-api.

3
00:00:08,610 --> 00:00:11,050
For that we first of all, need to dive back

4
00:00:11,050 --> 00:00:15,310
into the users-app.js file and edit to code again,

5
00:00:15,310 --> 00:00:20,020
because there we replaced some code with dummy code.

6
00:00:20,020 --> 00:00:20,853
For example,

7
00:00:20,853 --> 00:00:23,800
here in this signup route function,

8
00:00:23,800 --> 00:00:26,350
the hashedPassword which is a dummy text,

9
00:00:26,350 --> 00:00:28,290
that should now be reverted

10
00:00:28,290 --> 00:00:31,433
to making that access request again.

11
00:00:33,180 --> 00:00:37,350
And the same for the login route there,

12
00:00:37,350 --> 00:00:39,840
this dummy response

13
00:00:39,840 --> 00:00:41,240
should be replaced

14
00:00:41,240 --> 00:00:43,660
with the actual response

15
00:00:43,660 --> 00:00:46,423
which we get from sending a axios request.

16
00:00:47,680 --> 00:00:50,950
Now please note that these axois requests

17
00:00:50,950 --> 00:00:53,480
are sent to this domain

18
00:00:53,480 --> 00:00:54,930
or this address,

19
00:00:54,930 --> 00:00:57,150
which is just auth.

20
00:00:57,150 --> 00:00:58,950
I have this code here

21
00:00:58,950 --> 00:01:02,340
because this was set up for docker-compose

22
00:01:02,340 --> 00:01:05,310
and there you learned earlier in the course

23
00:01:05,310 --> 00:01:08,400
that it creates a network for you automatically.

24
00:01:08,400 --> 00:01:10,440
And these service names here

25
00:01:10,440 --> 00:01:14,000
can then be used internally, in that network

26
00:01:14,000 --> 00:01:16,250
in the code that runs in the containers

27
00:01:16,250 --> 00:01:19,020
to send requests to each other.

28
00:01:19,020 --> 00:01:21,750
Hence I'm using the auth-service name here

29
00:01:21,750 --> 00:01:24,663
to send requests to that auth-service.

30
00:01:25,990 --> 00:01:28,930
Now that works for docker-compose

31
00:01:28,930 --> 00:01:30,650
but it will actually not do the trick

32
00:01:30,650 --> 00:01:33,433
like this, at least for kubernetes.

33
00:01:34,300 --> 00:01:36,790
So again, to stay flexible here,

34
00:01:36,790 --> 00:01:40,600
what I will do is I will actually replace this

35
00:01:40,600 --> 00:01:42,490
with an environment variable

36
00:01:42,490 --> 00:01:45,280
so that the exact address which can be used

37
00:01:45,280 --> 00:01:47,710
can be changed depending on the environment

38
00:01:47,710 --> 00:01:49,110
on which we're running.

39
00:01:49,110 --> 00:01:54,110
And for this, we should replace this string here

40
00:01:54,160 --> 00:01:56,570
with the string created with back ticks,

41
00:01:56,570 --> 00:01:58,410
instead of single quotes.

42
00:01:58,410 --> 00:02:00,890
So that's a different character on your keyboard,

43
00:02:00,890 --> 00:02:02,520
which still creates a string.

44
00:02:02,520 --> 00:02:03,660
But in JavaScript,

45
00:02:03,660 --> 00:02:04,820
which we're using here

46
00:02:04,820 --> 00:02:08,070
allows us to inject a dynamic value into the string

47
00:02:08,070 --> 00:02:11,002
with this dollar sign calibrates syntax.

48
00:02:12,150 --> 00:02:14,860
And here we could then expect an environment variable

49
00:02:14,860 --> 00:02:17,983
which is the AUTH_ADDRESS, let's say.

50
00:02:19,130 --> 00:02:23,170
So I'm going to copy that and not just do it up here

51
00:02:23,170 --> 00:02:27,060
but all's is a little bit further below, here.

52
00:02:27,060 --> 00:02:30,210
Use back ticks instead of single quotes here,

53
00:02:30,210 --> 00:02:34,493
and replace auth with this injected environment variable.

54
00:02:36,250 --> 00:02:39,060
Using that we can go to docker-compose,

55
00:02:39,060 --> 00:02:42,370
and then here on the users-service,

56
00:02:42,370 --> 00:02:45,360
we can add the environment key,

57
00:02:45,360 --> 00:02:50,360
and then simply add our AUTH_ADDRESS environment variable,

58
00:02:52,220 --> 00:02:55,800
which we just injected here in the end

59
00:02:55,800 --> 00:02:59,720
and give this a value of auth here,

60
00:02:59,720 --> 00:03:02,270
in this case, in the docker-compose world,

61
00:03:02,270 --> 00:03:03,933
sort of this service name.

62
00:03:04,980 --> 00:03:06,890
And then use a different value

63
00:03:06,890 --> 00:03:11,040
when we create and launch this with kubernetes.

64
00:03:11,040 --> 00:03:11,900
Now I will come back

65
00:03:11,900 --> 00:03:15,370
to which address we need to use there, in a second.

66
00:03:15,370 --> 00:03:18,540
Before we start this users-api

67
00:03:18,540 --> 00:03:21,470
with a new value for AUTH_ADDRESS though,

68
00:03:21,470 --> 00:03:22,390
let's make sure

69
00:03:22,390 --> 00:03:27,030
that we do have a running auth-api container as well.

70
00:03:27,030 --> 00:03:28,260
And for that of course

71
00:03:28,260 --> 00:03:32,060
we need to build this image and push it to dockerhub.

72
00:03:32,060 --> 00:03:34,100
Hence, let's go

73
00:03:34,100 --> 00:03:35,400
into the

74
00:03:36,899 --> 00:03:38,740
auth-api folder

75
00:03:39,920 --> 00:03:44,320
and then run docker built dot and give it a name,

76
00:03:44,320 --> 00:03:46,210
which of course should be a repository

77
00:03:46,210 --> 00:03:48,180
you have on dockerhub.

78
00:03:48,180 --> 00:03:51,320
Hence I will first of all create a new repository,

79
00:03:51,320 --> 00:03:52,880
with Create Repository

80
00:03:52,880 --> 00:03:55,303
and name it, kub-demo-auth.

81
00:03:57,560 --> 00:04:01,570
So that's my repository name, click create.

82
00:04:01,570 --> 00:04:06,530
And then as always, use this as a name for the image.

83
00:04:06,530 --> 00:04:08,913
So here I'll insert this,

84
00:04:10,720 --> 00:04:12,760
and build this image.

85
00:04:12,760 --> 00:04:14,300
And then once it will be built,

86
00:04:14,300 --> 00:04:16,920
we of course wanna push it, just like before.

87
00:04:16,920 --> 00:04:20,709
So docker push and then this repository image,

88
00:04:20,709 --> 00:04:22,917
name to push it to dockerhub.

89
00:04:24,020 --> 00:04:25,170
And once it is pushed

90
00:04:25,170 --> 00:04:27,820
we can use that image to create containers

91
00:04:27,820 --> 00:04:29,973
inside of our kubernetes pods.

92
00:04:30,940 --> 00:04:33,790
So let's wait for this pushing to finish.

93
00:04:33,790 --> 00:04:35,340
And now that it finished,

94
00:04:35,340 --> 00:04:37,810
we can deploy this container.

95
00:04:37,810 --> 00:04:40,230
Now we could create a new deployment,

96
00:04:40,230 --> 00:04:42,910
but keep in mind that at least for the moment,

97
00:04:42,910 --> 00:04:44,930
I wanna have that auth-api

98
00:04:44,930 --> 00:04:48,610
in the same pod as the users-api.

99
00:04:48,610 --> 00:04:51,510
So it should not be in a separate pod.

100
00:04:51,510 --> 00:04:53,920
If we would create a new deployment file,

101
00:04:53,920 --> 00:04:56,940
we would also create a new kind of pod.

102
00:04:56,940 --> 00:04:58,145
Yes, a deployment is able

103
00:04:58,145 --> 00:05:01,090
to create and manage multiple pods,

104
00:05:01,090 --> 00:05:02,942
that's the idea behind the deployment,

105
00:05:02,942 --> 00:05:05,460
but only one kind of pod.

106
00:05:05,460 --> 00:05:07,810
And then when we talk about multiple pods,

107
00:05:07,810 --> 00:05:09,903
we're just talking about the replicas.

108
00:05:10,980 --> 00:05:13,340
So we're not going to create a new deployment,

109
00:05:13,340 --> 00:05:16,350
instead in the existing users-deployment here,

110
00:05:16,350 --> 00:05:18,410
we'll add a new container.

111
00:05:18,410 --> 00:05:21,510
A container which we can give a name of auth,

112
00:05:21,510 --> 00:05:23,550
and which then should use this image name

113
00:05:23,550 --> 00:05:25,690
we just pushed as an image.

114
00:05:25,690 --> 00:05:27,253
That's what I wanna do here.

115
00:05:28,400 --> 00:05:32,540
I'll also add the latest tag after both images,

116
00:05:32,540 --> 00:05:35,850
because as you also learned earlier in this course,

117
00:05:35,850 --> 00:05:37,800
when using the latest tag,

118
00:05:37,800 --> 00:05:39,420
by default kubernetes

119
00:05:39,420 --> 00:05:43,050
always refetches and re-evaluates the image

120
00:05:43,050 --> 00:05:47,210
when something changes about our deployment configuration.

121
00:05:47,210 --> 00:05:48,140
So that will ensure

122
00:05:48,140 --> 00:05:51,290
that we always use the latest source code,

123
00:05:51,290 --> 00:05:53,020
because of course we'll also need

124
00:05:53,020 --> 00:05:56,030
to repush the users-api image soon,

125
00:05:56,030 --> 00:05:58,810
because we also changed something there in the code.

126
00:05:58,810 --> 00:06:01,420
We switched back to sending a real request

127
00:06:01,420 --> 00:06:03,470
using this environment variable,

128
00:06:03,470 --> 00:06:04,880
but that will be a next step.

129
00:06:04,880 --> 00:06:07,530
For the moment let's go back to our deployment file

130
00:06:07,530 --> 00:06:10,253
and make sure you added your auth container here.

131
00:06:11,570 --> 00:06:14,933
Now I don't edit my service file there though,

132
00:06:15,850 --> 00:06:20,850
because whilst my auth-api does listen on a different port,

133
00:06:20,850 --> 00:06:22,680
it listens on port 80.

134
00:06:22,680 --> 00:06:26,590
I don't wanna expose that port to the outside world,

135
00:06:26,590 --> 00:06:31,350
after all the requests should be made Pod-internal.

136
00:06:31,350 --> 00:06:35,520
So the users-api should be reachable from the outside world

137
00:06:35,520 --> 00:06:38,363
but the auth-api should actually not.

138
00:06:39,480 --> 00:06:42,320
So therefore in the service,

139
00:06:42,320 --> 00:06:45,530
I don't wanna expose the auth-app,

140
00:06:45,530 --> 00:06:48,600
the auth container port for the moment.

141
00:06:48,600 --> 00:06:50,310
Instead in this service,

142
00:06:50,310 --> 00:06:52,300
only one port should be exposed

143
00:06:52,300 --> 00:06:54,140
which points at the users-port,

144
00:06:54,140 --> 00:06:56,820
which will only be handled by the users-api,

145
00:06:56,820 --> 00:06:59,360
which is part of the users-port.

146
00:06:59,360 --> 00:07:01,980
The port exposed by the auth-api

147
00:07:01,980 --> 00:07:03,920
is not exposed by this service.

148
00:07:03,920 --> 00:07:07,300
So it shouldn't be reachable through the service.

149
00:07:07,300 --> 00:07:09,770
I hope that makes sense.

150
00:07:09,770 --> 00:07:12,770
So with that we've got both containers in one port

151
00:07:12,770 --> 00:07:14,470
in one deployment.

152
00:07:14,470 --> 00:07:16,600
Now of course we need to make sure

153
00:07:16,600 --> 00:07:19,260
that all of that can be applied.

154
00:07:19,260 --> 00:07:23,820
And for that we need to push the updated users-api image,

155
00:07:23,820 --> 00:07:26,010
and we of course also need to make sure

156
00:07:26,010 --> 00:07:27,780
that in the kubernetes world,

157
00:07:27,780 --> 00:07:31,167
we provide a value for the AUTH_ADDRESS.

158
00:07:32,110 --> 00:07:33,800
Now first things first,

159
00:07:33,800 --> 00:07:36,200
let's first of all,

160
00:07:36,200 --> 00:07:39,010
cd into the users-api folder

161
00:07:39,010 --> 00:07:41,590
and rebuild the image there

162
00:07:41,590 --> 00:07:45,430
with that tag of kub-demo-users,

163
00:07:45,430 --> 00:07:47,810
which was the image name I used before,

164
00:07:47,810 --> 00:07:50,600
and which is the image name being used here

165
00:07:50,600 --> 00:07:53,000
in the deployment yaml file.

166
00:07:53,000 --> 00:07:54,980
And then of course, push it again

167
00:07:56,260 --> 00:07:59,080
so that we have that latest version of that image

168
00:07:59,080 --> 00:08:01,000
with the updated source code,

169
00:08:01,000 --> 00:08:02,657
pushed to dockerhub,

170
00:08:03,700 --> 00:08:06,570
and then let's apply our changes.

171
00:08:06,570 --> 00:08:08,760
And for that, of course, let's ensure

172
00:08:08,760 --> 00:08:11,487
that we provide a value for AUTH_ADDRESS.

173
00:08:12,477 --> 00:08:13,980
And the big question now is,

174
00:08:13,980 --> 00:08:16,460
which value should be provided here

175
00:08:16,460 --> 00:08:19,120
in the kubernetes cluster world?

176
00:08:19,120 --> 00:08:22,890
What's the correct address for that users-api,

177
00:08:22,890 --> 00:08:26,390
to be able to talk to that auth-api,

178
00:08:26,390 --> 00:08:28,480
which lives in the same port,

179
00:08:28,480 --> 00:08:30,323
but in a different container?

