Tuesday 4 May 2021

Continuous Delivery of Microservices on AWS using AWS Elastic Beanstalk

 

Continuous Delivery of Microservices on AWS using AWS Elastic Beanstalk

AWS Elastic Beanstalk service can be used to deploy and scale applications and services (from docker containers) developed with Java, .NET, PHP, Nodejs, Python, Ruby, Go etc on servers such as Apache, Tomcat, Nginx etc. Docker containers provide the flexibility of selecting one's runtime environment of choice including platform, programming language, app/services dependencies, and also, configuring the environment appropriately. All one is required to do is simply push the docker image to the image repository, and deploy the container. Elastic Beanstalk service, then, takes care of different aspects of deployment such as capacity provisioning, load balancing, auto-scaling, application/service health monitoring etc.

Docker platform for Elastic Beanstalk has two generic configurations such as following:

  • Single container Docker
  • Multicontainer Docker

We shall try and cover the use cases for both the configuration types.

Single Container Docker

Before getting into details of single container docker configurations for Elastic Beanstalk, lets quickly look into the solution architecture for deploying microservices on AWS using AWS Elastic Beanstalk.

Solution Architecture

Following represents the solution architecture of deploying microservices on AWS using AWS Elastic Beanstalk using single container docker configurations.

Solution Architecture - Microservices to AWS Elastic Beanstalk

In the above diagram, pay attention to some of the following:

  1. Code is checked into code repository such as Gitlab
  2. Webhook configured in GitLab triggers the Jenkins job
  3. Jenkins job starts executing which results in following steps:
    • Retrieve the microservice artifacts from Gitlab
    • Build the microservice
    • Run the tests such as unit and integration tests
    • Build the image if all of the above steps are successful
    • Push the image to image repository such as Dockerhub or AWS ECR
    • Deploy using AWS Elastic Beanstalk CLI command

Single Docker Container Configuration

Following steps need to be taken to get setup with Elastic Beanstalk to deploy application/services/microservices from docker containers based on single docker container configuration:

  1. Create a Beanstalk application using AWS console for creating new application. Pay attention to some of the following while creating the application.
    • Select environment as Web Server environment.
    • On environment type page, select configuration as "Docker" (and not Multi-container Docker) and environment type as "Load balancing, auto scaling"
    • Continue with choosing default in each step. In "Application version", you may choose to upload the Dockerfile or Dockerrun.aws.json. Later, the same Dockerfile or Dockerrun.aws.json can be uploaded using "eb deploy" command as part of Jenkins build post-steps.
  2. Install Elastic Beanstalk command line interface (EB CLI). EB CLI provides a set of commands for creating, updating and monitoring environments from a local repository.
  3. Go to the project folder consisting of Dockerrun.aws.json or Dockerfile (for single docker container configuration). Use "eb init" command to choose some of the following:
    • Region
    • Access key and secret key
    • Select an application (created earlier using EB console).
    • Select a keypair (one which was selected while creating the application using EB console)

With above steps followed, one should be all set to execute the following command from the project folder.

eb deploy

Make sure that project folder consists of either Dockerfile or Dockerrun.aws.json. In the code below, image is retrieved from Dockerhub. Note the AWSEBDockerrunVersion as "1". For multi-container configuration, the value becomes "2".

{
  "AWSEBDockerrunVersion": 1,
  "Image": {
    "Name": "xyz/springboot-web-app",
    "Update": "true"
  },
  "Ports": [
    {
      "ContainerPort": "8080"
    }
  ],
  "Logging": "/var/log"
}

Configure Jenkins Post-steps

Jenkins post-steps can be configured to achieve following:

Pushing images to Dockerhub; Deploy to Elastic Beanstalk

# Build the docker image
sudo docker build -t ImageName:tag /var/jenkins_home/workspace/SpringBootApp

# Login into Dockerhub; dockerhubLogin and dockerhubPassword is login and password respectively for dockerhub.com
sudo docker login -u="dockerhubLogin" -p="dockerhubPassword"

# Push docker image into Dockerhub
sudo docker push ImageName:tag

# EB Deploy; Go to the project folder and execute the command, "eb deploy"
cd /var/jenkins_home/workspace/awsebdemo
eb deploy

In above code samples, note some of the following:

  • ImageName:tag should be replaced with image such as xyz/springboot-web-app:latest.

Reference

Continuous Delivery of Microservices with AWS ECS

 

Continuous Delivery of Microservices with AWS ECS

AWS EC2 Container Service (ECS) is a highly scalable container management service which is used to start, stop and run microservices within Docker containers on a cluster of AWS EC2 instances. In this project, it is demonstrated as to how to deploy container-based microservices using CLI commands from within Jenkins. In order to achieve the same, following needs to be done:

  1. Setup ECS Service
    • Create a repository (EC2 Repository - ECR)
    • Create a task definition
    • Create an ECS cluster
    • Create a service
  2. Configure Jenkins build Post-steps

Note: For the demonstration purpose, both Gitlab and Jenkins are setup within Docker Containers. In real world scenario, Gitlab and Jenkins may get setup within different VMs.

Before getting into the setup details, let us try and understand the solution architecture related with achieving continuous delivery of microservices with AWS ECS.

Solution Architecture

Following represents the solution architecture of deploying micro-services using AWS ECS.

Solution Architecture - Microservices to AWS ECS

In above diagram, pay attention to some of the following:

  1. Code is checked into code repository such as Gitlab
  2. Web-hook configured in GitLab triggers the Jenkins job
  3. Jenkins job starts executing which results in following steps:
    • Retrieve the micro-service artifacts from Gitlab
    • Build the micro-service
    • Run the tests such as unit and integration tests
    • Build the image if all of the above steps are successful
    • Push the image to image repository such as Dockerhub or AWS ECR
    • Register task definition with AWS ECS
    • Update AWS ECS

Setup ECS Service

Before configuring steps into Jenkins, following needs to be setup using AWS ECS console.

Create an Image Repository with AWS ECR

First step is getting setup with AWS ECR. Following command needs to be executed in order to create an ECR repository.

# Login into ECR
aws configure
aws ecr get-login
docker built -t ImageName .
docker tag ImageName:tag AWS_ECR_URL/ImageName:tag
docker push AWS_ECR_URL/ImageName:tag

Note some of the following with above command:

  • AWS_ECR_URL is of the format https://aws_account_id.dkr.ecr.region.amazonaws.com. One can get the value of Account id by logging into console and going to My Account page. Region information can be found from the region and availability zones page
  • aws configure command is used to setup AWS CLI installation. The command would require one to enter credentials and config information before one starts working with their AWS services. The command would require one to enter details for access key ID, secret access key, default region name and default output format. However, as we need to achieve this without entering details at the prompt, following needs to be done in order to achieve the promptless command such as aws configure --profile default
    • Create a folder, .aws in the home.
    • Create a file named as config within above folder, .aws, with following content. One could get access key id and secret access key information by logging into the AWS console and accessing "My Security Credentials".
[default]
aws_access_key_id=AKXXIXXYXX4X4XXXXJRY
aws_secret_access_key=DyxxxxxxeqQyxyyyyytXcwwthbbCxaaaa8Qi0y
region=us-west-2
output=json
  • aws ecr get-login command is used to get the login prompt which needs to be executed further to login (start authenticated session) into AWS ECR and thereafter, push the image.
  • Other commands are usual commands to push the docker image to the AWS ECR image repository.

Executing above commands leads to user entering the details at the prompt. If one wants to achieve the same without prompt, from within Jenkins, the same could be achieved using following command which is a combination of commands to achieve promptless execution.

yes "" | aws configure --profile default ; aws ecr get-login > awslogin.sh ; sudo sh awslogin.sh

One can observe that executing command such as "aws ecr get-login" leads to output of command such as following which needs to be executed for successfully logging in. The command below is sent to awslogin.sh file as shown in the command and then awslogin.sh is executed.

docker login -u AWS -p SomeRandomPasswordStringSentByAWS -e none https://**aws_account_id**.dkr.ecr.**region**.amazonaws.com

Create a Task Definition

Next step is to create a task definition. Command such as following could be used to create the task definition:

aws ecs register-task-definition --family TaskDefinitionFamily --container-definitions "[{\"name\":\"ContainerName\",\"image\":\"ImageName:tag\",\"memory\":300,\"portMappings\":[{\"hostPort\":0,\"containerPort\":8080,\"protocol\":\"tcp\"}]}]" 

In above command, note the following two aspects:

  • TaskDefinitionFamily is the name of family for a task definition, which allows you to track multiple versions of the same task definition. The family is used as a name for your task definition.
  • ContainerName which is the name of the container.
  • container-definitions which is used to provide information related with one or more containers which will be started as a result of executing task based on the task definition.

One may want to login and access the AWS console at Services/EC2 Container Service/Task Definitions and try and create task definition to understand different aspects of task definition creation. Further details in relation with register-task-definition can be found on this page, register-task-definition.

Create an ECS Cluster

As this is one time activity, one may want to use AWS console at Services/EC2 Container Service/Clusters to create the cluster. It is pretty straight forward and very easy to create the cluster.

Create/Update the Service

Once done with creating cluster, one will be required to update ECS service. update-service command is used to modify the task definition and deploy a new version of the service.

aws ecs update-service --cluster ClusterName --service ServiceName --task-definition TaskDefinitionName --desired-count 2

In above code snippet, note some of the following:

  • TaskDefinitionName is name of the task definition. The family and revision (family:revision ) or full Amazon Resource Name (ARN) of the task definition to run in your service. If a revision is not specified, the latest ACTIVE revision is used. If you modify the task definition with update-service , Amazon ECS spawns a task with the new version of the task definition and then stops an old task after the new version is running.
  • ClusterName is name of the ECS cluster
  • ServiceName is name of the service
  • desired-count is used to configure number of instantiations of the task to place and keep running in your service.

Further details in relation with update-service command can be found on this page, update-service.

Configure Jenkins Post-steps

Jenkins post-steps can be configured to achieve following:

Pushing images to Dockerhub; Register task definition; Update ECS

# Build the docker image
sudo docker build -t ImageName:tag /var/jenkins_home/workspace/SpringBootApp

# Login into Dockerhub; dockerhubLogin and dockerhubPassword is login and password respectively for dockerhub.com
sudo docker login -u="dockerhubLogin" -p="dockerhubPassword"

# Push docker image into Dockerhub
sudo docker push ImageName:tag

# Login using AWS CLI
yes "" | aws configure --profile default ; aws ecr get-login > awslogin.sh ; sudo sh awslogin.sh

# Register task definition`
aws ecs register-task-definition --family TaskDefinitionFamily --container-definitions "[{\"name\":\"ContainerName\",\"image\":\"ImageName:tag\",\"memory\":300,\"portMappings\":[{\"hostPort\":0,\"containerPort\":8080,\"protocol\":\"tcp\"}]}]" 

# Update service
aws ecs update-service --cluster ClusterName --service ServiceName --task-definition TaskDefinitionName --desired-count 2

In above code samples, note some of the following:

  • ImageName:tag is the image name. For example, ajitesh/springboot-web-app:latest.
  • TaskDefinitionFamily is the name of family for a task definition, which allows you to track multiple versions of the same task definition. The family is used as a name for your task definition.
  • ContainerName which is the name of the container.
  • ClusterName is the name of the ECS cluster
  • ServiceName is the name of the service

Pushing images to ECR; Register the task definition ; Update the ECS service

# Login into AWS
yes "" | aws configure --profile default ; aws ecr get-login > awslogin.sh ; sudo sh awslogin.sh

# Build the docker image
sudo docker build -t ImageName /var/jenkins_home/workspace/SpringBootApp

# Tag the image; Push docker image into AWS ECR
sudo docker tag ImageName:tag 153819127898.dkr.ecr.us-west-2.amazonaws.com/ImageName:tag
sudo docker push AWS_ECR_URL/ImageName:tag

# Register task definition`
aws ecs register-task-definition --family TaskDefinitionFamily --container-definitions "[{\"name\":\"ContainerName\",\"image\":\"ImageName:tag\",\"memory\":300,\"portMappings\":[{\"hostPort\":0,\"containerPort\":8080,\"protocol\":\"tcp\"}]}]" 

# Update service
aws ecs update-service --cluster ClusterName --service ServiceName --task-definition TaskDefinitionName --desired-count 2

Reference