Showing posts with label AWS. Show all posts
Showing posts with label AWS. Show all posts

Friday, July 12, 2024

How to Set Up Blue Green Deployment on an EC2 Instance for an Angular Application Using AWS CodeDeploy Pipeline with GitHub [Part - I ]

Description: In this blog, we'll walk through setting up a Blue Green deployment.Deployment strategy for an Angular application hosted on Amazon EC2 instances.

Blue-Green Deployment allows for zero-downtime updates, ensuring that users experience seamless transitions during application updates. This setup will leverage AWS services, including EC2, Auto-Scaling, Load Balancing, and Code Deploy.





Application Deployment: Traditional vs Blue-Green pipeline

When delivering an application traditionally, a failed deployment is usually fixed by redeploying an older, stable version of the application.

Due to the expense and time involved in provisioning additional resources, redeployment in traditional data centers often uses the same set of resources

This strategy works, but it has a lot of drawbacks. Rollbacks are difficult to implement since they necessitate starting from scratch with a previous version. 

Since this process takes time, the application can be inaccessible for extended periods of time. Even though the application is simply compromised, a rollback is necessary to replace the flawed version. 

As a result, you are unable to troubleshoot the existing problematic program.

Blue-Green Deployment is an application release methodology in which you create two separate but identical environments. The traffic from the current version (blue environment) of an application or micro-service is transferred to a newer version(green environment), both of which are already in production.

After the green environment has undergone testing, the blue environment is deprecated, and actual application traffic is switched to the green environment.

The blue-green deployment methodology increases application availability and lowers deployment risk by simplifying the rollback process if a deployment fails.


Advantages of Blue-Green Deployment

It is challenging to validate your new application version in a production deployment while also continuing to use the older version of the program when using a traditional deployment with in-place upgrades. 

Your blue and green application environments will be somewhat isolated thanks to blue/green deployments. 

This makes sure that creating a parallel green environment won’t have an impact on the resources supporting your blue environment. The danger of deployment is decreased by this separation.


Steps to set up Blue-Green Deployment with an Angular Application with EC2

Step1: In-Placed Deployment, First we are going to setup In-placed deployment using AWS Code-deploy with Angular


Setup EC2 instance with Nginx and setup custom directory for angular with below configuration

OS: Ubuntu 22
Instance Type: t2.micro
Role: Nginx
Tag:  Name = Angular-Instance


Install and start nginx, start the service
# apt update
# apt install -y  nginx
# systemctl start nginx
# systemctl enable nginx


Create project directory
# cd /var/www
# mkdir my-angular

Change it in default document root
File: /etc/nginx/sites-available/default

Change root directory  from
/var/www/html
TO
root /var/www/my-angular;

Restart nginx
# service nginx restart 

Browse the URL with public IP of machine shows 403

Install AWS code deploy agent in EC2 instance 

[In this example I used Ubuntu image]

Below are the command to setup agent and service for code deploy

# apt update
# apt install -y ruby-full
# apt install wget
# cd ~
# wget https://bucket-name.s3.region-identifier.amazonaws.com/latest/install

For us-west-1 region url is as follow
# wget https://aws-codedeploy-us-west-1.s3.us-west-1.amazonaws.com/latest/install

Reference URL:
https://docs.aws.amazon.com/codedeploy/latest/userguide/resource-kit.html#resource-kit-bucket-names
https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html


# chmod +x ./install
# sudo ./install auto
# systemctl start codedeploy-agent
# systemctl enable codedeploy-agent



Step2 -Setup Github Angular repo with appsepc.yml and buildspec.yml 

Below is the Github project URL with  appsepc.yml and buildspec.yml

Github Project URL : https://github.com/harpal1990/angular-ec2-project


appsepc.yml: The appspec.yaml file is used to specify the deployment actions to be taken by CodeDeploy, It  is a crucial configuration file used in AWS CodeDeploy for defining the deployment actions and specifying how AWS CodeDeploy should manage the deployment process for your application. This file includes details about the files to be transferred, the destination of those files, and the lifecycle event hooks that allow you to run custom scripts at various stages of the deployment.

version: 0.0 os: linux files: - source: dist/my-angular-project destination: /var/www/my-angular permissions: - object: /var/www/my-angular pattern: '**' mode: '0755' owner: root group: root type: - file - directory hooks: ApplicationStart: - location: deploy-scripts/application-start-hook.sh timeout: 300

 

buildspec.yml:  It is is a configuration file used by AWS Code Build to define the build process for your application. This file specifies the commands to run during the build, including installing dependencies, running tests, and packaging the application. It also defines the artifacts to be produced and can include environment variables and other settings.

version: 0.2 phases: install: runtime-versions: nodejs: 12 commands: - npm install -g @angular/cli@9.0.6 pre_build: commands: - npm install build: commands: - ng build --prod finally: - echo This is the finally block execution! artifacts: files: - 'dist/my-angular-project/**/*' - appspec.yml - 'deploy-scripts/**/*'


Step3: Setup Code Build Project for prepare build project and upload it to S3 bucket 

To create the build project navigate to AWS Devleoper Tools --> Build --> Build Projects --> Create Project 




After click on create project, fill all the require details, click on connect to Github to connect


After connect the Github, Click on webhook trigger "Rebuild everytime a code change pushed to the repository"


Set Environment for Build

 

 

Define buildspec configuration: we have already upload buildspec.yml in root path with repository, so here we only define the buildspec.yml file with same name

Artifacts: We are going to save artifacts into s3, So I have created one bucket to store the artifacts



Logs: We can stored build logs in cloudwatch and S3 bucket as well and click on create project



Once project created, test build by click on start build



Once build completed, you will get message as follow, also you will get build in s3 bucket as well



 S3 Bucket


Step4: Create Application and Deployment Group  in Code-deploy to deploy the application in EC2 instance 

To create the application, navigate to CodeDeploy --> Create application




Fill name and Compute platform --> EC2/on-premise

After create application, Create deployment group and fill all the details

Service Role: CodeDeployEC2Role

Deployment type: In place 


Environment Type: Amazon EC2 Instance, Also define Tag which we have setup while setting up the machine

[Note: Here we are applying in placed deployment on standalone machine without auto-scaling]

Define Deployment Setting: Deployment All at once, click on Create Deployment Group



Step5: Setup IAM role for EC2 Instance and attach it to EC2 instance

Create Role with below permission policies

  • AmazonEC2FullAccess
  • AmazonS3FullAccess
  • AmazonEC2RoleforAWSCodeDeploy
  • AWSCodeDeployRole

 


After create the IAM role attach to EC2 instance  



Step6: Create Pipeline for automatic deployment on EC2 Instance
 
To create pipeline navigate to pipeline --> create pipeline fill all the details
 
Create new role for pipeline and click on Next 

 




Source: Filled all the source details like repository and branch name, change the detection option for trigger


Build: Filled all details regarding the build project 


Deploy: Fill all the details for deploy Application Name and Deployment Group


Review: Review the settings and click on create pipeline, Once you click on create it will create to run pipeline 


Once the code pipeline executed, you can see the source code extracted to webroot path


Also we can validate by browse the public IP of EC2 instance

So this is how you can setup standalone EC2 instance with angular using code deploy pipeline. 

In the next topics: we will setup code deploy In-placed and Blue Green deployment with auto-scaling and load balancer


Tuesday, September 12, 2023

Deploying a application in Kubernetes Cluster with Amazon EKS

 Description: Here I have explained, what is Amazon EKS? How to deploy Kubernetes Cluster with Amazon EKS

What is Amazon EKS?
Amazon EKS [Elastic Container Service for Kubernetes] is the managed Kubernetes service which allows you to run Kubernetes on AWS. In the cloud, Amazon EKS automatically manages the availability and scalability of the Kubernetes control plane nodes responsible for scheduling containers, managing application availability, storing cluster data, and other key tasks.

Prerequisites 
Below are the prerequisites to setup Amazon EKS

  • AWS CLI: You will need version 1.16 at least. Follow the URL for setup and version details
  • Kubectl: This command is used for communicating with the cluster API server. Refer URL to setup and version details
  • AWS IAM Authenticator: To allow authentication with kubernetes cluster need to setup IAM user for kubernetes 


AWS EKS ROLE: First we need to setup EKS role in IAM. To create the IAM role perform the below steps

  • Open IAM service in AWS and navigate to Roles 
  • Click on Create role 


  • Filled all the required details in form, Select AWS Service,  in service use case select EKS and select EKS-cluster in specified service as in screenshot


  • You will get the permission list in the review page

  •  Give name the cluster "eksClusterRole" and validate the trust entities as screenshot and create the  role




AWS IAM User: After creating AWS EKS role with eksClusterRole name now we need to create and setup IAM user in the local machine to run the AWS commands 

  • To create the user navigate to IAM --> Users --> Create User 
  • Fill all the required details and create the user with Administrator access privileges and EKS service privileges


     
  • In the example I create user with the name AWSEKSUSER
  • After create the user I am going to add access key to access the AWS APIs using CLI. So navigate to the user properties and click on create Access Keys. Make sure to download the csv file of credentials 
  • Once the user created setup user in local instance AWS CLI using aws configure command line utility 




Kubectl: Kubectl utility is command line utility that used to communicate with Kubernetes API server.  Here is the URL to setup Kubectl utility

You can verify the version of kubectl using kubectl version command 


IAM Role for EKS Node: We also need IAM role for EKS worker node. So to create the role again navigate to IAM and create the role with required permission.

Rolename: AmazonEKSNodeRole




Setup Amazon EKS cluster: After full filled all the prerequisites we are going to setup AWS EKS cluster.

  • Navigate to EKS service in AWS and click on Add Cluster
  • It will pop-up form to create the cluster filled all the required details and select eksClusterRole which we have created previously





  • Need to select VPC and subnets, So in this example we used default VPC in us-east-1 region 



  • Select public interface for cluster



  • Now select the element which you want to enable the logs. But in the example I am not going to enabled anything 



  • Select the add-ons which need to add with the setup. So I am taking the default add-ons 



  • Select the version for each add-ons. In this example I used the default versions



  • Review the details and click on create. It will take around 20 minutes so wait until the process completed. 


  • Once the process completed you will find active status in cluster


Node Group
  • To create the node group navigate to newly created cluster --> Compute tab
  • Under Compute tab option to create "Add node group"


  • Once you click on Add node group you will find form for the node. You need to fill node-group name and IAM role which created for node and click on next 



  • Next step to Set configuration for node group. Fill all required details like AMI, instance type, scaling configuration etc. In this example I used Amazon EC2 AMI, t3.Medium instance type, disk size 20 GB and scaling group 2 for each




  • Next select the sub nets included in node. It will automatically selected the required sub nets. 
  • Review all the details and click on create. It also took few minutes to create it 



  • Once the node group created it shows in active status 



You will get the nodes under the cluster as follow



Configure EKS cluster in AWS CLI: After performing all the above steps now need to configure and manage EKS cluster in AWS CLI. In previous steps we configured AWS cli in the local linux machine. So we used same IAM user to manage the EKS cluster.

First step to configure EKS cluster using AWS Cli, below is the command to configure same

$ aws eks --region us-east-1 update-kubeconfig  --name example-cluster

Note: In above command need to change the region and the name of the cluster. In this example we created cluster with the name example-cluster. Once the command executed the configuration exported to .kube/config file 


Once account configured you can get nodes details from the cluster using command line

$ kubectl get nodes



Setup K8-Application

Once all the configuration done need to checkout the K8 application from the GitHub repository. To checkout the application create one folder and checkout the master branch from the give GitHub repository

https://github.com/harpal1990/k8-application.git

once repository checkout navigate to k8s-specifications directory under repo. You will find different files relevant to kubernetes application 


Now I am going to run each yaml file one by one in a sequence as follow


$ kubectl create -f voting-app-deploy.yaml
$ kubectl create -f voting-app-service.yaml
$ kubectl create -f redis-deploy.yaml
$ kubectl create -f redis-service.yaml
$ kubectl create -f postgres-deploy.yaml
$ kubectl create -f postgres-service.yaml
$ kubectl create -f worker-app-deploy.yaml
$ kubectl create -f result-app-deploy.yaml
$ kubectl create -f result-app-service.yaml





You will get the result of deployment and service using below command

$ kubectl get deployments,svc


In the above screenshot we can see all the services pod up and running with 2 load balancer type service 

First I am opening the voting-service public url for voting application and select the cats for vote

http://a25ca28a3348a407e8bdd3f912145b48-1964993370.us-east-1.elb.amazonaws.com/




Now after vote open the result application for result using below url

http://a8a7f7a6a4e4044dd89c1f08ecb5707c-548992896.us-east-1.elb.amazonaws.com/






So in this way we can deploy kubernetes application to Amazon EKS