DevOps Project - 14 (Step-by-step Implementation)
DevOps Project - 14: 3 Tier Application Deployment using Kubernetes EKS Cluster.
1. To start with three tier application deployment
2. Now we will create an EC2 instance
3. Allow port 3000 in Inbound rules.
4. Connect to the created server and clone the code from the repository.
5. Now, the next step is to create a Dockerfile for frontend
6. Let’s start with creating a Dockerfile for frontend.
7. Run “vim Dockerfile” and start writing.
8. Now, we will Install Docker, by running the following command:
sudo apt-get update
sudo apt install docker.io
docker ps
sudo chown $USER /var/run/docker.sock
9. Now, we can see docker has been installed.
10. Now, we will build the created Dockerfile for frontend.
11. We will check it by running “docker images”. And we can see that the built image is present.
12. Now, we will run the built image by running following command:
Docker run -d -p 3000:3000 threetier-frontend:latest
13. We will test it by running “public ip of EC2:3000”
14. As, we have done with the frontend testing, now for the time being we can kill the container.
15. Next step is to install AWS CLI
16. Create Security Credentials for AWS CLI.
17. Now we will install AWS CLI v2 by running following commands:
sudo apt install unzip
unzip awscliv2.zip
sudo ./aws/install -i /usr/local/aws-cli -b /usr/local/bin --update
18. This way we will configure the AWS CLI with our account.
aws configure
19. Now, we will navigate to Amazon ECR.
20. Now, start a public repository by giving a name as per your choice and click on “Create Repository”.
21. Now, we will push that front-end image that we created. For that we will open the repository and click on “View Push Commands”. And run them one by one.
22. By following these steps our image will be pushed to the ECR Repository.
23. Now we will start with a backend. Navigate to backend folder where all the code is present.
24. Now, we will create a Dockerfile of the backend code.
vim Dockerfile
25. Now, we will create a new repository to store an image of backend.
26. Now, again we will push that back-end image that we will create. For that we will open the repository and click on “View Push Commands”. And run them one by one.
27. Image has been built and uploaded to threetier-backend repository.
28. Now, we will start a container with the built image by running following command:
docker run -d -p 8080:8080 threetier-backend:latest
29. Now, both frontend as well as backend are ready. It’s time to create Kubernetes cluster.
30. To work with Kubernetes, firstly we will Install kubectl, by running the following command:
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
31. Now we will Install eksctl, by running the following command:
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
32. Now, we will Setup EKS Cluster that will include minimum and maximum of two t2.medium nodes, by running the following command:
eksctl create cluster --name three-tier-cluster --region us-east-1 --node-type t2.medium --nodes-min 2 --nodes-max 2
33. After running first command, It will deploy a stack over the AWS CloudFormation.
34. We can check what things are going on behind the scenes, by navigating to CloudFormation. Here we can see that a Stack is in the progress.
35. By navigating to Stack and go to Resources, here we can see what are the things that are being created. It will take around 20 minutes.
Have a Cup of Coffee!! ☕
36. This will create the Stack. As well as all the required resources such as two EC2 nodes, Elastic Kubernetes Service etc.
Recommended by LinkedIn
37. Now, we will bind a particular EKS cluster with the virtual machine to work via CLI with following commands:
aws eks update-kubeconfig --region us-east-1 --name three-tier-cluster
38. Now, we will check the nodes by running:
kubectl get nodes
39. Now, we have manifest files present in the GitHub repository, under the folder /k8s_manifests. If you want to create then, you can follow the official documentation here: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
40. To deploy the manifest files, we need to create a namespace first.
Kubectl create namespace workshop
41. Now, we will deploy the manifest files by running command:
kubectl apply -f deploy.yaml
42. Now, we can view any of the Deployments present inside the namespace by running command:
kubectl get deployment -n workshop
43. Here, we can see that mongodb isn’t ready, the reason is we haven’t passed the secrets yet. We will pass the secrets by using the command:
kubectl apply -f secrets,yaml.
44. As, secrets have been created. Now, if we check deployments again, then the deployment of mongodb will be ready and it is running.
45. Now, we will create a service with which it will help mongodb to communicate internally.
46. Now, we will create deployment as well as service for backend:
Kubectl apply -f backend-deployment.yaml
47. Now, we have two tiers ready with us, i.e. mongodb and backend. We will run the following command to check it:
kubectl get pods -n workshop
48. Now, we will check the logs of backend pod again, as last time it wasn’t connecting to the database. And Boom!!! It is listening to port 8080.
49. Now, we will work on frontend tier pod, and we will create deployment as well as service for the same.
50. Now, we will check the pod count, with following command:
kubectl get pods -n workshop
Boom!! All the tiers are up!!!
51. And, if we need to do internal routing then we will use Ingress controller. We will install HELM package manager that will include the Kubernetes manifest files to create ingress controller.
52. We will now install Application Load Balancer (to access it from the internet) in EKS Cluster with the help of eksctl.
53. We need an account to perform the installation for that we will install the AWS IAM policy by running the command:
54. We will create IAM Policy for the connectivity of Application Load Balancer and EKS, by running following command:
aws iam create-policy --policy-name AWSLoadBalancerControllerIAMPolicy --policy-document file://iam_policy.json
55. Run the following commands for IAM policy addition:
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
56. Now, service account with the help of Cloud Formation, will update the EKS cluster.
57. We have successfully created the service account for Load Balancer.
58. Now, we will deploy Application Load Balancer inside the EKS cluster with the help of following commands:
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 apply -f full_stack_lb.yaml
59. To see if my ALB is running or not, we will use the following command:
kubectl get deployment -n kube-system aws-load-balancer-controller
We have set replicacount: 2, so it is showing the status as two.
60. Now, we need to install an Ingress controller for internal routing. For that we need to browse to “/k8s_manifests/full_stack_lb.yaml”
61. Now, we will create it with the help of following command:
kubectl apply -f full_stack_lb.yaml
62. Now, we will verify the recently created ingress with the help of command:
Kubectl get ing -n workshop
63. Now, all the three tiers, one Application Load Balancer and one Ingress Controller has been deployed successfully.
64. Now, we will map the domain. For that we need to have a domain name.
65. Suppose we have domain named trainwithshubham.com, we will navigate inside the domain on GoDaddy and create a New Record with following details:
Type: CNAME
Name: challenge
TTL: Custom >> 600 seconds.
And click on Save.
66. If someone will search for “challenge.trainwithshubham.com” they will redirect to “k8s-workshop-mainlb-7f430e3450-1036210311.us-west-2.elb.amazonaws.com”.
67. We can check the Load Balancers from the AWS console as well by navigating to EC2 >> Load Balancers.
68. Now, we will search as “challenge.trainwithshubham.com” and Booooooom!! 🎉🎉
Three tier application is Up!!!
(You can further play with the stuff on your level to enhance new features)
Hope you find this helpful. Do follow for more such content.
~ Chetan Rakhra.
Thank you for the insightful article .. great work
Great work, Chetan! The 3-tier application deployment using Kubernetes EKS Cluster sounds fascinating. Your step-by-step article is a valuable resource for the DevOps community. Keep up the fantastic contributions! 👏
Very Helpful
Thanks for posting
This is really a good effort from you