1 00:00:02,890 --> 00:00:05,340 ‫There are two pieces of metadata in this YAML that 2 00:00:06,160 --> 00:00:08,218 ‫you probably have seen in the examples and 3 00:00:09,250 --> 00:00:12,249 ‫thought about, wondering what labels and annotations 4 00:00:12,250 --> 00:00:13,250 ‫are really there for. 5 00:00:13,930 --> 00:00:15,910 ‫You probably didn't realize their significance. 6 00:00:15,940 --> 00:00:18,999 ‫They're a key component of Kubernetes and 7 00:00:19,000 --> 00:00:20,437 ‫how things connect to each other. 8 00:00:20,438 --> 00:00:21,879 ‫Much more so than Swarm. 9 00:00:22,580 --> 00:00:25,600 ‫The first one is labels. Labels go under the metadata 10 00:00:25,930 --> 00:00:28,980 ‫key in the root of the file, so they're 11 00:00:28,990 --> 00:00:31,685 ‫optional there. You don't have to put them but they are 12 00:00:32,200 --> 00:00:35,439 ‫a key value, or a list of key values, underneath 13 00:00:35,560 --> 00:00:36,640 ‫the labels heading. 14 00:00:36,850 --> 00:00:38,950 ‫Those are there for multiple reasons. 15 00:00:39,190 --> 00:00:42,460 ‫They're not always required, but you may need them for 16 00:00:42,520 --> 00:00:45,699 ‫selecting the objects, grouping them 17 00:00:45,730 --> 00:00:48,129 ‫and even filtering them later on. 18 00:00:48,340 --> 00:00:50,251 ‫This can be anything that describes the 19 00:00:51,310 --> 00:00:52,569 ‫object or the resource. 20 00:00:52,600 --> 00:00:55,899 ‫It can be a key value pair that the value 21 00:00:55,900 --> 00:00:57,039 ‫is simply true or false. 22 00:00:57,340 --> 00:00:58,869 ‫The keys can be whatever you want. 23 00:00:58,900 --> 00:01:01,960 ‫There just can't be more than one key in each object, 24 00:01:01,990 --> 00:01:04,869 ‫so you can't have two keys in a row in the YAML 25 00:01:05,200 --> 00:01:07,869 ‫for the same object that have the key name of, let's say 26 00:01:07,870 --> 00:01:11,499 ‫app, right. These are used really to describe 27 00:01:11,710 --> 00:01:14,500 ‫the object. These resources that we're creating 28 00:01:15,160 --> 00:01:16,571 ‫have different properties to them. 29 00:01:16,572 --> 00:01:19,069 ‫Maybe it's the environment like prod, dev or test. 30 00:01:19,180 --> 00:01:21,760 ‫Maybe it's the location like dmz, or 31 00:01:22,130 --> 00:01:25,239 ‫backend or frontend. Maybe it's the app name, or 32 00:01:25,240 --> 00:01:28,120 ‫maybe it's the client name, if you're hosting client sites 33 00:01:28,150 --> 00:01:30,339 ‫or something. It could just be whatever you want and 34 00:01:30,340 --> 00:01:31,840 ‫there's not a whole lot of standards. 35 00:01:32,170 --> 00:01:34,800 ‫The Kubernetes documentation listed in the resources here 36 00:01:34,820 --> 00:01:37,440 ‫does give you some examples, but those are just examples. 37 00:01:37,450 --> 00:01:39,849 ‫There's not a whole lot of standards in the industry. 38 00:01:39,850 --> 00:01:42,699 ‫It's really up to your team, and your company, about how 39 00:01:42,700 --> 00:01:44,919 ‫you want to describe this thing. 40 00:01:45,290 --> 00:01:48,400 ‫We also have annotations. They're sort of a yin and yang. 41 00:01:48,430 --> 00:01:50,170 ‫The labels are very simple. 42 00:01:50,230 --> 00:01:52,929 ‫They are very limited in size and what characters you can 43 00:01:52,930 --> 00:01:55,930 ‫put in them. They're meant to describe the resource. 44 00:01:56,320 --> 00:01:59,890 ‫With annotations, this is for more complex 45 00:02:00,070 --> 00:02:02,919 ‫data and multi-value data, stuff like that. 46 00:02:03,160 --> 00:02:06,099 ‫You'll usually see annotations used when 47 00:02:06,130 --> 00:02:09,039 ‫you're adding custom stuff into Kubernetes that 48 00:02:09,160 --> 00:02:11,560 ‫can talk to Kubernetes and get data out of it. 49 00:02:11,800 --> 00:02:14,650 ‫The annotations are there to maybe store config data, like 50 00:02:14,860 --> 00:02:17,919 ‫proxies and ingress stuff can often store 51 00:02:17,920 --> 00:02:20,949 ‫different configurations, inside different annotations. 52 00:02:21,220 --> 00:02:23,919 ‫So, those are not necessarily there 53 00:02:24,190 --> 00:02:27,550 ‫for describing but more for configuration. 54 00:02:27,820 --> 00:02:30,429 ‫Let's go back to labels for a minute, because there's a lot 55 00:02:30,430 --> 00:02:33,321 ‫more to that that's key to understanding how all this stuff 56 00:02:33,430 --> 00:02:34,430 ‫works in Kubernetes. 57 00:02:34,870 --> 00:02:37,989 ‫The first thing is that we can do simple filters 58 00:02:38,110 --> 00:02:40,509 ‫by limiting to the labels from the command line. 59 00:02:40,540 --> 00:02:42,843 ‫We can do a get pods and just say, only show me 60 00:02:43,550 --> 00:02:44,949 ‫the pods with this label. 61 00:02:45,310 --> 00:02:47,919 ‫You can even use a comma there to add multiple labels. 62 00:02:47,950 --> 00:02:51,039 ‫In fact, there's a whole level of syntax 63 00:02:51,040 --> 00:02:54,015 ‫to this thing where you can do different types of syntaxes, 64 00:02:54,033 --> 00:02:55,033 ‫different or's and and's. 65 00:02:55,690 --> 00:02:57,699 ‫Really, it's one of those things where I don't want to 66 00:02:57,700 --> 00:03:00,493 ‫teach you all that because it's going to be like learning 67 00:03:00,610 --> 00:03:03,354 ‫Regex. It's something where you won't really learn it by 68 00:03:03,550 --> 00:03:04,810 ‫just trying it once. 69 00:03:04,840 --> 00:03:07,140 ‫It's only when you need it that you're really going to 70 00:03:07,240 --> 00:03:09,699 ‫understand it. The documentation from Kubernetes is really 71 00:03:09,700 --> 00:03:12,009 ‫great. They've got multiple links I've put in the resources 72 00:03:12,370 --> 00:03:15,069 ‫that will take you through the path of all the different 73 00:03:15,070 --> 00:03:16,719 ‫ways that you might need labels. 74 00:03:16,990 --> 00:03:19,734 ‫Quite frankly, you don't really need to learn that until 75 00:03:20,080 --> 00:03:22,479 ‫you know you need it to solve a problem. 76 00:03:23,020 --> 00:03:24,849 ‫You can even use it for apply commands. 77 00:03:24,910 --> 00:03:27,660 ‫If you had a whole bunch of stuff in one big YAML 78 00:03:27,880 --> 00:03:30,183 ‫file and you only wanted to apply certain parts 79 00:03:31,150 --> 00:03:32,150 ‫of that YAML file. 80 00:03:32,720 --> 00:03:35,139 ‫I don't do this a whole lot, but it is there where you 81 00:03:35,140 --> 00:03:38,380 ‫could limit it to say, only apply 82 00:03:38,560 --> 00:03:41,360 ‫the changes or information that you've put in the YAML 83 00:03:41,560 --> 00:03:42,560 ‫for specific labels. 84 00:03:43,000 --> 00:03:44,859 ‫Of course, again with this one, you can put the same 85 00:03:44,860 --> 00:03:46,869 ‫syntax. You can just have a single label. 86 00:03:46,930 --> 00:03:49,509 ‫You can put in commas, have multiple labels and values 87 00:03:49,510 --> 00:03:51,130 ‫there. It's up to you. 88 00:03:51,640 --> 00:03:54,699 ‫There's another thing that the labels do that's 89 00:03:54,790 --> 00:03:56,650 ‫important to how Kubernetes works. 90 00:03:56,800 --> 00:03:59,979 ‫It comes down to there are certain 91 00:04:00,280 --> 00:04:02,730 ‫resources that talk to other resources, or connect 92 00:04:03,550 --> 00:04:05,229 ‫to them, or are related to them. 93 00:04:05,440 --> 00:04:08,199 ‫However you want to say it. The good example is the 94 00:04:08,200 --> 00:04:11,169 ‫services need to know which pods to send their 95 00:04:11,170 --> 00:04:14,042 ‫traffic to. You might wonder how does it know? 96 00:04:14,200 --> 00:04:17,199 ‫I mean, when you type it in, if you do a kubectl service 97 00:04:17,200 --> 00:04:19,539 ‫Command and you're pointing it to the deployment, 98 00:04:20,170 --> 00:04:22,767 ‫what exactly is it using to identify that deployment? 99 00:04:22,830 --> 00:04:25,378 ‫Well, in the CLI, you would think that it's actually 100 00:04:25,660 --> 00:04:27,069 ‫pointing to the deployment object. 101 00:04:27,100 --> 00:04:29,746 ‫But really, the service needs to talk to the pods, not 102 00:04:30,070 --> 00:04:31,070 ‫the deployment itself. 103 00:04:31,660 --> 00:04:32,990 ‫It's using something called label selectors. 104 00:04:32,991 --> 00:04:36,310 ‫This is common with lots of the different resources 105 00:04:36,640 --> 00:04:39,129 ‫that are higher levels of abstraction of pods. 106 00:04:39,730 --> 00:04:42,129 ‫For them to relate to the pods, or to know which pods are 107 00:04:42,130 --> 00:04:44,860 ‫their's, they use label selectors. You will see that 108 00:04:45,310 --> 00:04:47,760 ‫in the YAML inside our repo that you've been using 109 00:04:48,370 --> 00:04:49,959 ‫for the last few lectures. 110 00:04:50,320 --> 00:04:53,679 ‫If you look in the app.yml, you'll notice that all along, 111 00:04:53,710 --> 00:04:55,899 ‫we've had a couple of things in there that we haven't 112 00:04:55,900 --> 00:04:58,449 ‫talked about. The first one is at the top part with the 113 00:04:58,450 --> 00:05:01,870 ‫service, we have under the spec a selector. 114 00:05:02,500 --> 00:05:03,640 ‫That has to be there. 115 00:05:04,000 --> 00:05:07,029 ‫We're telling it the key and value of a label 116 00:05:07,330 --> 00:05:09,970 ‫for pods that it needs to direct traffic to. 117 00:05:10,510 --> 00:05:12,009 ‫That's just a simple match. 118 00:05:12,040 --> 00:05:13,779 ‫It needs to find pods with that. 119 00:05:14,230 --> 00:05:17,259 ‫Then down in the deployment underneath, you'll see that our 120 00:05:17,260 --> 00:05:19,930 ‫spec has its own selector, 121 00:05:20,290 --> 00:05:22,750 ‫matchLabels, app, app-nginx. 122 00:05:24,010 --> 00:05:27,160 ‫What's happening there is both of those objects are 123 00:05:27,220 --> 00:05:29,980 ‫selecting pods based on that match. 124 00:05:30,370 --> 00:05:33,309 ‫That means in the template under our spec, where we're 125 00:05:33,310 --> 00:05:36,519 ‫templating out the pods, that needs to have 126 00:05:36,580 --> 00:05:39,729 ‫that label in it so that those resources 127 00:05:39,730 --> 00:05:40,779 ‫can find their pods. 128 00:05:41,140 --> 00:05:44,049 ‫The nice thing is in the later versions of Kubernetes, is 129 00:05:44,050 --> 00:05:47,139 ‫that at least with just a single deployment, these have 130 00:05:47,140 --> 00:05:50,199 ‫to be in sync. It'll actually fail to deploy if it 131 00:05:50,200 --> 00:05:53,589 ‫sees that the selector match labels 132 00:05:53,680 --> 00:05:56,889 ‫isn't matching the template metadata labels 133 00:05:57,100 --> 00:05:59,679 ‫because they have to in order for it to even deploy the 134 00:05:59,680 --> 00:06:01,059 ‫pods and be able to control them. 135 00:06:01,330 --> 00:06:03,969 ‫It's there to kind of prevent you from accidentally doing 136 00:06:03,970 --> 00:06:06,249 ‫it the wrong way. It will expect you to have those pieces 137 00:06:06,250 --> 00:06:08,740 ‫in there. In older versions, there used to be a way where 138 00:06:08,741 --> 00:06:11,019 ‫it would automatically generate them, by default, so you 139 00:06:11,020 --> 00:06:13,129 ‫could leave the stuff out. They've stopped doing that now, 140 00:06:13,130 --> 00:06:15,489 ‫and they force you to put this in there. I think that's a 141 00:06:15,490 --> 00:06:17,859 ‫good thing, especially how important these labels selectors 142 00:06:17,860 --> 00:06:20,774 ‫are. Technically, if you went back to Swarm, Swarm 143 00:06:21,460 --> 00:06:24,057 ‫does a little bit of this out-of-the-box where it has 144 00:06:24,190 --> 00:06:26,738 ‫labels that match things up from stacks to services, 145 00:06:27,730 --> 00:06:30,100 ‫and stacks to their volumes, and stuff like that. 146 00:06:30,370 --> 00:06:32,379 ‫We don't really get to control it, and we don't see it a 147 00:06:32,380 --> 00:06:33,909 ‫whole lot in the interface. It's kind of done in the 148 00:06:33,910 --> 00:06:34,910 ‫background for us in Swarm. 149 00:06:35,500 --> 00:06:37,120 ‫But, it's also very limiting. 150 00:06:37,150 --> 00:06:39,629 ‫You can't change things on the fly. 151 00:06:39,640 --> 00:06:42,579 ‫You can't necessarily go and customize those for really 152 00:06:42,580 --> 00:06:43,580 ‫unique setups. 153 00:06:43,890 --> 00:06:46,959 ‫Here, we could possibly have a 154 00:06:47,080 --> 00:06:49,138 ‫service talk to multiple deployments if we 155 00:06:50,110 --> 00:06:51,610 ‫wanted to change those selectors. 156 00:06:52,000 --> 00:06:54,819 ‫Selectors aren't just about linking resources together. 157 00:06:54,880 --> 00:06:57,069 ‫It's also about controlling where they go. 158 00:06:57,190 --> 00:07:00,309 ‫On the nodes, you can use labels and selectors 159 00:07:00,310 --> 00:07:01,660 ‫to control where they go. 160 00:07:02,050 --> 00:07:05,080 ‫You can use even further customization with things called 161 00:07:05,110 --> 00:07:07,560 ‫taints and toleration that are sort of the inverse 162 00:07:08,320 --> 00:07:11,499 ‫of that where you're telling nodes that they can't 163 00:07:11,500 --> 00:07:13,852 ‫do certain things so that Kubernetes knows where 164 00:07:14,470 --> 00:07:16,449 ‫to basically control where your pods go. 165 00:07:16,900 --> 00:07:19,899 ‫These things get really custom and really advanced 166 00:07:19,990 --> 00:07:23,260 ‫pretty quickly. Doing them all together can get 167 00:07:23,370 --> 00:07:25,059 ‫a little bit hairy and crazy. 168 00:07:25,330 --> 00:07:28,330 ‫I definitely recommend that you take it slow with them. 169 00:07:28,420 --> 00:07:29,740 ‫Keep it all simple out-of-the-box. 170 00:07:30,430 --> 00:07:33,459 ‫Avoid taints and tolerations until you really 171 00:07:33,460 --> 00:07:34,629 ‫need to look into those. 172 00:07:35,140 --> 00:07:37,329 ‫All right. Let's clean up for this whole section. 173 00:07:37,360 --> 00:07:40,479 ‫If you have anything running in a default namespace 174 00:07:40,480 --> 00:07:43,371 ‫for your cluster, if you just do a kubectl get all, and you 175 00:07:43,510 --> 00:07:46,552 ‫see stuff there beyond the Kubernetes API itself, then 176 00:07:47,380 --> 00:07:50,429 ‫go ahead and delete all those resources because we're 177 00:07:50,440 --> 00:07:51,760 ‫going to be moving on. We won't need those.