1
00:00:01,623 --> 00:00:04,240
So having a deployment is nice,

2
00:00:04,240 --> 00:00:05,780
but not enough.

3
00:00:05,780 --> 00:00:07,830
I will add a second file here,

4
00:00:07,830 --> 00:00:10,160
and that's the service.yaml file,

5
00:00:10,160 --> 00:00:12,450
where I wanna define the service

6
00:00:12,450 --> 00:00:15,990
for my deployment for this application.

7
00:00:15,990 --> 00:00:18,300
And the service.yaml file of course,

8
00:00:18,300 --> 00:00:21,240
is very similar to the deployment.yaml file,

9
00:00:21,240 --> 00:00:23,460
at least to some extent.

10
00:00:23,460 --> 00:00:26,320
Since it's still a Kubernetes Config File,

11
00:00:26,320 --> 00:00:29,400
we still need to define the API version.

12
00:00:29,400 --> 00:00:33,080
Here, the difference compared to deployment is,

13
00:00:33,080 --> 00:00:35,130
that it's just v1.

14
00:00:35,130 --> 00:00:38,380
And we can see this in the official reference documentation,

15
00:00:38,380 --> 00:00:43,380
there, if you have a look at the service resource,

16
00:00:43,560 --> 00:00:46,610
you see the group is the core group,

17
00:00:46,610 --> 00:00:49,140
and that essentially means that you can omit it.

18
00:00:49,140 --> 00:00:53,440
So it's not core/v1, it's just v1.

19
00:00:53,440 --> 00:00:55,770
Now, I as mentioned before that alone,

20
00:00:55,770 --> 00:00:58,200
also in conjunction with the file name,

21
00:00:58,200 --> 00:01:01,050
won't tell Cooper, Nita is what you wanna create.

22
00:01:01,050 --> 00:01:03,150
Because it ignores the file name,

23
00:01:03,150 --> 00:01:06,020
since that name is totally up to you.

24
00:01:06,020 --> 00:01:09,160
Instead, you need to specify a kind in this case,

25
00:01:09,160 --> 00:01:10,170
the service kind.

26
00:01:10,170 --> 00:01:12,710
Since we wanna create a service resource

27
00:01:12,710 --> 00:01:16,513
and you again see that here in the reference docs as well.

28
00:01:17,760 --> 00:01:20,230
There, you'll also see what else can be defined,

29
00:01:20,230 --> 00:01:21,760
and that's of course again,

30
00:01:21,760 --> 00:01:26,350
the metadata and the specification.

31
00:01:26,350 --> 00:01:28,280
So for the metadata, I again,

32
00:01:28,280 --> 00:01:32,960
wanna give this service a name and I'll name it backend,

33
00:01:32,960 --> 00:01:35,080
the name is of course, up to you.

34
00:01:35,080 --> 00:01:36,400
And then for the spec,

35
00:01:36,400 --> 00:01:39,993
we will again define and configure this service.

36
00:01:42,260 --> 00:01:45,830
And here, if we have a look at the reference doc,

37
00:01:45,830 --> 00:01:47,420
we see that there's again,

38
00:01:47,420 --> 00:01:49,960
a lot we can configure because of course

39
00:01:49,960 --> 00:01:53,720
kubernetes is very powerful and gives us a lot of options.

40
00:01:53,720 --> 00:01:58,070
But again, we typically don't need all these options.

41
00:01:58,070 --> 00:02:01,040
What we do need here though, is a selector.

42
00:02:01,040 --> 00:02:04,120
And I did talk about selectors before.

43
00:02:04,120 --> 00:02:07,790
A selector identifies which other resources

44
00:02:07,790 --> 00:02:12,790
should be controlled or connected to this resource.

45
00:02:13,220 --> 00:02:16,550
In this case here, we wanna define which pods

46
00:02:16,550 --> 00:02:19,180
should be part of this service.

47
00:02:19,180 --> 00:02:20,950
Because as we learned before,

48
00:02:20,950 --> 00:02:23,210
a service object exposes pods

49
00:02:23,210 --> 00:02:26,460
to the cluster or to the outside world.

50
00:02:26,460 --> 00:02:28,930
So you don't control deployments with a service,

51
00:02:28,930 --> 00:02:30,900
but instead pods, but of course,

52
00:02:30,900 --> 00:02:33,850
pods are typically generated by a deployment

53
00:02:33,850 --> 00:02:36,050
nonetheless with that selector here,

54
00:02:36,050 --> 00:02:39,100
we wanna select the individual pods.

55
00:02:39,100 --> 00:02:41,170
Now this elector of the service object

56
00:02:41,170 --> 00:02:43,070
works a bit differently than the one

57
00:02:43,070 --> 00:02:44,860
from the deployment object.

58
00:02:44,860 --> 00:02:47,190
There, we had matchlabels

59
00:02:47,190 --> 00:02:50,050
and then our label key value pairs,

60
00:02:50,050 --> 00:02:51,733
which we wanted to connect.

61
00:02:52,700 --> 00:02:54,940
Now, we had this syntax here

62
00:02:54,940 --> 00:02:59,320
because the deployment resource type is a bit newer.

63
00:02:59,320 --> 00:03:02,350
It has been added more recently, you could say,

64
00:03:02,350 --> 00:03:04,560
even though it's also over here around

65
00:03:04,560 --> 00:03:06,010
for quite some time now.

66
00:03:06,010 --> 00:03:08,450
But the service resource is simply older

67
00:03:08,450 --> 00:03:11,530
and there, the selector is simpler.

68
00:03:11,530 --> 00:03:14,110
You don't have matchlabels below it

69
00:03:14,110 --> 00:03:17,100
because you can only match labels here.

70
00:03:17,100 --> 00:03:18,890
So therefore you directly have

71
00:03:18,890 --> 00:03:20,963
to label key value pairs here.

72
00:03:21,810 --> 00:03:25,120
And I wanna control my ports,

73
00:03:25,120 --> 00:03:26,680
which are created like this,

74
00:03:26,680 --> 00:03:29,540
so which have these labels.

75
00:03:29,540 --> 00:03:32,880
Hence we can copy the labels here and add them here

76
00:03:32,880 --> 00:03:36,743
to selector, just make sure the indentation is correct.

77
00:03:37,730 --> 00:03:39,810
Now, you don't always have to select

78
00:03:39,810 --> 00:03:41,860
by all the labels though.

79
00:03:41,860 --> 00:03:45,740
Maybe that is what you want, but maybe in your project,

80
00:03:45,740 --> 00:03:47,700
you also have another deployment,

81
00:03:47,700 --> 00:03:50,820
which creates ports with an app of second app,

82
00:03:50,820 --> 00:03:53,790
but which does not have a tier of backend.

83
00:03:53,790 --> 00:03:55,230
And whilst these ports

84
00:03:55,230 --> 00:03:57,270
would be part of different deployments,

85
00:03:57,270 --> 00:04:00,160
you maybe wanna group them into the same service.

86
00:04:00,160 --> 00:04:03,930
So you could choose to just select by the app label

87
00:04:03,930 --> 00:04:05,900
and ignore the tier label.

88
00:04:05,900 --> 00:04:10,470
So that all ports which have that app label with that value,

89
00:04:10,470 --> 00:04:12,830
would be controlled by that service,

90
00:04:12,830 --> 00:04:15,520
even if they then have different tiers.

91
00:04:15,520 --> 00:04:17,300
Not something we have here,

92
00:04:17,300 --> 00:04:19,670
but something you can do with selectors,

93
00:04:19,670 --> 00:04:22,280
and definitely the idea behind selectors.

94
00:04:22,280 --> 00:04:25,920
The idea is that you have great flexibility when it comes

95
00:04:25,920 --> 00:04:30,920
to expressing which other resource should be connected to

96
00:04:31,110 --> 00:04:33,853
or controlled by some other resource.

97
00:04:34,780 --> 00:04:37,870
So in this case I'm saying this service selects

98
00:04:37,870 --> 00:04:42,870
and therefor exposes all ports with an app label

99
00:04:43,150 --> 00:04:45,370
with a value of second app.

100
00:04:45,370 --> 00:04:48,040
And that of course means that the ports generated

101
00:04:48,040 --> 00:04:51,893
by this deployment will be exposed by this service.

102
00:04:53,230 --> 00:04:56,520
So that's great, now we know which pods should be exposed,

103
00:04:56,520 --> 00:04:59,920
but we don't know yet how they should be exposed

104
00:04:59,920 --> 00:05:02,123
and which ports should be exposed.

105
00:05:03,140 --> 00:05:07,200
And keep in mind that when we used the imperative approach,

106
00:05:07,200 --> 00:05:09,440
we used the expose command

107
00:05:09,440 --> 00:05:12,700
to expose our first app deployment.

108
00:05:12,700 --> 00:05:15,460
And there we added dash dash port

109
00:05:15,460 --> 00:05:18,280
to decide which port should be exposed.

110
00:05:18,280 --> 00:05:22,170
And we add a dash dash type loadBalancer

111
00:05:22,170 --> 00:05:25,970
to decide how it should be exposed basically.

112
00:05:25,970 --> 00:05:27,670
And we can do the same things here.

113
00:05:27,670 --> 00:05:30,680
Let's start with the ports and you can expose more

114
00:05:30,680 --> 00:05:33,920
than one port, which is why this is named ports.

115
00:05:33,920 --> 00:05:36,770
And therefore you then have a list of ports

116
00:05:36,770 --> 00:05:39,220
that can be exposed.

117
00:05:39,220 --> 00:05:40,540
And there, you typically

118
00:05:40,540 --> 00:05:43,680
always define the protocol being used,

119
00:05:43,680 --> 00:05:45,793
which by default here is TCP.

120
00:05:46,820 --> 00:05:50,650
And then on the same list item the port,

121
00:05:50,650 --> 00:05:52,210
you wanna expose it on,

122
00:05:52,210 --> 00:05:56,090
let's say port 80 and the target port

123
00:05:56,090 --> 00:05:59,160
inside of the container in this case 80 80,

124
00:05:59,160 --> 00:06:00,730
because that is the port,

125
00:06:00,730 --> 00:06:03,293
my application is listening on.

126
00:06:05,520 --> 00:06:07,960
So therefore that's the target port, in the container

127
00:06:07,960 --> 00:06:10,500
and that's then the outside world port

128
00:06:10,500 --> 00:06:12,866
on which this should be exposed.

129
00:06:12,866 --> 00:06:15,180
And if you had multiple ports,

130
00:06:15,180 --> 00:06:17,970
you could also of course do that, right?

131
00:06:17,970 --> 00:06:22,470
So if you had another port, if you had another port,

132
00:06:22,470 --> 00:06:27,470
let's say port 443 you could of course also exposed that.

133
00:06:29,020 --> 00:06:31,663
But we don't have it here, so I'll comment this out.

134
00:06:32,920 --> 00:06:37,920
So that's the ports option now for the type option,

135
00:06:39,200 --> 00:06:42,300
you simply add that next to ports.

136
00:06:42,300 --> 00:06:46,550
So not nested below it, but on the same level as ports,

137
00:06:46,550 --> 00:06:48,360
you add your type.

138
00:06:48,360 --> 00:06:50,970
And there you got the types available,

139
00:06:50,970 --> 00:06:54,360
which I described, clusterIP, which is the default,

140
00:06:54,360 --> 00:06:56,900
which is an internally exposed IP.

141
00:06:56,900 --> 00:07:00,458
So only accessible from inside the cluster,

142
00:07:00,458 --> 00:07:03,750
NodePort to basically expose it on the IP

143
00:07:03,750 --> 00:07:05,300
and port of the worker node

144
00:07:05,300 --> 00:07:10,100
on which it runs and most commonly used loadBalancer.

145
00:07:10,100 --> 00:07:12,883
At least if you want outside world access.

146
00:07:13,760 --> 00:07:16,620
This then utilizes the loadBalancer

147
00:07:16,620 --> 00:07:19,870
off your cluster infrastructure, in this case,

148
00:07:19,870 --> 00:07:24,870
minikubes loadBalancer, and you will get an address,

149
00:07:24,900 --> 00:07:27,470
an outside world reachable address,

150
00:07:27,470 --> 00:07:29,830
which then allows you to reach this service

151
00:07:29,830 --> 00:07:32,900
and therefor the ports exposed by that service.

152
00:07:32,900 --> 00:07:37,343
And in addition, incoming traffic is distributed equally.

153
00:07:38,620 --> 00:07:42,410
And with that, we got our service object defined.

154
00:07:42,410 --> 00:07:44,470
That's our service resource

155
00:07:44,470 --> 00:07:46,733
and that's the service we now wanna create.

156
00:07:47,580 --> 00:07:51,730
And we created now just as we created our deployment,

157
00:07:51,730 --> 00:07:56,730
with kube control, apply dash file service.yaml.

158
00:07:56,850 --> 00:07:58,710
It's always the same command

159
00:07:58,710 --> 00:08:00,670
because it's simply looks into the file

160
00:08:00,670 --> 00:08:03,320
to then find out what it needs to create.

161
00:08:03,320 --> 00:08:06,760
So for now had enter this services created.

162
00:08:06,760 --> 00:08:10,450
So if we now get our services, we will see it here.

163
00:08:10,450 --> 00:08:15,340
And of course we can now expose it with minikube service

164
00:08:15,340 --> 00:08:18,603
and then the name you chose here, so in this case backend.

165
00:08:20,200 --> 00:08:22,860
and this will open up a new browser tab

166
00:08:22,860 --> 00:08:25,940
and you can view your application here.

167
00:08:25,940 --> 00:08:27,780
If it doesn't open a new tab, you can,

168
00:08:27,780 --> 00:08:31,340
of course manually use this URL here.

169
00:08:31,340 --> 00:08:34,159
So this is now working.

170
00:08:34,159 --> 00:08:36,950
This is now how we can create our deployment

171
00:08:36,950 --> 00:08:39,340
and our service in a declarative way.

172
00:08:39,340 --> 00:08:43,610
And this is the way and the approach you typically use

173
00:08:43,610 --> 00:08:45,490
because it's easier to maintain.

174
00:08:45,490 --> 00:08:48,370
You don't always have to re-type commands

175
00:08:48,370 --> 00:08:50,340
and it's less error prone.

176
00:08:50,340 --> 00:08:53,130
If you type a command like to Kube control,

177
00:08:53,130 --> 00:08:56,590
expose command, it's easy to mistype something,

178
00:08:56,590 --> 00:08:58,270
or to forget something.

179
00:08:58,270 --> 00:09:00,410
If you have it in your conflict file,

180
00:09:00,410 --> 00:09:03,630
making changes is easy, you don't have to repeat everything.

181
00:09:03,630 --> 00:09:05,440
You can just change one thing,

182
00:09:05,440 --> 00:09:08,300
and it's also very easy to share.

183
00:09:08,300 --> 00:09:10,400
In addition, if a colleague wrote this,

184
00:09:10,400 --> 00:09:13,560
you can quickly see how something needs to be configured,

185
00:09:13,560 --> 00:09:17,010
and you don't have to ask your colleague on how you should

186
00:09:17,010 --> 00:09:19,950
run this command the next time you're running it.

187
00:09:19,950 --> 00:09:22,030
So that's why the declarative approach

188
00:09:22,030 --> 00:09:25,040
is the recommended approach and why it is the approach

189
00:09:25,040 --> 00:09:27,063
We will use throughout this course.

