how to run one docker per instance to use elb - amazon-web-services

I am trying to use ecs from aws and i have 3 instances in my ecs
cluster
I have these 3 instances as part of auto scaling group.
I want only one docker of  each image type to run on one instance so
i can use aws elb.I am usign below approach for this.
https://aws.amazon.com/blogs/aws/category/ec2-container-service/
Now if my instance per say instance 1  goes down lets say my desired
count is 3 for my service.It will try to start my api-image docker
 in  instance 2 to meet desired count and  now i have 2 docker of my
 api-docker  running  in same instance .Hence i cannot use aws
elb?Is there any  way to solve this problem?

In this case, the ECS scheduler doesn't work due to port conflicting if you have 2 services in the same host. You have to turn on the instance 1 anyway ( if you persist using ELB)

Related

Is there a proper way to stop ECS tasks and the EC2 instances inside a cluster?

I have 2 ECS clusters in one aws account and planning to shut down 1 of the clusters, the services inside that cluster as well as stop/terminate the ec2 instances in the auto-scaling group. Is there a proper way to achieve this without leaving any traces? I have thought about the following:
Changing number of tasks to 0 in each ECS service (cumbersome as I have 7-8 services)
Setting Desired Count in auto scaling group to 0 (Not sure if this will stop or terminate the EC2 instances)
Any help is appreciated
Assuming you are using AWS Cloudformation to setup the ecs cluster, you can delete the cloudformation stack, that will remove all the resources with respect to that CF stack.
Else, see if this helps - deleting via console : https://docs.aws.amazon.com/AmazonECS/latest/developerguide/delete_cluster.html
Whenever you delete ECS cluster it will delete auto-scalling configuration, which also delete instances managed by autoscalling.
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/asg-capacity-providers-delete-capacity-provider.html#:~:text=Option%201%3A%20Use%20the%20delete%20command%20to%20delete%20the%20cluster.

Running ECS service on 2 container instances (ECS instances)

I have an ECS service which has a requirement that it should be running on exactly 2 container instances. How can this be achieved? I could not find any specific place in container definition, where I can fix the number of ECS instances.
There are a few ways to achieve this. One is to deploy your ECS service on Fargate. When you do so and you set your task count to, say, 2 ... ECS will deploy your 2 tasks onto 2 separate and dedicated operating systems/VMs managed by AWS. Two or more tasks can never be colocated to one of these VMs. It's always a 1 task : 1 VM relationship.
If you are using EC2 as your launch type and you want to make sure your service deploys exactly 1 task per instance the easiest way would be to configure your ECS service as type DAEMON. In this case you don't even need (or can't) configure the number of tasks in your service because ECS will always deploy 1 task per EC2 instance that is part of the cluster.
At the time of creating service you will find the field Number of tasks it means that how many container you want exactly. If you write 1 than it will launch only 1 and if you write 2 then it will launch 2 . I Hope you understand

Is it possible to start/stop docker contains on demand in AWS?

I'm trying to deploy a docker image to AWS's Elastic Container Service, and then run this as an EC2 instance (via Fargate). However, I believe I need to specify a minimum of 1 running instance in the TaskDefinition.
What I want to achieve though is basically to be able to spin up this container on demand, as it'll be used infrequently and then shut it down after. So the plan was to start/stop this via a lambda and redirect to the public IP (so within web request timeouts).
I've seen examples of how to do this using EC2, but none actually using Fargate. I don't believe I can define an EC2 task, based off of a docker image (if I can, this might be my solution?).
Does anyone know if it's possible to achieve this? If so could you provide some guidance on how I might approach it, and if you've any CloudFormation examples that would be brilliant.
There is almost not difference in defining ECS task for EC2 or Fargate. Only one difference is networking. With Fargate you have to use awsvpc networking.
You can use lambda. But there is better idea to achieve your use case.
To spin exactly one task, you have to set
Minimum instances: 0
Desired count: 1
Max instances: 1 or more
Autoscaling solution
However better idea than Lambda is to use Service autoscaling. The ECS Servce autoscaling requires metrics in cloudwatch. So you can push metric to cloudwatch to start task. Then compute your task and on the end of your computation put metrics to stop task.
Manual solution
Another solution can be switching desired count to 1 when you want to start task and to 0 when you want to stop task
References: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/service-auto-scaling.html

create snapshot of compute instance group in google cloud

I have the following setup in google cloud.
Http load balancer will balance between 2 instance groups in different regions
Each instance group have minimum of 2 instances and maximum of 5 instances
I want to schedule a snapshot for each running instances at any moment. Since i don't know upfront how many instances will be running at any moment, what is the better way to create snapshot of every running instances. Is that possible?
My main question is about what are you trying to save. If you are running MIGs (Managed Instance Groups) all of your VMs are clones of the Instance Template. Maybe another approach is better here. If you are trying to save logs, send your logs to Cloud Monitoring ; if you are trying to preserve objects, send your files to a Storage Bucket or use Cloud Filestore. So, what are you trying to preserve? Maybe Snapshots are no the best solution for your case

How to launch new tasks on ecs instances which come up in autoscaling group

I have an ECS Cluster which has two instances as defined in an autoscaling group with 2 as minimum capacity,
I have defined the ecs service to run two containers per instance when it is created or updated. So it launches two containers per ecs instances in the ecs cluster.
Now, suppose when I stop/terminate an instance in that cluster a new instance will automatically come up since the autoscaling group has a minimum capacity of two.
The problem is when the new instance come up in the autoscaling group it does not run two tasks which are defined to be in service, instead, it runs 4 tasks on one ecs instance and the other new ecs instance doesn't have any task running on it.
How could I manage that whenever a new instance come up in Auto Scaling group it also has those two tasks running?
if you want those two ec2 instance to be dedicated for those 4 tasks then you can modify task definition memory limits and make it require half of your 1 ecs instance memory.
Let's say you have t3.small then your task definitions limits would be 1gb for memory limit. in this way if you have one t3.small instance you will get only 2 tasks running on it. whenever you add another t3.small instance you should fulfil the missing required memory and another two tasks will run on that new t3.small instance.
You can also consider running 1 task per ecs instance, to do so in service creation choose to have Deamon service type. and give more memory to your task in task definition. so every new ec2 instance will have 1 running task for this service all the time.