All right. This lecture is starting off where the last one 2 2
ended. So, if you're just jumping into this one and you 3 3
skipped the last one, go back to that one because you'll 4 4
need the Deployment that we created in that one 5 5
to start this one. 6 6
When I do a kubectl get all, I 7 7
should see here that I have one Deployment 8 8
that has a replica set of five. 9 9
You'll see the five pods. 10 10
Then you'll have the Kubernetes default service, 11 11
which is always there. Then the httpenv 12 12
cluster IP service. 13 13
You, of course, may have different IP addresses and 14 14
different pod names. That's where we need to be. 15 15
We have this Deployment. 16 16
We've created an IP address inside the cluster for others 17 17
to access it with the friendly name of httpenv 18 18
because that's the service name. 19 19
Let's create one that's exposed externally on 20 20
the host IP. This would be, if you were maybe in 21 21
a data center or somewhere that you didn't have a load 22 22
balancer you could control with the load balancer 23 23
service, which we'll talk about a minute. 24 24
You just basically, in this case, want to do a NodePort 25 25
where you get ports somewhere in the high range 26 26
of your Kubernetes cluster that you can 27 27
use to talk from the outside in. 28 28
So it will be able to access it from some external 29 29
service, right. It's not going to be on port 80 by default. 30 30
The NodePorts are meant to be high ports so you're usually 31 31
putting something in front of that, like an external load 32 32
balancer or some automated service that will manage 33 33
that for you. In this case, I can do a 34 34
kubectl expose again. 35 35
It's going to look like the last one we did. 36 36
deployment httpenv. 37 37
That's the one we need to expose. 38 38
We're going to put in port 8888 again. 39 39
But, we're going to give it a new name because we already 40 40
have one called httpenv. 41 41
So, this one we're going to call a 42 42
httpenv-np for no port. 43 43
That way, it will work side by side with 44 44
the cluster IP one that's already there. 45 45
Then we're going to change the type to NodePort. 46 46
You'll notice that type of NodePort, because the default 47 47
type, which we can specify manually if you want to. 48 48
The default type is cluster IP. 49 49
If I go back and do a kubectl get 50 50
services, you'll 51 51
see a new one there that's type NodePort. 52 52
Let's talk about the ports on the right. 53 53
Those ports, when they have the colon in the middle, are 54 54
the opposite of what you would see in Docker and Swarm. 55 55
The port on the left, in this case, is 56 56
the one inside the cluster, inside the 57 57
container itself that's listing. 58 58
Then the port on the right is the port that's on 59 59
your nodes exposed to the outside world. 60 60
You might wonder where the port on the right came from. 61 61
That port is from a default range 62 62
for NodePorts that are preset inside your container 63 63
cluster. 64 64
It's a high range. The hope here is that there's 65 65
no conflicts, right. That there's nothing else on your 66 66
system, by default, running on those ports. 67 67
Because often the low ports, like 8888, are 68 68
common enough that they might be used by something else. Then any 69 69
ports in the really low range are 70 70
usually only accessible by root. Ports 1024 and below. 71 71
There's a few things to note here. 72 72
The first one is you'll see the cluster IP. 73 73
Even though when we created this NodePort 74 74
service, we didn't say cluster IP. 75 75
We said NodePort. 76 76
These services are additive. 77 77
What that means is it's starting with cluster IP, and 78 78
then going to NodePort, and then to the load balancer 79 79
optional service. Each one of them will create the 80 80
services above it in this list. 81 81
Which means that when I create a NodePort, it 82 82
creates a cluster IP endpoint for me because 83 83
that's how it connects. It takes the connection coming into 84 84
that port on the high port of the node and then it 85 85
redirects it to the cluster IP for that service. 86 86
Of course, like most things in Kubernetes, all this is 87 87
changeable and customizable. 88 88
One of the things you can change is the port range. 89 89
One of them is you can avoid the cluster IP. 90 90
A lot of that stuff you can do, but most of it 91 91
you can only do in YAML, which we will get to later. 92 92
When you're doing stuff at the command line like this, 93 93
we're limited on the number of options we can use. 94 94
If you go beyond what those options allow, you're going 95 95
to need to start doing YAML. We'll talk about that later in 96 96
the course. OK. If you're on Linux, this 97 97
NodePort is accessible on localhost now. 98 98
So, you can use localhost, or the host IP itself, to 99 99
cURL this port. Whatever port you have. 100 100
It's probably not going to be the same port as mine since 101 101
mine's 32334. 102 102
If you're on Docker Desktop, it 103 103
will provide a convenience layer with something called 104 104
vpnkit that's just built into Docker Desktop where 105 105
it will connect this to the localhost on your Windows or 106 106
Mac machine. 107 107
I can sit here on my Mac and cURL 108 108
localhost 32334. 109 109
I get back that same response as if I cURLed the cluster 110 110
IP directly. The reason that this is here on 111 111
Docker Desktop is so that you don't have to jump into those 112 112
pods like we did earlier just so that you can access the 113 113
service. This is one way to have your Kubernetes services 114 114
shown up on your machines so that you can test things, 115 115
right. So, if you're developing or testing inside 116 116
Kubernetes, you can then just do a NodePort 117 117
and go to that port, which is going to be a little random 118 118
on your host. 119 119
Again, you can specify an exact port there, 120 120
but you'd have to do that in YAML, which we'll do later. 121 121
The other option, if you're on Docker Desktop, is that it 122 122
comes with a built-in load balancer service. 123 123
You remember me talking earlier when we were talking about 124 124
these four types of services, is that the load balancer 125 125
wasn't built in, by default, and the only way you could use 126 126
it was through an external service, usually in 127 127
a cloud service, right. 128 128
With Amazon, that would be ELBs and ALBs. 129 129
With DigitalOcean, that would be with their load balancers. 130 130
You would need to add that plugin in your Kubernetes so it 131 131
would work with those external load balancers. 132 132
Essentially, the kube API talks to their API 133 133
to control that load balancer. 134 134
If you're on Docker Desktop, then you can try this command. 135 135
We'll call it httpenv-lb 136 136
for this service, and we'll change the type to load 137 137
balancer. 138 138
If we do a kubectl get service, we should see three 139 139
load balancers for this Deployment. 140 140
Again, if you're on Docker Desktop, you'll be able to see 141 141
all three. If you're on Linux, or using 142 142
Minikube, or something like that, you would only see two. 143 143
Each load balancer, because it's technically like a 144 144
third-party plugin or service that's a remote API, the 145 145
load balancers will all have varying features and supported 146 146
stuff. You can sort of look at their documentation to 147 147
figure out what they'll do. 148 148
In this case, with Docker Desktop, all we did is we told 149 149
it the port 8888 that was a 150 150
part of the Deployment, and the load balancer service 151 151
plugin from Docker will then publish it 152 152
on port 8888. 153 153
The nice thing here is that that is the one way 154 154
to have your Kubernetes on a specific port 155 155
outside of the default set, right. 156 156
So, when we did the NodePort, we were defined, 157 157
or limited, to that set in the 30000 158 158
range. If you wanted to do something like you would with 159 159
Compose, or Swarm, or Docker run locally where you just 160 160
want to run it on a port like 80 or whatever, 161 161
you can now with Docker Desktop, using the built-in 162 162
Kubernetes, you can do that load balancer and just type 163 163
in that port there, and it will be responsive there. 164 164
Now on Mac or Windows, I could type cURL 165 165
localhost 8888 and get 166 166
that response back. You'll notice there for the load 167 167
balancer that it still has that other port. 168 168
Again, that's because the 32 range there, on mine 169 169
at least, is the built in 170 170
NodePort that it's creating. Technically in the background, 171 171
remember these are three different layers so the load 172 172
balancer is accepting my packet, then passing it to the 173 173
NodePort, and then the NodePort is passing it to the 174 174
cluster IP. There's always going to be that 175 175
NodePort shown there next to the 176 176
four 8's for a load balancer, even though that's not 177 177
really the port the load balancer is using on my localhost. 178 178
Before going to the next lecture, let's clean up everything 179 179
we just did. We created a lot of stuff. 180 180
These commands show you that you can use the delete command 181 181
with multiple objects at the same time. 182 182
They don't have to be related objects. 183 183
In fact, you could just make a big list of them if you 184 184
wanted on a single command.