1 00:00:00,320 --> 00:00:01,670 ‫So a new concept you need to know 2 00:00:01,670 --> 00:00:05,310 ‫for the exam is the concept of ECS tasks placements. 3 00:00:05,310 --> 00:00:08,100 ‫So when you create a task of type EC2, 4 00:00:08,100 --> 00:00:09,370 ‫ECS must figure out 5 00:00:09,370 --> 00:00:14,090 ‫where to place the task based on the available memory, CPU 6 00:00:14,090 --> 00:00:16,990 ‫and ports on your target EC2 instances. 7 00:00:16,990 --> 00:00:19,480 ‫So for example, here is our ECS cluster 8 00:00:19,480 --> 00:00:21,580 ‫made of three EC2 instances 9 00:00:21,580 --> 00:00:24,460 ‫and some tasks are placed on each instance 10 00:00:24,460 --> 00:00:25,348 ‫in some ways. 11 00:00:25,348 --> 00:00:28,700 ‫If the ECS service has a new container 12 00:00:28,700 --> 00:00:31,497 ‫a new task that it wants to place on your EC2 instances, 13 00:00:31,497 --> 00:00:34,470 ‫it needs to figure out where to place it. 14 00:00:34,470 --> 00:00:36,320 ‫And that's the big question mark. 15 00:00:36,320 --> 00:00:39,880 ‫So similarly, whenever a service scales in 16 00:00:39,880 --> 00:00:43,260 ‫I mean that we remove an ECS task, 17 00:00:43,260 --> 00:00:45,860 ‫the ECS service needs to determine 18 00:00:45,860 --> 00:00:48,670 ‫which ECS task to terminate. 19 00:00:48,670 --> 00:00:49,503 ‫Okay. 20 00:00:49,503 --> 00:00:51,010 ‫So to assist you with this 21 00:00:51,010 --> 00:00:54,340 ‫you can define what's called a task placement strategy 22 00:00:54,340 --> 00:00:56,440 ‫and task placement constraints. 23 00:00:56,440 --> 00:00:58,843 ‫And this will guide where this new container 24 00:00:58,843 --> 00:01:02,860 ‫will be added or where a container will be removed from. 25 00:01:02,860 --> 00:01:06,350 ‫This is only working for when you use ECS 26 00:01:06,350 --> 00:01:08,370 ‫launched on EC2 instances, 27 00:01:08,370 --> 00:01:09,203 ‫not for Fargate, 28 00:01:09,203 --> 00:01:11,290 ‫because for Fargate AWS figures out 29 00:01:11,290 --> 00:01:13,250 ‫for you where to start the container 30 00:01:13,250 --> 00:01:15,660 ‫and you don't manage any backend instances. 31 00:01:15,660 --> 00:01:16,493 ‫Okay. 32 00:01:16,493 --> 00:01:19,230 ‫So this is only valid for ECS on EC2. 33 00:01:19,230 --> 00:01:20,063 ‫Okay. 34 00:01:20,063 --> 00:01:22,010 ‫Now let's talk about the task placement process. 35 00:01:22,010 --> 00:01:22,843 ‫Okay. 36 00:01:22,843 --> 00:01:23,676 ‫So the first thing to note is that 37 00:01:23,676 --> 00:01:26,360 ‫task placement strategies are a best effort. 38 00:01:26,360 --> 00:01:28,010 ‫If you wanted to have constraints 39 00:01:28,010 --> 00:01:29,335 ‫we'll see this very very soon. 40 00:01:29,335 --> 00:01:30,330 ‫Okay. 41 00:01:30,330 --> 00:01:32,030 ‫So when ECS places tasks 42 00:01:32,030 --> 00:01:33,440 ‫it will use the following process 43 00:01:33,440 --> 00:01:35,440 ‫to select where to place it. 44 00:01:35,440 --> 00:01:37,501 ‫The first one is that it will identify instances 45 00:01:37,501 --> 00:01:40,060 ‫that satisfy the CPU, memory 46 00:01:40,060 --> 00:01:42,230 ‫and port requirements in the task definition. 47 00:01:42,230 --> 00:01:44,940 ‫So figure out where it can place the task 48 00:01:44,940 --> 00:01:46,280 ‫in the first place. 49 00:01:46,280 --> 00:01:47,950 ‫Then it will look at the 50 00:01:47,950 --> 00:01:50,640 ‫task placement constraints that we'll see in this lecture. 51 00:01:50,640 --> 00:01:52,050 ‫And then finally, 52 00:01:52,050 --> 00:01:54,140 ‫it will try to identify the instance 53 00:01:54,140 --> 00:01:56,820 ‫that satisfies best the task placement strategy. 54 00:01:56,820 --> 00:01:59,760 ‫And then it will select that instance for the task placement 55 00:01:59,760 --> 00:02:01,009 ‫and place the task there. 56 00:02:01,009 --> 00:02:02,810 ‫So now let's talk about what 57 00:02:02,810 --> 00:02:05,070 ‫these task placement strategies are. 58 00:02:05,070 --> 00:02:06,990 ‫The first one that you need to know for the exam that comes 59 00:02:06,990 --> 00:02:08,372 ‫up is called binpack. 60 00:02:08,372 --> 00:02:10,950 ‫And binpack will place tasks 61 00:02:10,950 --> 00:02:15,110 ‫based on the least available amount of CPU or memory. 62 00:02:15,110 --> 00:02:17,710 ‫And this is to help you minimize 63 00:02:17,710 --> 00:02:19,810 ‫the number of instances in use. 64 00:02:19,810 --> 00:02:21,630 ‫So this will bring you cost saving. 65 00:02:21,630 --> 00:02:24,840 ‫So if you want to define a binpack placement strategy 66 00:02:24,840 --> 00:02:27,060 ‫this is the JSON that's according to it. 67 00:02:27,060 --> 00:02:27,893 ‫So it's here. 68 00:02:27,893 --> 00:02:30,980 ‫It says binpack on the field memory so RAM. 69 00:02:30,980 --> 00:02:33,410 ‫And so that means that if we have one EC2 instance, 70 00:02:33,410 --> 00:02:35,100 ‫it's going to try to fill up 71 00:02:35,100 --> 00:02:38,040 ‫that EC2 instance all the way with containers. 72 00:02:38,040 --> 00:02:41,520 ‫And then when it can't put any more containers 73 00:02:41,520 --> 00:02:43,140 ‫on that one EC2 instance, 74 00:02:43,140 --> 00:02:45,430 ‫it's going to place EC2 containers 75 00:02:45,430 --> 00:02:47,270 ‫on another EC2 instance. 76 00:02:47,270 --> 00:02:48,470 ‫So as you can see here 77 00:02:48,470 --> 00:02:51,240 ‫we are placing as many containers as possible 78 00:02:51,240 --> 00:02:53,229 ‫on one EC2 instance before moving on 79 00:02:53,229 --> 00:02:54,207 ‫to the other. 80 00:02:54,207 --> 00:02:55,680 ‫That's why it's called binpack 81 00:02:55,680 --> 00:02:58,290 ‫because it packs all the containers together. 82 00:02:58,290 --> 00:03:00,650 ‫And that's the strategy that brings the most cost saving 83 00:03:00,650 --> 00:03:03,980 ‫because it will minimize the number of EC2 instances in use 84 00:03:03,980 --> 00:03:06,160 ‫and try to maximize the utilization 85 00:03:06,160 --> 00:03:08,370 ‫of one EC2 instance at a time. 86 00:03:08,370 --> 00:03:09,450 ‫Okay? 87 00:03:09,450 --> 00:03:12,400 ‫Next the task placement strategy for random. 88 00:03:12,400 --> 00:03:15,550 ‫So here very simple, it places the tasks randomly. 89 00:03:15,550 --> 00:03:17,670 ‫So if we look at this placement strategy 90 00:03:17,670 --> 00:03:18,794 ‫in JSON it's very simple. 91 00:03:18,794 --> 00:03:21,146 ‫And so say we have two EC2 instance 92 00:03:21,146 --> 00:03:22,763 ‫and tasks are being added 93 00:03:22,763 --> 00:03:25,245 ‫then they would just be placed randomly. 94 00:03:25,245 --> 00:03:27,670 ‫And there is no logic to it, just random, 95 00:03:27,670 --> 00:03:28,850 ‫very very simple. 96 00:03:28,850 --> 00:03:30,090 ‫Not an optimal strategy, 97 00:03:30,090 --> 00:03:32,290 ‫but one that works really great. 98 00:03:32,290 --> 00:03:33,123 ‫Okay. 99 00:03:33,123 --> 00:03:35,350 ‫The last placement version to be aware of 100 00:03:35,350 --> 00:03:36,940 ‫is called spread. 101 00:03:36,940 --> 00:03:38,943 ‫So say we have three EC2 instances 102 00:03:38,943 --> 00:03:42,135 ‫and they are in three different availability zones. 103 00:03:42,135 --> 00:03:44,850 ‫Then, if we do a spread 104 00:03:44,850 --> 00:03:46,420 ‫the task will be spread 105 00:03:46,420 --> 00:03:49,160 ‫based on the specified value. 106 00:03:49,160 --> 00:03:51,730 ‫So for example, that value could be instance ID 107 00:03:51,730 --> 00:03:54,400 ‫or ECS availability zones and so on. 108 00:03:54,400 --> 00:03:55,310 ‫So in this example 109 00:03:55,310 --> 00:03:56,170 ‫I'll make it very simple 110 00:03:56,170 --> 00:03:57,320 ‫and understandable. 111 00:03:57,320 --> 00:03:59,240 ‫We choose a placement strategy of spread 112 00:03:59,240 --> 00:04:02,400 ‫on the ECS availability zones. 113 00:04:02,400 --> 00:04:04,900 ‫That means that the task will be spread evenly 114 00:04:04,900 --> 00:04:06,250 ‫across AZs. 115 00:04:06,250 --> 00:04:08,020 ‫So my first task may be on AZ-A 116 00:04:08,020 --> 00:04:09,023 ‫then AZ-B, 117 00:04:09,023 --> 00:04:09,860 ‫then AZ-C. 118 00:04:09,860 --> 00:04:12,566 ‫And so they're spread as you can see on each AZ 119 00:04:12,566 --> 00:04:14,663 ‫and then it will start all over again. 120 00:04:14,663 --> 00:04:15,760 ‫So with this, 121 00:04:15,760 --> 00:04:19,080 ‫we have basically maximized the high availability 122 00:04:19,080 --> 00:04:21,710 ‫of our ECS service by spreading the task 123 00:04:21,710 --> 00:04:23,710 ‫on the EC2 instances. 124 00:04:23,710 --> 00:04:25,910 ‫So, these task placement strategies 125 00:04:25,910 --> 00:04:27,086 ‫they can be mixed together. 126 00:04:27,086 --> 00:04:30,240 ‫So we can have a spread on availability zone 127 00:04:30,240 --> 00:04:32,035 ‫and then a spread on instance ID 128 00:04:32,035 --> 00:04:34,390 ‫or we can have a spread on availability zone 129 00:04:34,390 --> 00:04:36,390 ‫and then a binpack on memory. 130 00:04:36,390 --> 00:04:38,280 ‫So it is possible for you to mix and match. 131 00:04:38,280 --> 00:04:41,140 ‫But the exam should test you only on the basics. 132 00:04:41,140 --> 00:04:42,180 ‫So understanding the difference 133 00:04:42,180 --> 00:04:45,150 ‫between binpack spread and random. 134 00:04:45,150 --> 00:04:48,140 ‫And then we have ECS task placement constraints 135 00:04:48,140 --> 00:04:50,520 ‫to bring constraints to how tasks are being placed. 136 00:04:50,520 --> 00:04:53,180 ‫So, the first one is called distinctInstance 137 00:04:53,180 --> 00:04:54,940 ‫and we are seeing through the ECS service 138 00:04:54,940 --> 00:04:57,140 ‫that each task should be placed 139 00:04:57,140 --> 00:04:59,110 ‫on a different container instance. 140 00:04:59,110 --> 00:05:02,390 ‫So you will never have two tasks on the same instance. 141 00:05:02,390 --> 00:05:04,020 ‫And this is defined by this JSON 142 00:05:04,020 --> 00:05:06,722 ‫called placement constraints distinctInstance. 143 00:05:06,722 --> 00:05:09,860 ‫And then there is a second task constraint 144 00:05:09,860 --> 00:05:11,460 ‫called memberOf 145 00:05:11,460 --> 00:05:13,090 ‫and we want to place task 146 00:05:13,090 --> 00:05:14,754 ‫on instances that satisfy an expression 147 00:05:14,754 --> 00:05:17,760 ‫that can be defined in the cluster query language, 148 00:05:17,760 --> 00:05:18,820 ‫which is pretty advanced. 149 00:05:18,820 --> 00:05:21,590 ‫And so I'll just give you one concrete example of it 150 00:05:21,590 --> 00:05:23,470 ‫and that should be enough going into the exam. 151 00:05:23,470 --> 00:05:25,514 ‫So for example, we do a placement constraint here 152 00:05:25,514 --> 00:05:30,514 ‫and we wanna say that the instance type must be of type t2. 153 00:05:30,830 --> 00:05:32,834 ‫And so what we are saying here is that 154 00:05:32,834 --> 00:05:37,262 ‫all these tasks should be placed only on t2 instances. 155 00:05:37,262 --> 00:05:38,830 ‫So this is the kind of constraints 156 00:05:38,830 --> 00:05:40,180 ‫you can use with a memberOf. 157 00:05:40,180 --> 00:05:41,710 ‫So we have distinctInstance 158 00:05:41,710 --> 00:05:42,900 ‫which is very easy to understand 159 00:05:42,900 --> 00:05:44,999 ‫and memberOf with a more complicated 160 00:05:44,999 --> 00:05:48,370 ‫cluster query language to force your task to be 161 00:05:48,370 --> 00:05:50,326 ‫for example only on EC2 instances. 162 00:05:50,326 --> 00:05:51,290 ‫Okay. 163 00:05:51,290 --> 00:05:52,410 ‫That's it for this lecture. 164 00:05:52,410 --> 00:05:53,243 ‫I hope you liked it. 165 00:05:53,243 --> 00:05:55,180 ‫And I will see you in the next lecture.