How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study
Problem Statement:
You have just joined a start-up Ventura Software as a DevOps Lead Engineer. The company relies on a monolithic architecture for its products. Recently, the senior management was hired.
The new CTO insists on having a micro-service architecture. The development team is working on breaking the monolith. Meanwhile, you have been asked to host a test application on Kubernetes to understand how it works.
Following things have to be implemented:
1. Sample code has been checked-in at the following GitHub repo: https://github.com/hshar/website.git
You have to containerize this code, and push it to Docker Hub. Once done, deploy it on Kubernetes with 2 replicas.
2. Deploy an Apache2 deployment of 2 replicas
3. Deploy ingress with the following rules:
a. */apache* should point to the Apache pods
b. */custom* should point to the GitHub application.
Check the Git Hub Repository for this Assignment to Copy the Commands & Code:
Problem (1) Solution: Sample code has been checked-in at the following GitHub repo: https://github.com/hshar/website.git
You have to containerize this code, and push it to Docker Hub. Once done, deploy it on Kubernetes with 2 replicas
Follow these steps;
A. Create an EC2 Instance & Install Docker and MiniKube on It
Step 1: Go to “Services” section & search “EC2”. Put cursor over “EC2” & click on “Instances”.
Step 2: Click on “Launch Instances”.
Step 3: Choose “K8s-Case-Study” in “Name” in the “Name and tags” section.
Step 4: Choose “AMI” as “Ubuntu” & “AMI Version” as “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type”.
Step 5: Choose “Instance type” as “t2.medium”.
Step 6: Choose “Key pair (login)” as “Docker”.
Step 7: Click on “Edit” in the “Network Settings”.
Step 8: Choose the following options:
VPC- required: vpc-0f0baca9d8f4b299b (default-vpc-vpc)
Subnet: default-vpc-subnet-public1-us-west-2a
Auto-assign public IP: Enable
Step 9: In “Firewall (security groups)”, choose “Select exisiting security group”. While, in “Common security groups”, choose “default” security group because we have “All traffic” enabled here.
Step 10: In the “Configure Storage”, leave the volume as by default (8 GiB).
Step 11: Click on the “Launch Instance”.
Step 12: Click on the “Hyperlink”.
Step 13: Your instance will be in the “Running State”.
Step 14: Select the “Instance (K8s-Case-Study) & click on “Connect”.
Step 15: Click on “Connect”.
Step 16: First, update the machine using this command:
sudo apt-get update
Step 17: Now, we will install the Kubernetes, Docker & Minikube using the “bash” file. First, we will create a file using the below-given command:
nano a.sh
Press “Enter” from the keyboard.
A file will be opened.
Step 18: Paste the below-given content in the “a.sh” file:
sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl enable - now=docker
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64
sudo chmod 777 /var/run/docker.sock
minikube start - force - driver=docker
sudo snap install kubectl - classic
minikube status
minikube addons enable ingress
Do “CTRL+X” & press “Y” & after that press “Enter” from the keyboard.
Step 19: Run the below-given command to install Minikube, Docker & Kubernetes:
bash a.sh
Press “Enter” from the keyboard. The installation will be started.
Step 20: The “Minikube” has been successfully installed on the machine.
Step 21: For finding the nodes, run the below-given command:
kubectl get nodes
The master node will be successfully created.
B. Clone the Given Git Hub Repository on Created EC2 Machine
Step 1: Clone this repository in the “Git Hub Account”. Click on “Code” & Copy the URL:
https://github.com/hshar/website.git
Step 2: Type the below-given command in the EC2 Instance & press enter from the keyboard:
git clone https://github.com/hshar/website.git
It will be started cloning the website from Git.
Step 3: Type the below-given command to show how many directories are present:
ls
Step 4: For going to the “website” directory, type the below-given command:
cd website
C. Write a Docker file & Push the Image to Docker Hub
Step 1: Type the below-given command to create a Dockerfile:
nano Dockerfile
Press “Enter” from the keyboard.
Step 2: Type these commands to run “apache2” in the containerized environment:
FROM ubuntu
RUN apt-get update
RUN apt-get install apache2 -y
RUN apt-get install apache2-utils -y
RUN apt-get clean
ENTRYPOINT apachectl -D FOREGROUND
ADD . /var/www/html/
Press CTRL+X & Type Yes & Press Enter from the keyboard to save the File.
Step 3: Run the below-given command to build an image from the Dockerfile:
sudo docker build –t img .
Step 4: The image will be successfully created. Now run the below-given command to view the images in the website directory:
docker images
Step 5: Use the below-given command to rename the image:
sudo docker tag img visaltyagi12/k8casestudy
Step 6: Type the below-given command to view the image:
docker images
Step 7: To push the image to Docker Hub. First, login into the customer using the below-given command:
docker login
Type the username and password here.
Login will be successfully done.
Step 8: Use the below-given command to push the image in the “Docker Hub” account:
docker push visaltyagi12/k8casestudy
The image will be successfully pushed to the Docker Hub.
Step 9: Go to the “Docker Hub” account by login into the account by username and password.
Step 10: You will notice the pushed images in the DockerHub account.
D. Deploy the Custom Image with 2 Replicas
Step 1: Run the below-given command to create the custom image with 2 replicas:
kubectl create deployment custom --image=visaltyagi12/k8scasestudy --replicas=2 --port=80
A Custom Deployment will be created with Two Pods.
Run the below-given command to check how many deployments are present:
kubectl get deploy -o wide
Step 2: Run the below-given command to check the pods:
kubectl get pods
Two custom pods will be created.
Problem (2) Solution: Deploy the Apache2 with 2 Replicas
Step 1: Run the below-given command to create the deployment with 2 replicas:
kubectl create deployment apache --image=ubuntu/apache2 --replicas=2 --port=80
A deployment will be successfully created on port 80.
When you run the below-given commands:
kubectl get deploy –o wide
All the deployment details will be shown to you.
Step 2: When you run the below-given command:
kubectl get pods
Two Apache pods will be successfully created.
Problem (3) Solution: Deploy ingress with the following rules:
a. */apache* should point to the Apache pods
b. */custom* should point to the GitHub application.
Follow these steps;
A. Expose Both the Replicas on “NodePort” for Creating a Service
Step 1: First, we will expose “apache” on “Node Port”. Run the below-given command:
kubectl expose deploy apache --type=NodePort.
The apache service has been successfully created on “NodePort”.
Step 2: Second, we will expose the “custom” on “NodePort”. Use the below-given command:
kubectl expose deploy custom --type=NodePort
Step 3: Run the below-given command to find the service:
kubectl get svc -o wide
Both services are successfully exposed on “NodePort”.
B. Create an ingress.yaml file to point out the paths
Step 1: Create an “ingress.yaml” file using the below-given command:
nano ingress.yaml
Step 2: Paste the below-given content into the “ingress.yaml” file:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /apache
pathType: Prefix
backend:
service:
name: apache
port:
number: 80
- path: /custom
pathType: Prefix
backend:
service:
name: custom
port:
number: 80
Step 4: Create an “ingress.yaml” file using the below-given command:
kubectl create -f ingress.yaml
The “ingress” deployment will be created.
Step 5: Run the below-given command to view the ingress:
kubectl get ing
C. Apply Port Forwarding to Open the Web Page on Separate Port
Step 1: Paste the below-given command into “EC2 Machine”:
kubectl port-forward service/ingress-nginx-controller -n ingress-nginx --address 0.0.0.0 :443
Press “Enter” from the keyboard.
The port will be successfully forwarded.
D. Accessing the Web Page using Browser
Step 1: Go to the “Browser Address Bar” & type this URL there:
https://35.92.5.20:46131/apache
Your apache page has been successfully accessed like this:
Step 2: Go to the “Browser Address Bar” & type this URL there:
https://35.92.5.20:46131/custom
Your “GitHub Application Custom Web page” has been successfully accessed like this:
More Kubernetes HandsOn Assignments:
How to Deploy a Kubernetes Cluster & Create 3 Replica of Nginx Deployment — Kubernetes Assignment 1
How to Create A NodePort Service & Deploy NGINX on It — Kubernetes Assignment 2
How to Change the Replicas for the Deployment — Kubernetes Assignment 3
How to Change the NodePort Service to Cluster IP — Kubernetes Assignment 4
How to Deploy Ingress Service Using MiniKube in Kubernetes — Kubernetes Assignment 5
Other DevOps Case Studies:
Suggested a Git Workflow Architecture to Manage the Product Release — Git Case Study 1
Resolve Merge Conflict in The GitHub Repository — Git Case Study 2
Containerized an HTML Website using Docker on Production Environment — Docker Case Study
Creating an Architecture using Terraform on AWS — Terraform Case Study
Integration of DevOps Tools with Jenkins — Jenkins Case Study