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 commandaws configure
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
Step 4: Install Docker
sudo apt-get updatesudo apt install docker.iodocker pssudo 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/kubectlchmod +x ./kubectlsudo mv ./kubectl /usr/local/binkubectl 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 /tmpsudo mv /tmp/eksctl /usr/local/bineksctl 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 2aws eks update-kubeconfig --region us-west-2 --name three-tier-clusterkubectl get nodes
Step 8: Clone GitHub Repository
git clone https://github.com/ArjunMnn/TWSThreeTierAppChallengecd 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 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
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
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 commandkubectl exec -it -n workshop <mongodb-pod-name> -- mongo
Check Database and Collection
use tododb.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:
Configure Kubeconfig Credential
In Jenkins, go to "Manage Jenkins" > "Manage Credentials."
Under the "Stores scoped to Jenkins" section, click on "(global)".
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
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 workshopkubectl --kubeconfig=/path/to/kubeconfig get services -n workshopkubectl --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.
Top comments (0)