1 00:00:02,640 --> 00:00:05,080 ‫Now that we've talked about the four types of Kubernetes 2 00:00:05,300 --> 00:00:08,149 ‫services, let's create some from the command line. 3 00:00:08,780 --> 00:00:11,605 ‫First, we're going to need to create a Deployment 4 00:00:12,410 --> 00:00:14,869 ‫that we can point these services to. Then we'll create 5 00:00:14,870 --> 00:00:17,250 ‫different types of services to point at this Deployment. 6 00:00:18,410 --> 00:00:20,660 ‫In this demo, we're going to create two different shells, 7 00:00:20,750 --> 00:00:23,374 ‫or different windows, into your Kubernetes environment 8 00:00:24,470 --> 00:00:27,709 ‫so we can see what's happening with a watch 9 00:00:27,710 --> 00:00:30,589 ‫command while we're doing it with the other commands. 10 00:00:30,950 --> 00:00:33,349 ‫All right. In my case, I have two windows, one on top of 11 00:00:33,350 --> 00:00:35,509 ‫the other. It doesn't really matter how you do that. 12 00:00:35,540 --> 00:00:38,419 ‫It's just for easier use for you to see what I'm doing. 13 00:00:38,750 --> 00:00:41,270 ‫We're going to use the kubectl get pods. 14 00:00:41,360 --> 00:00:42,360 ‫We're going to add a -w. 15 00:00:43,550 --> 00:00:46,789 ‫This is like a watch command where it will keep 16 00:00:46,790 --> 00:00:49,579 ‫running and show us over time as things change. 17 00:00:50,060 --> 00:00:53,600 ‫You'll notice that this is an option on multiple, different 18 00:00:54,050 --> 00:00:56,745 ‫kubectl commands. We'll do that here just to see what's 19 00:00:56,840 --> 00:00:57,920 ‫happening in the background. 20 00:01:00,119 --> 00:01:01,130 ‫get pods with -w. 21 00:01:04,522 --> 00:01:06,729 ‫Then in my other window, we're going to create a 22 00:01:06,730 --> 00:01:07,730 ‫Deployment. 23 00:01:12,370 --> 00:01:13,590 ‫create deployment. We're going to call this httpenv. 24 00:01:17,840 --> 00:01:20,464 ‫What this is using is a very simple HTTP 25 00:01:21,220 --> 00:01:24,046 ‫Web server that essentially when you hit it with a cURL, 26 00:01:24,460 --> 00:01:27,057 ‫it just spits back all the environment variables it's 27 00:01:27,280 --> 00:01:28,760 ‫getting at the command line. 28 00:01:28,810 --> 00:01:31,701 ‫It's a very simple, rudimentary way for us to just validate 29 00:01:31,780 --> 00:01:34,989 ‫that it's working, but also to get back interesting values 30 00:01:34,990 --> 00:01:36,434 ‫like the name of the pod. 31 00:01:36,880 --> 00:01:37,880 ‫Stuff like that. 32 00:01:38,200 --> 00:01:39,969 ‫Now that we've created that, you'll see at the top that 33 00:01:39,970 --> 00:01:41,529 ‫it's creating containers. 34 00:01:41,590 --> 00:01:44,709 ‫Think of it more like a log where each time something 35 00:01:44,710 --> 00:01:47,405 ‫changes state, or the status changes, or something like 36 00:01:47,650 --> 00:01:49,953 ‫that, you'll see it as a new line printing down 37 00:01:50,650 --> 00:01:51,819 ‫the page like a log file. 38 00:01:52,060 --> 00:01:55,030 ‫You'll see in that last line, it now shows that we have 39 00:01:55,360 --> 00:01:57,849 ‫that pod running in the running state there. 40 00:01:58,390 --> 00:02:00,693 ‫Let's spin up more of these pods so that we can 41 00:02:01,540 --> 00:02:03,794 ‫have more than one of these containers running 42 00:02:04,510 --> 00:02:07,059 ‫behind our service that we'll create a second. 43 00:02:15,030 --> 00:02:18,210 ‫The scale command is useful with a Deployment because 44 00:02:18,240 --> 00:02:20,939 ‫it allows us to change the number of replicas in a single, 45 00:02:20,940 --> 00:02:23,340 ‫friendly command. Of course, with everything in Kubernetes, 46 00:02:23,730 --> 00:02:25,919 ‫there are multiple ways to do this, but this is the easiest 47 00:02:25,920 --> 00:02:27,539 ‫way in our situation here. 48 00:02:28,440 --> 00:02:30,689 ‫All right. Now you'll see that it's created four more pods 49 00:02:30,720 --> 00:02:32,789 ‫and it's showing those there as running. 50 00:02:33,330 --> 00:02:35,290 ‫Now, we're going to create that service. 51 00:02:35,430 --> 00:02:37,169 ‫For that, we use the expose command. 52 00:02:51,930 --> 00:02:54,749 ‫In this case, we're telling it which Deployment to expose 53 00:02:55,140 --> 00:02:57,690 ‫and the port that that Deployment is listening on. 54 00:02:58,290 --> 00:03:01,439 ‫You'll see that this didn't change any of the pods because 55 00:03:01,500 --> 00:03:03,901 ‫this is creating an additional feature that's not 56 00:03:04,620 --> 00:03:05,919 ‫related to the pods themselves. 57 00:03:05,970 --> 00:03:08,460 ‫This is actually going to create a cluster IP by default. 58 00:03:08,490 --> 00:03:10,710 ‫That's the cluster IP type of service. 59 00:03:11,160 --> 00:03:14,099 ‫It will create that in front of the 60 00:03:14,130 --> 00:03:16,796 ‫Deployment. Let's check out that service we just created. 61 00:03:16,797 --> 00:03:18,720 ‫You should see two here. 62 00:03:18,810 --> 00:03:21,064 ‫The first one will be the Kubernetes endpoint. 63 00:03:21,180 --> 00:03:22,349 ‫That's for the API. 64 00:03:23,010 --> 00:03:25,979 ‫Then you'll see our service we just created and its type is 65 00:03:25,980 --> 00:03:29,189 ‫cluster IP. Remember, cluster IP is only usable 66 00:03:29,190 --> 00:03:32,219 ‫inside the cluster for nodes and other pods 67 00:03:32,220 --> 00:03:33,220 ‫to be able to access it. 68 00:03:33,690 --> 00:03:37,229 ‫This IP address we can reach, but it depends 69 00:03:37,230 --> 00:03:38,880 ‫on where we are and whether or not we can reach it. 70 00:03:39,810 --> 00:03:42,554 ‫Since that IP address is only accessible from either the 71 00:03:42,750 --> 00:03:46,139 ‫nodes themselves or other pods 72 00:03:46,170 --> 00:03:47,279 ‫inside the cluster. 73 00:03:47,880 --> 00:03:50,771 ‫When you're on Docker Desktop, if you remember that this is 74 00:03:50,940 --> 00:03:53,819 ‫not running on the host OS of Mac and Windows. 75 00:03:53,820 --> 00:03:56,069 ‫This is technically running in a small Linux VM. 76 00:03:56,580 --> 00:03:59,550 ‫We need to run, in this case, another 77 00:04:00,000 --> 00:04:02,808 ‫pod that's running on that cluster so that we can cURL 78 00:04:03,120 --> 00:04:06,360 ‫it. We don't want to run a Deployment with a replica set. 79 00:04:06,390 --> 00:04:09,479 ‫In this case, we really just want to run one, single pod. 80 00:04:09,990 --> 00:04:13,050 ‫The easiest way to do that is with kubectl run. 81 00:04:14,880 --> 00:04:17,820 ‫We're going to use an alternate generator. 82 00:04:19,160 --> 00:04:20,160 ‫You might remember that from the error earlier. 83 00:04:21,649 --> 00:04:22,762 ‫We'll talk about what generators are in a little bit. 84 00:04:22,930 --> 00:04:25,651 ‫There's a lot going on in this command, so let's break it 85 00:04:25,652 --> 00:04:28,011 ‫down. The kubectl run you've seen before. 86 00:04:28,500 --> 00:04:30,970 ‫The generator is the type of template we want the 87 00:04:31,770 --> 00:04:32,770 ‫run command to use. 88 00:04:33,040 --> 00:04:34,531 ‫We'll talk a little bit more about that in a future lecture. 89 00:04:35,590 --> 00:04:37,774 ‫We're picking the pod template because we only want to run 90 00:04:37,775 --> 00:04:40,559 ‫a pod. We don't want a full Deployment with replica sets or 91 00:04:40,560 --> 00:04:41,990 ‫anything. We're going to call it tmp-shell. 92 00:04:42,850 --> 00:04:43,881 ‫You'll notice that some of these 93 00:04:46,020 --> 00:04:47,210 ‫options are similar to docker run. That's on purpose. 94 00:04:48,030 --> 00:04:49,739 ‫One of the future goals of the 95 00:04:51,150 --> 00:04:54,119 ‫kubectl run command is to act more like a docker run 96 00:04:54,120 --> 00:04:56,521 ‫command. In fact, we'll talk later about how it's 97 00:04:57,210 --> 00:04:59,100 ‫going to eventually, by default, just create a pod for you 98 00:04:59,101 --> 00:05:01,220 ‫and you won't have to use this generator option. 99 00:05:01,490 --> 00:05:04,649 ‫We're going to use the --rm here which means once the 100 00:05:04,650 --> 00:05:06,383 ‫pod stops, go ahead and remove it. 101 00:05:06,570 --> 00:05:09,511 ‫The -it gives us a shell into that pod 102 00:05:09,740 --> 00:05:13,259 ‫once we hit the enter command. Then --image is 103 00:05:13,260 --> 00:05:14,810 ‫telling us what image we prefer. 104 00:05:15,450 --> 00:05:19,019 ‫In this case, we're using my netshoot image, which 105 00:05:19,020 --> 00:05:20,579 ‫is a whole bunch of different networking and Linux 106 00:05:20,580 --> 00:05:21,980 ‫utilities in it. One of which is cURL. 107 00:05:23,290 --> 00:05:26,290 ‫Then with the run command, we can change 108 00:05:26,410 --> 00:05:29,440 ‫the cmd, or the thing that's going to run 109 00:05:29,470 --> 00:05:30,609 ‫inside the container. 110 00:05:31,030 --> 00:05:34,089 ‫We do that with a space -- space. 111 00:05:34,390 --> 00:05:37,569 ‫That tells the run command to stop looking at options 112 00:05:37,840 --> 00:05:41,259 ‫and that anything after that double dashes is the command 113 00:05:41,260 --> 00:05:43,600 ‫to run when I execute this container. 114 00:05:44,320 --> 00:05:47,290 ‫You'll see up at the top that the temp shell pod 115 00:05:47,320 --> 00:05:50,139 ‫is showing up and now it's a status of running, and we're 116 00:05:50,140 --> 00:05:52,059 ‫in the pod with our bash prompt. 117 00:05:52,430 --> 00:05:54,459 ‫It's something we'll talk about in a little bit is that the 118 00:05:54,460 --> 00:05:57,639 ‫service name that we created becomes part 119 00:05:57,760 --> 00:06:00,459 ‫of the DNS name for this service. 120 00:06:00,610 --> 00:06:03,489 ‫We can use...the very similar to what we just did in Docker 121 00:06:03,490 --> 00:06:06,879 ‫and Swarm, we can use the service name to get it with DNS. 122 00:06:10,650 --> 00:06:12,674 ‫Remember, it's running on Port 8888. 123 00:06:13,540 --> 00:06:15,899 ‫When we do that, we get back the response that I would 124 00:06:15,900 --> 00:06:18,959 ‫expect. This httpenv utility 125 00:06:18,990 --> 00:06:22,050 ‫simply dumps all the environment variables on 126 00:06:22,110 --> 00:06:24,070 ‫that machine which is inside that unique 127 00:06:25,110 --> 00:06:28,199 ‫pod, right. You'll see the pod name is the hostname and 128 00:06:28,200 --> 00:06:29,880 ‫other Kubernetes defaults out-of-the-box. 129 00:06:29,881 --> 00:06:31,841 ‫That method will work for any of you, on 130 00:06:32,850 --> 00:06:36,029 ‫any cluster, as long as your kubectl command 131 00:06:36,030 --> 00:06:39,209 ‫line can talk to the API, that command will get you 132 00:06:39,540 --> 00:06:42,510 ‫into a new pod running that image 133 00:06:42,870 --> 00:06:44,399 ‫inside a shell. That's great. 134 00:06:44,730 --> 00:06:47,180 ‫But, if you're on Linux, there's an easier way. 135 00:06:47,190 --> 00:06:50,189 ‫Because the way that Kubernetes works is that all nodes 136 00:06:50,220 --> 00:06:52,770 ‫and containers, by default, can talk to each other 137 00:06:53,010 --> 00:06:55,656 ‫out-of-the-box. The node that you're on, assuming that 138 00:06:55,980 --> 00:06:58,889 ‫you're running Kubernetes on that local machine, has access 139 00:06:58,890 --> 00:07:00,629 ‫to all of these private IP addresses. 140 00:07:00,630 --> 00:07:03,689 ‫You should just be able to cURL the service IP. 141 00:07:04,110 --> 00:07:06,832 ‫Let me get out of my shell and do a kubectl 142 00:07:07,770 --> 00:07:09,119 ‫get service again. 143 00:07:09,480 --> 00:07:13,110 ‫That IP address for the cluster IP should be resolvable 144 00:07:13,380 --> 00:07:16,649 ‫from your host. Your host won't know the 145 00:07:16,800 --> 00:07:19,152 ‫DNS name like we did with inside the pod because 146 00:07:19,890 --> 00:07:21,870 ‫that's exclusive to being inside the cluster. 147 00:07:22,230 --> 00:07:24,959 ‫But, it will know the IP address and be able to access it. 148 00:07:25,010 --> 00:07:26,970 ‫You can cURL the IP address 149 00:07:29,100 --> 00:07:32,129 ‫:8888 just like you would for another machine on 150 00:07:32,130 --> 00:07:34,199 ‫your network. The reason that doesn't work from Docker 151 00:07:34,200 --> 00:07:36,944 ‫Desktop is because you're sitting on Mac or Windows when 152 00:07:37,140 --> 00:07:39,394 ‫you're typing those commands, and they are not 153 00:07:40,110 --> 00:07:42,620 ‫on the same cluster of Kubernetes 154 00:07:43,260 --> 00:07:45,930 ‫as that Linux VM running your Kubernetes for you. 155 00:07:46,680 --> 00:07:49,139 ‫All right. Let's go ahead and leave that stuff running for 156 00:07:49,140 --> 00:07:51,450 ‫the next lecture where we are going to create more 157 00:07:51,510 --> 00:07:52,139 ‫services.