What is Task? Task Definition? Service?
In short:
-
Task Definition. Define which image to use, define how much resources (vCPU, memory, etc) should be allocated to the task.
-
Task. It is the most basic building blocks in Fargate, they are instances of Task Definition.
-
Service. It is a system that ensures X amount of tasks are up and running.
When we have containerized an application, we can readily deploy it on cloud using ECS Fargate!
Fargate is designed to work with load balancer. Make sure to have one before proceeding.
Procedures to Create an ECS Fargate Service/Task
Create a Task Definition
-
We fill in the highlighted fields and leave the rest as default.
-
A new definition revision will be created
-
Since we will repeatedly create new docker image as an update, we can reuse our old revision by simply changing the image URI:
Create Target Group and Associte it with a Load Balancer
-
We can only use Target Group of type
IP Address
-
Target group acts like a forward proxy, we just need HTTP (without SSL):
-
Make sure we have created a route for health-check, in my case I use
/test
which simply responses{"success": true}
. -
Click Next.
-
Fill in the destination port
since we have not created a task/service yet, we can leave everything unchanged and click create target group.
-
Associate this target group with our load balancer by creating a new listener:
-
Choose a certificate:
-
and click Add:
Back to ECS's Task Definition: Create a Service
-
We can start our deployment by running a task or creating a service using this task definition.
-
Why there are two options?
-
Create service. With this option we can set how many tasks are up and running, we can also set min and max number of tasks to handle sudden changes of traffic.
-
Run task. However, not every task is readily scalable.
For example, if our web server is also a socket.io chat server, we need to scale it by subscribing and publishing to a redis client (see here) and change the mechanism of "client send message" in backend to adapt this change.
In such cases, we only want 1 task to be kept running.
Back to task definition, check our desired revision, we first proceed by "Create service".
-
-
Choose cluster (which groups our services), choose Launch type and choose FARGATE (default)
-
Input a service name, then configure deployment options (leave it unchanged)
-
We use an existing security group, later we will allow load balancer to access our service by adding a new inbound rule.
-
Choose our load balancer, and then skip to Target Group, choose the target group that we have asscoiated with the load balancer, the Listener fields will be filled up automatically.
-
Click Create.
Let Load Balancer Access our Service
-
In clusters dashboard, click the service name
-
Go to Networking tab
-
Now the networking is governed by the security group list here, click it
-
Edit inbound rules and add the security group of our load balancer into the whitelist.
-
We can find the name of security group of the load balancer here
-
After that our deployment is complete.
Verifying it is Working
Run a Task Instead of Running a Service
-
Recall that our deployment setting is:
-
If we want only one instance (task) to be deployed, we might want to change the Max running tasks % from 200 to 101, which in my case results in buggy behaviour.
-
To make sure there are only one task, we can choose to run task instead of create service.
-
Same setting as before:
-
Next we leave everything unchanged, click create.
-
This time we will bind our task to Target Group through private IP.
-
Click Tasks tab and click the running task:
-
Copy the private IP
-
Choose our old target group that associated with our load balancer, register a new target:
-
Choose an availability zone that does not show warning, then click Include as pending below.
-
Finally click Register pending targets.
-
Let's wait for health check:
-
and we are done:
-
Same result:
-
Cheers!