DEV Community

Arjun Menon for AWS Community Builders

Posted on • Originally published at arjunmenon.hashnode.dev on

Deploying a Three-Tier Application with CI/CD using Jenkins, ReactJS, NodeJS, and MongoDB on Kubernetes

Introduction

In this tutorial, we will guide you through the process of deploying a three-tier application on Kubernetes using ReactJS for the frontend, NodeJS for the backend, and MongoDB as the database. We'll use the provided GitHub repository and Docker containers for each tier. Additionally, we'll create Kubernetes manifest files to orchestrate the deployment.

Prerequisites

Before starting, ensure you have the following prerequisites:

  1. AWS account with IAM administrator access.

  2. An EC2 instance with Ubuntu in your preferred region.

  3. All the required code is available in this GitHub repository.

Step 1: IAM Configuration

Create a user eks-admin with AdministratorAccess and generate security credentials (Access Key and Secret Access Key).

# AWS CLI commandaws configure
Enter fullscreen mode Exit fullscreen mode

Step 2: EC2 Setup

Launch an Ubuntu instance and SSH into it.

Step 3: Install AWS CLI v2

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"sudo apt install unzipunzip awscliv2.zipsudo ./aws/install -i /usr/local/aws-cli -b /usr/local/bin --update
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Docker

sudo apt-get updatesudo apt install docker.iodocker pssudo chown $USER /var/run/docker.sock
Enter fullscreen mode Exit fullscreen mode

Step 5: Install kubectl

curl -o kubectl https://amazon-eks.s3.us-west-2.amazonaws.com/1.19.6/2021-01-05/bin/linux/amd64/kubectlchmod +x ./kubectlsudo mv ./kubectl /usr/local/binkubectl version --short --client
Enter fullscreen mode Exit fullscreen mode

Step 6: Install eksctl

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

Step 7: Setup EKS Cluster

eksctl create cluster --name three-tier-cluster --region us-west-2 --node-type t2.medium --nodes-min 2 --nodes-max 2aws eks update-kubeconfig --region us-west-2 --name three-tier-clusterkubectl get nodes
Enter fullscreen mode Exit fullscreen mode

Step 8: Clone GitHub Repository

git clone https://github.com/ArjunMnn/TWSThreeTierAppChallengecd TWSThreeTierAppChallenge
Enter fullscreen mode Exit fullscreen mode

Step 9: Build Docker Images

Build Docker images for all three tiers (frontend, backend, and MongoDB). Refer to the provided Dockerfiles in the repository.

Step 10: Create Kubernetes Manifests

Use the provided manifest files to deploy the application on Kubernetes.

kubectl create namespace workshopkubectl apply -f backend-deployment.yamlkubectl apply -f backend-service.yamlkubectl apply -f frontend-deployment.yamlkubectl apply -f frontend-service.yamlkubectl apply -f deploy.yamlkubectl apply -f secrets.yamlkubectl apply -f service.yamlkubectl apply -f full_stack_lb.yaml
Enter fullscreen mode Exit fullscreen mode

Step 11: Install AWS Load Balancer

curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.5.4/docs/install/iam_policy.jsonaws iam create-policy --policy-name AWSLoadBalancerControllerIAMPolicy --policy-document file://iam_policy.jsoneksctl utils associate-iam-oidc-provider --region=us-west-2 --cluster=three-tier-cluster --approveeksctl 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-2sudo snap install helm --classichelm repo add eks https://aws.github.io/eks-chartshelm repo update ekshelm 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-controllerkubectl get deployment -n kube-system aws-load-balancer-controllerkubectl apply -f full_stack_lb.yaml
Enter fullscreen mode Exit fullscreen mode

Step 12: Test the Application

Access the Application

Open your web browser and navigate to the DNS of the AWS Load Balancer. This will take you to the frontend of your three-tier application.

Create Tasks

  1. On the frontend, you should see the application interface.

  2. Click around and interact with the application, creating tasks to test the functionality.

Verify MongoDB Entries

To ensure that entries are being added to the MongoDB database, follow these steps:

Access MongoDB Shell

# Example SSH commandkubectl exec -it -n workshop <mongodb-pod-name> -- mongo
Enter fullscreen mode Exit fullscreen mode

Check Database and Collection

use tododb.tasks.find()
Enter fullscreen mode Exit fullscreen mode

This will show you the entries in the tasks collection of the todo database. You should see the tasks you created through the application.

Step 13: Configure Jenkins for CI/CD

Configure AWS Credentials

  1. In Jenkins, go to "Manage Jenkins" > "Manage Credentials."

  2. Under the "Stores scoped to Jenkins" section, click on "(global)".

  3. Add new credentials:

Configure Kubeconfig Credential

  1. In Jenkins, go to "Manage Jenkins" > "Manage Credentials."

  2. Under the "Stores scoped to Jenkins" section, click on "(global)".

  3. Add new credentials:

Note: Kubeconfig file is in the path /home/ubuntu/.kube/config. Download it to your local using scp.

Create a Jenkins Pipeline Job

  1. In Jenkins, click on "New Item."

  2. Choose "Pipeline" and give your job a name.

  3. Under the "Pipeline" section, select "Pipeline script from SCM."

  4. Set the SCM to Git and provide your repository URL.

  5. In the "Script Path," specify the path to your Jenkinsfile (e.g., Jenkinsfile).

  6. Save the job.

Trigger the Pipeline

  1. Make a change in your GitHub repository, commit, and push.

  2. Jenkins should automatically detect the change and trigger the pipeline.

  3. Monitor the Jenkins dashboard for the progress of each stage in the pipeline.

Step 14: Verify CI/CD Deployment

Check AWS ECR

  1. Open the AWS ECR console.

  2. Verify that the images for the frontend and backend have been pushed successfully.

Check Kubernetes Deployment

  1. Open your Kubernetes dashboard or use the following command:
kubectl --kubeconfig=/path/to/kubeconfig get deployments -n workshopkubectl --kubeconfig=/path/to/kubeconfig get services -n workshopkubectl --kubeconfig=/path/to/kubeconfig get pods -n workshop
Enter fullscreen mode Exit fullscreen mode
  1. Verify that the frontend and backend deployments are running.

Make a Change in GitHub

  1. Make another change in your GitHub repository, commit, and push.

  2. Jenkins should automatically trigger the pipeline again.

Verify Updated Deployment

  1. Check the AWS ECR console for new image versions.

  2. Monitor the Jenkins dashboard for the progress of each stage in the updated pipeline.

  3. Verify that the changes reflect in your Kubernetes deployment.

Conclusion

If you can access the application through the DNS and see the tasks you created in the MongoDB database, congratulations! You have successfully deployed and tested your three-tier application on Kubernetes.

Remember to clean up your resources after testing by deleting the EKS cluster and any associated resources to avoid incurring unnecessary costs. Use the following command:

eksctl delete cluster --name three-tier-cluster --region us-west-2
Enter fullscreen mode Exit fullscreen mode

This will delete the EKS cluster and associated resources.

Top comments (0)