Cloud-Native Two-Tier Application Deployment with EKS, TOMCAT and RDS in AWS.

Cloud-Native Two-Tier Application Deployment with EKS, TOMCAT and RDS in AWS.

Step 1: Set up your EKS cluster

  1. Create an AWS account if you don't have one.

  2. Install AWS CLI and configure it with your AWS credentials.

    a. Launch an Instance of EC2 as a Bootstrap.

    b. Install aws cli - (https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)

    To install following command:

     sudo curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
     sudo unzip awscliv2.zip
     sudo ./aws/install
    

    Then install docker and git.

    1. To install following command:

       sudo yum install docker git -y
       #then give sudo permissions to docker group following command
       sudo usermod -aG docker $USER
       newgrp docker
      
  3. Install and configure kubectl to interact with the EKS cluster.

    https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/

    1. To install kubectl and setup follow command:

       sudo yum update -y
       sudo curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
       #Grant executable premission to kubectl
       sudo chmod +x kubectl
       #move kubectl to /usr/local/bin
       sudo mv kubectl /usr/local/bin
      
    2. Now setup the EKS cluster using following commands:

      https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html

    #Download and extract the eks cluster.
    sudo curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
    #move extracted files to /usr/local/bin
    sudo mv /tmp/eksctl /usr/local/bin
    #now check the version of eks
    eksctl version.
  1. Now you should give four IAM permission to access the eks cluster to ec2 in aws.

    1. IAM FULL ACCESS.

    2. EKS Custom

      To install following command:

      1.    {
               "Version": "2012-10-17",
               "Statement": [
                   {
                       "Sid": "eksadministrator",
                       "Effect": "Allow",
                       "Action": "eks:*",
                       "Resource": "*"
                   }
               ]
           }
        
    3. AmazonEC2Fullaccess.

    4. AmazonVPCFullaccess.

    5. AwsCloudFormationFullAccess.

  2. Now attach this IAM (EKS-ROLE) role to ec2.

  3. Now create your own cluster using this command.

    To install following command:

    1.    eksctl create cluster --name Ashok-cluster --region ap-south-1 --node-type t2.small
      

      If you execute above command with the help of cloudformation in aws the entire cluster will created like vpc, eks cluster...

      EKS CLUSTER created.

      Now you see below pic node is creating.

      Our cluster is ready.

      cluster is created in the name of "Ashok-cluster"

      Now our nodes are ready.

      Finally, Guys we successfully created our eks cluster in aws. To create cluster its take time nearly 5 to 10 mins.

Step-2: Now inside this we are build three tier architechture.

  1. First clone the git 3 tier project from this link.

    https://github.com/Ashoksana/aws-rds-java.git

  2. Now install tomcat server(v-7), maven, java(v1.8) in your EKS-ec2

    1. To install following command:

       #Installation of java-1.0.8v 
       sudo yum -y install java-1.8.0 java-1.8.0-devel
       java -version
       #install maven version 3.8 from tar file
       sudo wget wget https://archive.apache.org/dist/maven/maven-3/3.8.2/binaries/apache-maven-3.8.2-bin.tar.gz
       sudo tar -xvzf apache-maven-3.8.2-bin.tar.gz
       sudo mv apache-maven-3.8.2 /opt/
       sudo ln -s /opt/apache-maven-3.8.2/bin/mvn /usr/bin/mvn
       mvn -version
      

      If you execute above command with the help of cloudformation in aws

  • Open cd aws-rds-java

  • Then cd src/main/webapp/

  • Edit file sudo vim sudo login.jsp and userRegistration.jsp

Wait .......Wait... before going this you should create RDS Database in aws and this rds should be create cluster vpc

    • Edit 6th line in both login.jsp & userRegestration.jsp and paste RDS endpoint

Now the mvn package where pom.xml file located.

Now Run maven for build war file command is mvn package

Now we did above tomcat setup as in ec2 now these entire steps write in docker file >> build docker image >> push into docker hub.

  1. Follow below Dockerfile for tomcat frontend

    1.    # Use the official CentOS 7 as the base image
         FROM centos:7
      
         #Working dir
         WORKDIR /eks
      
         # Update the package manager and install necessary packages
         RUN yum -y update
         RUN yum -y install epel-release
         RUN yum -y install java-1.8.0-openjdk-devel
         RUN yum -y install wget
      
         # Set the JAVA_HOME environment variable
         ENV JAVA_HOME /usr/lib/jvm/java-1.8.0-openjdk
      
         #Install maven using tar file
         RUN cd /eks
         RUN wget https://archive.apache.org/dist/maven/maven-3/3.8.2/binaries/apache-maven-3.8.2-bin.tar.gz
         RUN tar -xvzf apache-maven-3.8.2-bin.tar.gz
         RUN mv apache-maven-3.8.2 /opt/
         RUN ln -s /opt/apache-maven-3.8.2/bin/mvn /usr/bin/mvn
         # Install git and clone the code of java
         RUN cd /eks
         RUN yum install -y git
         RUN git clone https://github.com/Ashoksana/aws-rds-java.git
      
         #Build war file using maven
         RUN cd /eks/aws-rds-java
         RUN rm -rf /eks/aws-rds-java/src/main/webapp/login.jsp
         RUN rm -rf /eks/aws-rds-java/src/main/webapp/userRegistration.jsp
         COPY login.jsp /eks/aws-rds-java/src/main/webapp/login.jsp
         COPY userRegistration.jsp /eks/aws-rds-java/src/main/webapp/userRegistration.jsp
         RUN cd /eks/aws-rds-java/ && mvn clean package
      
         # Download and install Apache Tomcat 7
         # Download and install Apache Tomcat 7
         RUN cd /eks/ && \
             wget https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.94/bin/apache-tomcat-7.0.94.tar.gz && \
             tar xvf apache-tomcat-7.0.94.tar.gz && \
             rm apache-tomcat-7.0.94.tar.gz
         # Move the war file from aws-rds-java to tomcat
         RUN mv /eks/aws-rds-java/target/LoginWebApp.war /eks/apache-tomcat-7.0.94/webapps/
      
         # Copy the tomcat-users.xml configuration
         COPY tomcat-users.xml /eks/apache-tomcat-7.0.94/conf/tomcat-users.xml
      
         # Expose the default Tomcat port
         EXPOSE 8080
      
         # Start Tomcat
         CMD ["/bin/bash", "-c", "/eks/apache-tomcat-7.0.94/bin/startup.sh run && tail -f /eks/apache-tomcat-7.0.94/logs/catalina.out"]
      

      Build the image from dockerfile

      docker build -t <image_name> .

      Now push the image to your docker hub.

      docker login

      docker push <ur dockerhub username>/imagename:<tag>

    2. same like you should write dockerfile for backend mysql

      Create the below dockerfile for backend.

        1.       # Use the official MySQL Docker image
                FROM mysql:5.7
          
                # Set environment variables to configure MySQL
                ENV MYSQL_USER=admin
                ENV MYSQL_PASSWORD=12345678
                ENV MYSQL_ROOT_PASSWORD=12345678
                ENV MYSQL_DATABASE=mysql
                ENV DB_HOST=mysql.cdxd2n1ta3y4.ap-south-1.rds.amazonaws.com
                # Expose the MySQL port
                EXPOSE 3306
          

Same like tomcat-frontend create a dockerfile >> create image >> push into docker hub register.

Step:3 Create a kubernets deployment for eks cluster.

a. create "tomcat-mysql-deployment" file.

using this sudo vi tomcat-mysqldeployment.yml

  1. Below one is k8s deployment file

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: tomcat-mysql
     spec:
       replicas: 1
       selector:
         matchLabels:
           app: tomcat
       template:
         metadata:
           labels:
             app: tomcat
         spec:
           containers:
             - name: tomcat
               image: ashok8639289986/tomcat-fronend:v1
               ports:
                 - containerPort: 8080
    
     ---
     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: tomcat-mysql-backend
     spec:
       replicas: 1
       selector:
         matchLabels:
           app: mysql
       template:
         metadata:
           labels:
             app: mysql
         spec:
           containers:
             - name: mysql
               image: ashok8639289986/tomcat-mysql:latest
               ports:
                 - containerPort: 3306
    
     ---
     apiVersion: v1
     kind: Service
     metadata:
       name: tomcat-mysql
     spec:
       selector:
         app: tomcat
       ports:
         - protocol: TCP
           port: 80
           targetPort: 8080
       type: LoadBalancer
    

b. Now run this k8s yaml using this command kubectl apply -f tomcat-mysql-deployment.yml

c. Now open Load balancer in ur aws because in service i was give type load balancer. so, eks cluster creater load ba;ancer A record

This is loadbalancer end point below one.

the above dns copy and paste in browser.

d. Now click on Manager app

Now click on loginwebapp this is war file

After this now go to the pod database using this command:
kubectl exec -it mysql-7574cd555c-7vkbr -- mysql -h mysql.cdxd2n1ta3y4.ap-south-1.rds.amazonaws.. -u admin -p

now create jwt database inside this pod. by following commands.

  1. To install following command:

     CREATE DATABASE jwt;
     USE jwt;
     CREATE TABLE `USER` (
         `id` int(10) unsigned NOT NULL auto_increment,
         `first_name` varchar(45) NOT NULL,
         `last_name` varchar(45) NOT NULL,
         `email` varchar(45) NOT NULL,
         `username` varchar(45) NOT NULL,
         `password` varchar(45) NOT NULL,
         `regdate` date NOT NULL,
         PRIMARY KEY (`id`)
     ) ENGINE = InnoDB DEFAULT CHARSET = latin1;
     SHOW TABLES;
    

    Now come back to the tomcat server and click on loginwebapp

    Now you will get login interface and click on register

    Now register with your details.

    like this and submit

    After registered you details you will page like (your successfully registered)

    Now you want to see details of registered again go back to k8s pod of backend. you can see your details like this.

    Hurry finally we created 2-tier application using eks, tomcat, github, docker, mysql, rds of mysql database.

    After completing your practice please don't forget to delete EKS cluster by using the following command

    eksctl delete cluster <your_cluster_name> --region <aws_region_name>

    See EKS cluster is deleting...

    Hope you like my blog...!

    If you like the content follow me on LinkedIn: https://www.linkedin.com/in/ashok-sana

    Follow my Whatsapp & telegram community: https://chat.whatsapp.com/BzX1aruZIH645l29LxvgO3

    https://t.me/ExplorewithAshok

    Happy learning......!

Did you find this article valuable?

Support Ashoksana by becoming a sponsor. Any amount is appreciated!