Deploying a Three-Tier Application with CI/CD using Jenkins, ReactJS, NodeJS, and MongoDB on Kubernetes
Table of contents
- Introduction
- Prerequisites
- Step 1: IAM Configuration
- Step 2: EC2 Setup
- Step 3: Install AWS CLI v2
- Step 4: Install Docker
- Step 5: Install kubectl
- Step 6: Install eksctl
- Step 7: Setup EKS Cluster
- Step 8: Clone GitHub Repository
- Step 9: Build Docker Images
- Step 10: Create Kubernetes Manifests
- Step 11: Install AWS Load Balancer
- Step 12: Test the Application
- Step 13: Configure Jenkins for CI/CD
- Step 14: Verify CI/CD Deployment
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:
AWS account with IAM administrator access.
An EC2 instance with Ubuntu in your preferred region.
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 command
aws configure
Step 2: EC2 Setup
Launch an Ubuntu instance and SSH into it.
Use atleast t2.medium, because t2.micro would hang while running the build with Jenkins.
Step 3: Install AWS CLI v2
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
Step 4: Install Docker
sudo apt-get update
sudo apt install docker.io
docker ps
sudo chown $USER /var/run/docker.sock
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/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin
kubectl version --short --client
Step 6: Install eksctl
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
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 2
aws eks update-kubeconfig --region us-west-2 --name three-tier-cluster
kubectl get nodes
Step 8: Clone GitHub Repository
git clone https://github.com/ArjunMnn/TWSThreeTierAppChallenge
cd TWSThreeTierAppChallenge
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 workshop
kubectl apply -f backend-deployment.yaml
kubectl apply -f backend-service.yaml
kubectl apply -f frontend-deployment.yaml
kubectl apply -f frontend-service.yaml
kubectl apply -f deploy.yaml
kubectl apply -f secrets.yaml
kubectl apply -f service.yaml
kubectl apply -f full_stack_lb.yaml
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.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
sudo snap install helm --classic
helm repo add eks https://aws.github.io/eks-charts
helm repo update eks
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
kubectl get deployment -n kube-system aws-load-balancer-controller
kubectl apply -f full_stack_lb.yaml
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
On the frontend, you should see the application interface.
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 command
kubectl exec -it -n workshop <mongodb-pod-name> -- mongo
Check Database and Collection
use todo
db.tasks.find()
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
In Jenkins, go to "Manage Jenkins" > "Manage Credentials."
Under the "Stores scoped to Jenkins" section, click on "(global)".
Add new credentials:
Kind: Secret text
Access Key ID: Use the Jenkins credentials ID for Access Key.
Secret Access Key: Use the Jenkins credentials ID for Secret Access Key.
Configure Kubeconfig Credential
In Jenkins, go to "Manage Jenkins" > "Manage Credentials."
Under the "Stores scoped to Jenkins" section, click on "(global)".
Add new credentials:
Kind: Secret file
File: Upload the kubeconfig file.
ID: Set a unique ID for the credential.
Note: Kubeconfig file is in the path /home/ubuntu/.kube/config.
Download it to your local using scp.
Create a Jenkins Pipeline Job
In Jenkins, click on "New Item."
Choose "Pipeline" and give your job a name.
Under the "Pipeline" section, select "Pipeline script from SCM."
Set the SCM to Git and provide your repository URL.
In the "Script Path," specify the path to your Jenkinsfile (e.g.,
Jenkinsfile
).Save the job.
Trigger the Pipeline
Make a change in your GitHub repository, commit, and push.
Jenkins should automatically detect the change and trigger the pipeline.
Monitor the Jenkins dashboard for the progress of each stage in the pipeline.
Step 14: Verify CI/CD Deployment
Check AWS ECR
Open the AWS ECR console.
Verify that the images for the frontend and backend have been pushed successfully.
Check Kubernetes Deployment
- Open your Kubernetes dashboard or use the following command:
kubectl --kubeconfig=/path/to/kubeconfig get deployments -n workshop
kubectl --kubeconfig=/path/to/kubeconfig get services -n workshop
kubectl --kubeconfig=/path/to/kubeconfig get pods -n workshop
- Verify that the frontend and backend deployments are running.
Make a Change in GitHub
Make another change in your GitHub repository, commit, and push.
Jenkins should automatically trigger the pipeline again.
Verify Updated Deployment
Check the AWS ECR console for new image versions.
Monitor the Jenkins dashboard for the progress of each stage in the updated pipeline.
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
This will delete the EKS cluster and associated resources.
Follow me on LinkedIn.
Checkout my GitHub profile.