This blog will help a user deploy a multi tenant application to Amazon Elastic Container Service using Jenkins for automated deployment.
What is a multi tenant application model ?
A multi tenant application model refers to a software architecture in which a single instance of software runs on a server and serves multiple tenants. A tenant is a group of users who share a common access with specific privileges to the software instance.
Why Amazon Elastic Container Service (ECS) ?
Elastic Container Service uses a cluster of EC2 instances to deploy docker containers hence can be configured to autoscale. Also, we can use spot-instances to reduce the TCO.
Elastic Container Service provides multi tenancy i.e. each user will have his/her own set of container/s that will have scalability and high availability. It does so by providing the ability to create a “Service”. The service can be scaled by applying “service auto scaling” and can provide high availability by applying “service load balancing”.
How does Jenkins help ?
Jenkins allows a user to provide custom inputs that can be used as parameters in the automation scripts while deploying the application.
Jenkins helps you to deploy an application with a single click. All the automation scripts are fetched from git. Once the setup is done, Jenkins will provide the user with a link to his application can be accessed from anywhere.
– A Docker image containing the latest version of the application configured and uploaded to Docker Hub [Refer Fig.(2)]
– A Route53 Hosted Zone for creating application URL’s
Step 1 : Creating an ECS Cluster :
– Create an ECS cluster from the ECS console [Refer Fig. (3)]
– Launch EC2 instances in autoscaling
- Create a launch configuration that helps you launch spot instances and specify a max bid for acquiring the spot instances [Refer Fig. (4)]
- Also, provide custom user data that includes the ECS cluster name to register the instance with & docker hub authentication information [Refer Fig. (5)]
- Further create an autoscaling group that helps launch instances using the above created launch configuration [Refer Fig. (6)]
Step 2 : Running the Jenkins build :
– Once the ECS cluster is up and running, start the Jenkins build that will start deploying the application containers to ECS.
Step 2a : Create a task definition within ECS :
A task definition is required to run Docker containers in Amazon ECS. Some of the parameters you can specify in a task definition include:
- Docker Image : Docker images to use with the containers in your task
- Memory Limits : Memory to use with each container
- Networking Mode : The Docker networking mode to use for the containers in your task
- Port Mapping : Ports (if any) from the container that are mapped to the host container instance
- Entrypoint : The command the container should run when it is started
- nvironment variables (if any) that are to be passed to the container when it starts
Step 2b : Create a Service
Amazon ECS allows you to run and maintain a user specified number (the “desired count”) of instances of a task definition simultaneously in an ECS cluster. This is called as a service.
If any of your tasks should fail or stop for any reason, the Amazon ECS service scheduler launches another instance of your task definition to replace it and maintain the desired count of tasks in the service.
Configure Service Load Balancing :
Your Amazon ECS service can optionally be configured to use Elastic Load Balancing to distribute traffic evenly across the tasks in your service.
An Application Load Balancer can be created and used to configure service load balancing.
Configure Service Auto Scaling :
Your Amazon ECS service can optionally be configured to use Service Auto Scaling to adjust its desired count up or down in response to CloudWatch alarms.
Two alarms per service based on the CPU Utilization need to be created, viz.
– Increase in Load
– Decrease in Load
Associate the two alarms with the corresponding scaling policies, viz.
– Scale Out
– Scale In
Step 2c : Create an Alias record using Route 53 :
Create an Alias record set for the Application Load Balancer’s CNAME using the parameter provided through Jenkins and return it to the user.
Jenkins and automation scripts combined with Amazon ECS provide a simple yet effective way of deploying multi-tenant applications.
Also, it saves time by reducing the hassle of manual configuration for every request & provides high availability and scalability.