Ingress : In a Kubernetes environment, the frontend application, backend, and database are distributed across different locations. Their communication is facilitated internally through services. To make them accessible externally, an ingress controller is employed for routing. Additionally, to handle high traffic, a load balancer is utilized.
The Ingress Controller serves as a way to expose services to the external world. By utilizing an Ingress Controller, you can streamline the process of exposing services to the outside world while also providing a centralized point for managing external access rules. Popular Ingress Controllers include NGINX Ingress Controller, Traefik, and HAProxy Ingress.
To set up an Ingress Controller, you typically deploy it as a separate component in your Kubernetes cluster, and then you define Ingress resources to specify how external traffic should be handled.
https://github.com/LondheShubham153/TWSThreeTierAppChallenge.git
Or,
https://github.com/priya141/ThreeTierApp.git
Step 1 : Create EC 2 Instance (T2.micro) and connect to local machine using SSH
Step 2 : Git Clone https://github.com/LondheShubham153/TWSThreeTierAppChallenge.git
Step3 : Install Docker
sudo apt-get update
sudo apt install docker.io
docker ps
sudo chown $USER /var/run/docker.sockGo to TWSThreeTierAppChallenge File and;
Create Dockerfile and Docker Image of Frontend
- CD Frontend
- Create Dockerfile
- docker build -t three-tier-frontend (Docker Image and we will push this image to ECR)
- docker run -d -p 3000:3000 three-tier-frontend:latest (Just to run container and for learning purpose)
- Now open port 3000 into the security group of Instance
- We will kill the container using docker kill command like docker kill container_id since we are not using container to run the application.
- Now we will create ECR (instead of dockerHub) and before creating ECR we will install AWS CLI v2. Once ECR will be created we will push the docker image to ECR.
Step 4: Install AWS CLI v2 ( Install all in the home directory either it will be installed in the git)
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 aws configure
After installing AWS CLI now it should be configured and for that we need IAM user
IAM Configuration
- Create a user
eks-adminwithAdministratorAccess. - Generate Security Credentials: Access Key and Secret Access Key.
Now we will make credentials
Will create access key to interact with AWS on local machine
Now get back to ECR
Now run the first command to login to ECR
Now run the docker build command and before running docker build command go the the frontend directory
After the build completes tag your image so you push the image to the repository
Now, Run the push command to push the image to created AWS repository (ECR)
Frontend image is pusehd to ECR
Create Dockerfile and Docker Image of Backend
- CD Backend
- Create Dockerfile
# Use the official Node.js image as the base image
FROM node:14-alpine
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and package-lock.json to the working directory
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code to the working directory
COPY . .
# Expose the port that the app will run on
EXPOSE 8080
# Define the default command to run the application
CMD ["node", "index.js"]
Now go to ECR and create repository for Backend
Run all the commands given in the image to build image on the ECR
ECR Image-Backend
Now run the docker build command given in the above image and then run the docker run command to make the container
Command Docker ps will show the container running
Note: To check if the backend is running properly we will use the Command docker log <container_id>
In the above image if you see the error like couldnt connect to database it means it's not connected to the MongoDB database.
As given in the image ECR Image-Backend we will run pint 3 and point 4 too.
Backend image is pusehd to ECR
Create Dockerfile and Docker Image of Database
We will run the MongoDB to k8s cluster
Step 5: Install kubectl (In the Home directory)
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 (In the Home directory)
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 (EKS cluster will be created through CloudFormation)
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
aws eks update-kubeconfig --region us-west-2 --name three-tier-cluster
Above command is used to show the cluster info of three-tier-cluster only and after that if we run kubectl get nodes then we will get nodes of three-tier-cluster only
Step 8: Run Manifests
MongoDB manifests files
Frontend layer will be exposed with ALB (Amazon load balancer)
Step 9: Install AWS Load Balancer
We can change the policy name
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
We should change IAM id and Policy name
Now we have created service account of load balancer
Step 10: Deploy AWS Load Balancer Controller
Now we will Install Helm Chart
- Now, we wll need Ingress Controller to access the applications. We will install Ingress Controller through Helm. Search on Google with the Keyword "helm chart alb ingress controller"
- https://github.com/aws/eks-charts/tree/master/stable/aws-load-balancer-controller/templates
- To run load balancer we dont need to write menifests file as it's already given to the helm chart. The only thing we have to install the Helm Chart
Helm Chart
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
Now we will add the addess k8s.workshop-mainlb... to the GoDaddy and will click on Add new Record
If we will add IP address then will select A type but here we are adding a custom domain k8s.workshop-mainlb.. so we will select record type CNAME
Application is running now!!
To enter into the MongoDb database
Cleanup
- To delete the EKS cluster:
eksctl delete cluster --name three-tier-cluster --region us-west-2Important Notes:
1. When you run chmod +x filename, you are granting the execute permission to the owner, group, and others for the specified file. This allows the file to be executed as a program or script.
chmod +x myscript.sh
This command would make the file myscript.sh executable. After running this command, you can run the script using:
./myscript.sh
2. sudo mv ./kubectl /usr/local/bin
The command sudo mv ./kubectl /usr/local/bin is typically used to move the kubectl binary to the /usr/local/bin directory
./kubectl: Specifies the source file, in this case, thekubectlbinary in the current directory (./)./usr/local/bin: Specifies the destination directory where thekubectlbinary will be moved to./usr/local/binis a common location for system-wide executables in Unix-like systems.
3. The --silent flag, when used with a command or script, is typically an option to suppress or reduce the output of that command. It's often used to make the execution quieter, especially in automated or scripted environments.
4. aws eks update-kubeconfig --region us-west-2 --name three-tier-cluster
The aws eks update-kubeconfig command is used to update the kubeconfig file for an Amazon EKS (Elastic Kubernetes Service) cluster. It configures kubectl to communicate with the specified EKS cluster.
Summary
- Create EC 2 Instance (T2.micro) and connect to local machine using SSH
- Git Clone https://github.com/LondheShubham153/TWSThreeTierAppChallenge.git
- Install Docker
- Now we will create ECR
- Install AWS CLI v2
- IAM Configuration
- Push docker images to the ECR
- Install kubectl (In the Home directory)
- Install eksctl (In the Home directory)
- Setup EKS Cluster (EKS cluster will be created through CloudFormation)
- Run Manifests of Backend, Frontend and MongoDB
- Frontend layer will be exposed with ALB (Amazon load balancer)
- Install AWS Load Balancer
- Deploy AWS Load Balancer Controller through Helm Chart
- Install Helm Chart
- 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
Application is done !!




























Post a Comment