Three-Tier-Application-deployment-on-eks

Dhruv VatsDhruv Vats
4 min read

Launch an ec2 instance based upon requirements i created t3.micro

Created security group for ec2

created security-group for the app allow inbound rule for port 22,3000,3500,80,443

Connect with localhost using SSH client

enter into ec2 instance

Install Docker

update the system and install docker using below commands

sudo apt update 
sudo apt install docker.io

Check docker installed successfully

Run the command to check weather docker installed successfully

sudo docker run hello-world

you will get the output like this

Clone the Application with below commands

 git clone https://github.com/VedThavkar/TWSThreeTierAppChallenge
 cd TWSThreeTierAppChallenge

Dockerized the Frontend application

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "npm", "start" ]

Build the docker-image-frontend

docker build -t three-tier-frontend .

Run the docker container to see frontend is working on ec2 pulic IP

docker run -d -p 3000:3000  three-tier-frontend:latest

Access the Application using instance-publicip:port

Install kubectl eksctl on ec2 instance

 curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
 sudo apt install unzip
 unzip awscliv2.zip
 sudo ./aws/install -i /usr/local/aws-cli -b /usr/local/bin --update

Configure aws cli with ec2 insatnces

aws configure

Push frontend Image to ECR

  • Open the aws management console

  • create an ECR repository

  • Push the Frontend image to ecr

Dockerized the Backend

FROM node:14
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .

Build the Docker Image for Backend

Build and Run the Docker Image

 docker build -t three-tier-backend:latest .
 docker run -d -p 8080:8080 three-tier-backend:latest

Push the docker image to ECR

Install Kubectl and eksctl on ec2 instance

 curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
 sudo mv /tmp/eksctl /usr/local/bin
 eksctl version

Create EKS Cluster

 eksctl create cluster --name three-tier-cluster --region eu-north-1 --node-type t2.medium --nodes-min 2 --nodes-max 2

Kubernetes Cluster Created

Create Namespace

 kubectl create namespace three-tier

Deploy Manifests file

 kubectl apply -f k8s_manifests/deploy.yaml
 kubectl apply -f k8s_manifests/secrets.yaml
 kubectl apply -f k8s_manifests/service.yaml
 kubectl apply -f k8s_manifests/backend-deployment.yaml

Check after sometime All your pods running

Install ALB Controller

To do the internal routing between pods we required Ingress controller for that we will be installing helm package manager that will inlude kubernetes manifest file to create ingress controller

we need an account to perform the installation for that we will install the AWS IAM policy by runing commands

curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.5.4/docs/install/iam_policy.json
aws iam create-policy --policy-name AWSLoadBalancerControllerIAMPolicy --policy-document file://iam_policy.json
eksctl utils associate-iam-oidc-provider --region=us-west-2 --cluster=three-tier-cluster --approve
eksctl create iamserviceaccount --cluster=three-tier-cluster --namespace=kube-system --name=aws-load-balancer-controller --role-name AmazonEKSLoadBalancerControllerRole --attach-policy-arn=arn:aws:iam::626072240565:policy/AWSLoadBalancerControllerIAMPolicy --approve --region=us-west-2

if not created successfully create new service account using custom command

eksctl create iamserviceaccount \
  --region eu-north-1 \
  --name alb-controller \
  --namespace kube-system \
  --cluster three-tier-cluster \
  --role-name AmazonEKSLoadBalancerControllerRole \
  --attach-policy-arn arn:aws:iam::591690056778:policy/AWSLoadBalancerControllerIAMPolicy \
  --approve \
  --override-existing-serviceaccounts

Check Service account Created

Service account created successfully

Install Helm on ec2 instance

 sudo snap install helm --classic

Install ALB controller

helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
  -n kube-system \
  --set clusterName=my-cluster \
  --set serviceAccount.create=false \
  --set serviceAccount.name=aws-load-balancer-controller

Installed aws load balancer controller

Install Ingress Controller

kubectl apply -f ingress.yml

Check Alb is Provisioning

Map domain to ALB endpoint

Add CNAME record and point to an ALB endpoint
your both subdomain frontend.root-domain.com and backend.root-domain.com must be redirect to the same ALB

Ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mainlb
  namespace: three-tier
  annotations:
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: ip
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTP": 80}]'
spec:
  ingressClassName: alb
  rules:
    - host: api.backend.com
      http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service:
                name: api
                port:
                  number: 3500
    - host: frontend.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: frontend
                port:
                  number: 3000

Check the Application Access using subdomain

Clean-Up resources

Delete the eks cluster

 eksctl delete cluster --name three-tier-cluster --region ap-south-1

Haappyyy Deployingg !!

0
Subscribe to my newsletter

Read articles from Dhruv Vats directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhruv Vats
Dhruv Vats