1 1 00:00:02,630 --> 00:00:05,629 All right. This lecture is starting off where the last one 2 2 00:00:05,720 --> 00:00:07,452 ended. So, if you're just jumping into this one and you 3 3 00:00:07,670 --> 00:00:09,589 skipped the last one, go back to that one because you'll 4 4 00:00:09,590 --> 00:00:12,530 need the Deployment that we created in that one 5 5 00:00:12,590 --> 00:00:13,590 to start this one. 6 6 00:00:13,910 --> 00:00:16,323 When I do a kubectl get all, I 7 7 00:00:19,200 --> 00:00:22,230 should see here that I have one Deployment 8 8 00:00:22,470 --> 00:00:24,179 that has a replica set of five. 9 9 00:00:24,180 --> 00:00:25,379 You'll see the five pods. 10 10 00:00:25,770 --> 00:00:28,739 Then you'll have the Kubernetes default service, 11 11 00:00:28,740 --> 00:00:31,442 which is always there. Then the httpenv 12 12 00:00:32,159 --> 00:00:34,229 cluster IP service. 13 13 00:00:34,570 --> 00:00:36,809 You, of course, may have different IP addresses and 14 14 00:00:36,810 --> 00:00:38,909 different pod names. That's where we need to be. 15 15 00:00:39,030 --> 00:00:40,169 We have this Deployment. 16 16 00:00:40,350 --> 00:00:43,499 We've created an IP address inside the cluster for others 17 17 00:00:43,500 --> 00:00:46,023 to access it with the friendly name of httpenv 18 18 00:00:46,710 --> 00:00:47,880 because that's the service name. 19 19 00:00:48,330 --> 00:00:51,329 Let's create one that's exposed externally on 20 20 00:00:51,330 --> 00:00:54,389 the host IP. This would be, if you were maybe in 21 21 00:00:54,390 --> 00:00:57,179 a data center or somewhere that you didn't have a load 22 22 00:00:57,180 --> 00:01:00,689 balancer you could control with the load balancer 23 23 00:01:01,050 --> 00:01:02,430 service, which we'll talk about a minute. 24 24 00:01:02,790 --> 00:01:04,991 You just basically, in this case, want to do a NodePort 25 25 00:01:05,790 --> 00:01:08,639 where you get ports somewhere in the high range 26 26 00:01:09,030 --> 00:01:10,941 of your Kubernetes cluster that you can 27 27 00:01:12,120 --> 00:01:14,280 use to talk from the outside in. 28 28 00:01:14,310 --> 00:01:16,760 So it will be able to access it from some external 29 29 00:01:17,550 --> 00:01:20,310 service, right. It's not going to be on port 80 by default. 30 30 00:01:20,580 --> 00:01:23,069 The NodePorts are meant to be high ports so you're usually 31 31 00:01:23,070 --> 00:01:25,499 putting something in front of that, like an external load 32 32 00:01:25,500 --> 00:01:28,469 balancer or some automated service that will manage 33 33 00:01:28,470 --> 00:01:31,349 that for you. In this case, I can do a 34 34 00:01:31,680 --> 00:01:33,300 kubectl expose again. 35 35 00:01:34,200 --> 00:01:35,680 It's going to look like the last one we did. 36 36 00:01:36,450 --> 00:01:38,549 deployment httpenv. 37 37 00:01:39,020 --> 00:01:40,620 That's the one we need to expose. 38 38 00:01:41,040 --> 00:01:43,680 We're going to put in port 8888 again. 39 39 00:01:44,670 --> 00:01:47,279 But, we're going to give it a new name because we already 40 40 00:01:47,280 --> 00:01:49,463 have one called httpenv. 41 41 00:01:50,100 --> 00:01:51,359 So, this one we're going to call a 42 42 00:01:53,250 --> 00:01:56,099 httpenv-np for no port. 43 43 00:01:56,613 --> 00:01:59,789 That way, it will work side by side with 44 44 00:01:59,850 --> 00:02:01,439 the cluster IP one that's already there. 45 45 00:02:02,220 --> 00:02:04,878 Then we're going to change the type to NodePort. 46 46 00:02:05,640 --> 00:02:08,580 You'll notice that type of NodePort, because the default 47 47 00:02:08,610 --> 00:02:11,219 type, which we can specify manually if you want to. 48 48 00:02:11,400 --> 00:02:13,139 The default type is cluster IP. 49 49 00:02:14,080 --> 00:02:16,998 If I go back and do a kubectl get 50 50 00:02:17,140 --> 00:02:18,140 services, you'll 51 51 00:02:20,210 --> 00:02:21,818 see a new one there that's type NodePort. 52 52 00:02:22,410 --> 00:02:24,441 Let's talk about the ports on the right. 53 53 00:02:25,170 --> 00:02:27,889 Those ports, when they have the colon in the middle, are 54 54 00:02:27,890 --> 00:02:30,510 the opposite of what you would see in Docker and Swarm. 55 55 00:02:30,770 --> 00:02:32,632 The port on the left, in this case, is 56 56 00:02:33,860 --> 00:02:36,889 the one inside the cluster, inside the 57 57 00:02:36,890 --> 00:02:37,890 container itself that's listing. 58 58 00:02:38,550 --> 00:02:41,899 Then the port on the right is the port that's on 59 59 00:02:41,900 --> 00:02:44,533 your nodes exposed to the outside world. 60 60 00:02:45,360 --> 00:02:47,170 You might wonder where the port on the right came from. 61 61 00:02:47,400 --> 00:02:50,632 That port is from a default range 62 62 00:02:51,140 --> 00:02:53,639 for NodePorts that are preset inside your container 63 63 00:02:53,961 --> 00:02:54,961 cluster. 64 64 00:02:55,670 --> 00:02:58,063 It's a high range. The hope here is that there's 65 65 00:02:59,060 --> 00:03:00,470 no conflicts, right. That there's nothing else on your 66 66 00:03:00,471 --> 00:03:02,052 system, by default, running on those ports. 67 67 00:03:02,450 --> 00:03:04,950 Because often the low ports, like 8888, are 68 68 00:03:07,640 --> 00:03:09,199 common enough that they might be used by something else. Then any 69 69 00:03:09,200 --> 00:03:10,813 ports in the really low range are 70 70 00:03:12,800 --> 00:03:14,620 usually only accessible by root. Ports 1024 and below. 71 71 00:03:14,740 --> 00:03:16,395 There's a few things to note here. 72 72 00:03:17,430 --> 00:03:18,900 The first one is you'll see the cluster IP. 73 73 00:03:19,000 --> 00:03:21,619 Even though when we created this NodePort 74 74 00:03:22,100 --> 00:03:23,766 service, we didn't say cluster IP. 75 75 00:03:24,480 --> 00:03:25,480 We said NodePort. 76 76 00:03:26,230 --> 00:03:27,550 These services are additive. 77 77 00:03:28,320 --> 00:03:31,339 What that means is it's starting with cluster IP, and 78 78 00:03:31,340 --> 00:03:33,937 then going to NodePort, and then to the load balancer 79 79 00:03:34,970 --> 00:03:37,420 optional service. Each one of them will create the 80 80 00:03:38,030 --> 00:03:39,461 services above it in this list. 81 81 00:03:39,580 --> 00:03:42,040 Which means that when I create a NodePort, it 82 82 00:03:43,880 --> 00:03:45,970 creates a cluster IP endpoint for me because 83 83 00:03:47,840 --> 00:03:50,539 that's how it connects. It takes the connection coming into 84 84 00:03:50,540 --> 00:03:52,939 that port on the high port of the node and then it 85 85 00:03:52,940 --> 00:03:55,721 redirects it to the cluster IP for that service. 86 86 00:03:56,510 --> 00:03:59,239 Of course, like most things in Kubernetes, all this is 87 87 00:03:59,240 --> 00:04:00,340 changeable and customizable. 88 88 00:04:01,550 --> 00:04:02,860 One of the things you can change is the port range. 89 89 00:04:03,430 --> 00:04:05,500 One of them is you can avoid the cluster IP. 90 90 00:04:05,600 --> 00:04:07,854 A lot of that stuff you can do, but most of it 91 91 00:04:08,780 --> 00:04:10,930 you can only do in YAML, which we will get to later. 92 92 00:04:11,090 --> 00:04:13,010 When you're doing stuff at the command line like this, 93 93 00:04:13,011 --> 00:04:15,521 we're limited on the number of options we can use. 94 94 00:04:15,980 --> 00:04:18,949 If you go beyond what those options allow, you're going 95 95 00:04:18,950 --> 00:04:21,588 to need to start doing YAML. We'll talk about that later in 96 96 00:04:21,589 --> 00:04:24,350 the course. OK. If you're on Linux, this 97 97 00:04:24,690 --> 00:04:26,430 NodePort is accessible on localhost now. 98 98 00:04:26,431 --> 00:04:29,324 So, you can use localhost, or the host IP itself, to 99 99 00:04:29,930 --> 00:04:31,841 cURL this port. Whatever port you have. 100 100 00:04:32,511 --> 00:04:34,579 It's probably not going to be the same port as mine since 101 101 00:04:34,580 --> 00:04:35,580 mine's 32334. 102 102 00:04:36,220 --> 00:04:37,720 If you're on Docker Desktop, it 103 103 00:04:39,620 --> 00:04:41,229 will provide a convenience layer with something called 104 104 00:04:41,230 --> 00:04:44,299 vpnkit that's just built into Docker Desktop where 105 105 00:04:44,300 --> 00:04:47,044 it will connect this to the localhost on your Windows or 106 106 00:04:47,420 --> 00:04:48,199 Mac machine. 107 107 00:04:48,200 --> 00:04:50,569 I can sit here on my Mac and cURL 108 108 00:04:51,920 --> 00:04:52,920 localhost 32334. 109 109 00:04:56,500 --> 00:04:59,549 I get back that same response as if I cURLed the cluster 110 110 00:04:59,580 --> 00:05:02,639 IP directly. The reason that this is here on 111 111 00:05:03,210 --> 00:05:05,579 Docker Desktop is so that you don't have to jump into those 112 112 00:05:05,580 --> 00:05:08,324 pods like we did earlier just so that you can access the 113 113 00:05:08,490 --> 00:05:12,060 service. This is one way to have your Kubernetes services 114 114 00:05:12,330 --> 00:05:15,410 shown up on your machines so that you can test things, 115 115 00:05:15,420 --> 00:05:16,980 right. So, if you're developing or testing inside 116 116 00:05:17,250 --> 00:05:19,943 Kubernetes, you can then just do a NodePort 117 117 00:05:20,550 --> 00:05:23,100 and go to that port, which is going to be a little random 118 118 00:05:23,550 --> 00:05:24,629 on your host. 119 119 00:05:24,960 --> 00:05:27,959 Again, you can specify an exact port there, 120 120 00:05:28,080 --> 00:05:30,360 but you'd have to do that in YAML, which we'll do later. 121 121 00:05:30,990 --> 00:05:34,079 The other option, if you're on Docker Desktop, is that it 122 122 00:05:34,080 --> 00:05:36,990 comes with a built-in load balancer service. 123 123 00:05:37,260 --> 00:05:39,509 You remember me talking earlier when we were talking about 124 124 00:05:39,510 --> 00:05:42,200 these four types of services, is that the load balancer 125 125 00:05:42,720 --> 00:05:45,299 wasn't built in, by default, and the only way you could use 126 126 00:05:45,300 --> 00:05:47,554 it was through an external service, usually in 127 127 00:05:48,300 --> 00:05:49,300 a cloud service, right. 128 128 00:05:49,560 --> 00:05:51,464 With Amazon, that would be ELBs and ALBs. 129 129 00:05:52,500 --> 00:05:55,079 With DigitalOcean, that would be with their load balancers. 130 130 00:05:55,530 --> 00:05:58,154 You would need to add that plugin in your Kubernetes so it 131 131 00:05:58,470 --> 00:06:00,320 would work with those external load balancers. 132 132 00:06:00,720 --> 00:06:04,170 Essentially, the kube API talks to their API 133 133 00:06:04,380 --> 00:06:05,660 to control that load balancer. 134 134 00:06:06,540 --> 00:06:09,570 If you're on Docker Desktop, then you can try this command. 135 135 00:06:18,540 --> 00:06:19,540 We'll call it httpenv-lb 136 136 00:06:21,690 --> 00:06:24,899 for this service, and we'll change the type to load 137 137 00:06:24,900 --> 00:06:25,900 balancer. 138 138 00:06:25,929 --> 00:06:29,370 If we do a kubectl get service, we should see three 139 139 00:06:29,430 --> 00:06:31,190 load balancers for this Deployment. 140 140 00:06:35,140 --> 00:06:36,939 Again, if you're on Docker Desktop, you'll be able to see 141 141 00:06:36,940 --> 00:06:39,999 all three. If you're on Linux, or using 142 142 00:06:40,210 --> 00:06:42,970 Minikube, or something like that, you would only see two. 143 143 00:06:43,270 --> 00:06:45,699 Each load balancer, because it's technically like a 144 144 00:06:45,700 --> 00:06:48,344 third-party plugin or service that's a remote API, the 145 145 00:06:49,210 --> 00:06:52,101 load balancers will all have varying features and supported 146 146 00:06:52,180 --> 00:06:54,459 stuff. You can sort of look at their documentation to 147 147 00:06:54,460 --> 00:06:55,540 figure out what they'll do. 148 148 00:06:55,840 --> 00:06:58,959 In this case, with Docker Desktop, all we did is we told 149 149 00:06:58,960 --> 00:07:01,940 it the port 8888 that was a 150 150 00:07:01,960 --> 00:07:04,630 part of the Deployment, and the load balancer service 151 151 00:07:05,080 --> 00:07:06,991 plugin from Docker will then publish it 152 152 00:07:08,050 --> 00:07:09,609 on port 8888. 153 153 00:07:10,000 --> 00:07:13,029 The nice thing here is that that is the one way 154 154 00:07:13,450 --> 00:07:16,480 to have your Kubernetes on a specific port 155 155 00:07:16,600 --> 00:07:19,218 outside of the default set, right. 156 156 00:07:19,780 --> 00:07:22,959 So, when we did the NodePort, we were defined, 157 157 00:07:22,960 --> 00:07:26,089 or limited, to that set in the 30000 158 158 00:07:26,200 --> 00:07:28,569 range. If you wanted to do something like you would with 159 159 00:07:28,570 --> 00:07:31,265 Compose, or Swarm, or Docker run locally where you just 160 160 00:07:31,600 --> 00:07:34,240 want to run it on a port like 80 or whatever, 161 161 00:07:34,840 --> 00:07:37,485 you can now with Docker Desktop, using the built-in 162 162 00:07:37,960 --> 00:07:41,079 Kubernetes, you can do that load balancer and just type 163 163 00:07:41,080 --> 00:07:43,420 in that port there, and it will be responsive there. 164 164 00:07:43,450 --> 00:07:46,187 Now on Mac or Windows, I could type cURL 165 165 00:07:47,410 --> 00:07:50,439 localhost 8888 and get 166 166 00:07:50,440 --> 00:07:52,659 that response back. You'll notice there for the load 167 167 00:07:52,660 --> 00:07:55,690 balancer that it still has that other port. 168 168 00:07:55,750 --> 00:07:58,151 Again, that's because the 32 range there, on mine 169 169 00:07:58,930 --> 00:08:01,660 at least, is the built in 170 170 00:08:02,080 --> 00:08:04,149 NodePort that it's creating. Technically in the background, 171 171 00:08:04,150 --> 00:08:06,609 remember these are three different layers so the load 172 172 00:08:06,610 --> 00:08:09,220 balancer is accepting my packet, then passing it to the 173 173 00:08:09,610 --> 00:08:12,099 NodePort, and then the NodePort is passing it to the 174 174 00:08:12,100 --> 00:08:14,950 cluster IP. There's always going to be that 175 175 00:08:15,670 --> 00:08:18,279 NodePort shown there next to the 176 176 00:08:18,670 --> 00:08:21,218 four 8's for a load balancer, even though that's not 177 177 00:08:21,640 --> 00:08:24,142 really the port the load balancer is using on my localhost. 178 178 00:08:24,940 --> 00:08:26,979 Before going to the next lecture, let's clean up everything 179 179 00:08:26,980 --> 00:08:29,079 we just did. We created a lot of stuff. 180 180 00:08:29,470 --> 00:08:31,989 These commands show you that you can use the delete command 181 181 00:08:31,990 --> 00:08:33,729 with multiple objects at the same time. 182 182 00:08:33,789 --> 00:08:35,349 They don't have to be related objects. 183 183 00:08:35,590 --> 00:08:37,209 In fact, you could just make a big list of them if you 184 184 00:08:37,210 --> 00:08:38,409 wanted on a single command.