How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study

Visal Tyagi
DevOps-Guides
Published in
10 min readJul 20, 2024

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.

How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study
How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study

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”.

Go to Instances
Go to Instances

Step 2: Click on “Launch Instances”.

Create New Instance
Create New Instance

Step 3: Choose “K8s-Case-Study” in “Name” in the “Name and tags” section.

Write Instance Name Here
Write Instance Name Here

Step 4: Choose “AMI” as “Ubuntu” & “AMI Version” as “Ubuntu Server 20.04 LTS (HVM), SSD Volume Type”.

Choose “Ubuntu AMI”
Choose “Ubuntu AMI”

Step 5: Choose “Instance type” as “t2.medium”.

Choose the t2.medium machine
Choose the t2.medium machine

Step 6: Choose “Key pair (login)” as “Docker”.

Choose a Key Pair
Choose a Key Pair

Step 7: Click on “Edit” in the “Network Settings”.

Edit the Network Settings
Edit 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

Choose the VPC and Subnet Here
Choose the VPC and Subnet Here

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.

Select the Security Group
Select the Security Group

Step 10: In the “Configure Storage”, leave the volume as by default (8 GiB).

Root Volume
Root Volume

Step 11: Click on the “Launch Instance”.

Launch the Instance
Launch the Instance

Step 12: Click on the “Hyperlink”.

Click on Hyperlink
Click on Hyperlink

Step 13: Your instance will be in the “Running State”.

Instance Running
Instance Running

Step 14: Select the “Instance (K8s-Case-Study) & click on “Connect”.

Select the Instance
Select the Instance

Step 15: Click on “Connect”.

Connect to the Instance
Connect to the Instance

Step 16: First, update the machine using this command:

sudo apt-get update
Update the Machine
Update the Machine

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.

Create a bash file
Create a bash file

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
Minikube & Docker Installation Commands
Minikube & Docker Installation Commands

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
Run the Commands using Bash
Run the Commands using Bash

Press “Enter” from the keyboard. The installation will be started.

MiniKube Setup
MiniKube Setup
Ingress Started Installing
Ingress Started Installing

Step 20: The “Minikube” has been successfully installed on the machine.

Ingress Addon Enabled
Ingress Addon Enabled

Step 21: For finding the nodes, run the below-given command:

kubectl get nodes
Get the Minikube Nodes
Get the Minikube 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
Copy the Repository URL
Copy the Repository URL

Step 2: Type the below-given command in the EC2 Instance & press enter from the keyboard:

git clone https://github.com/hshar/website.git
Clone the Repository
Clone the Repository

It will be started cloning the website from Git.

Step 3: Type the below-given command to show how many directories are present:

ls
website directory
website directory

Step 4: For going to the “website” directory, type the below-given command:

cd website
Go to the website directory
Go to the website directory

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.

Create a Dockerfile
Create a Dockerfile

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/
Paste the Apache Installation Commands
Paste the Apache Installation Commands

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 .
Create a Docker Image
Create a Docker Image

Step 4: The image will be successfully created. Now run the below-given command to view the images in the website directory:

docker images
Docker Images
Docker Images

Step 5: Use the below-given command to rename the image:

sudo docker tag img visaltyagi12/k8casestudy
Rename the Image As per DockerHub
Rename the Image As per DockerHub

Step 6: Type the below-given command to view the image:

docker images
Docker Images Directory
Docker Images Directory

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.

Login into the DockerHub Account
Login into the DockerHub Account

Step 8: Use the below-given command to push the image in the “Docker Hub” account:

docker push visaltyagi12/k8casestudy
Push the Image to the DockerHub Account
Push the Image to the DockerHub Account

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.

Login to the DockerHub Account
Login to the DockerHub Account

Step 10: You will notice the pushed images in the DockerHub account.

DockerHub Account Custom Images
DockerHub Account Custom Images

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
Custom Deployment Created
Custom Deployment Created

Step 2: Run the below-given command to check the pods:

kubectl get pods
Custom Pods
Custom 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.

Apache Deployment Created
Apache Deployment Created

Step 2: When you run the below-given command:

kubectl get pods
Apache Pods
Apache 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.
Expose apache service to NodePort
Expose apache service to 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
Custom Deployment Exposed on NodePort
Custom Deployment Exposed on NodePort

Step 3: Run the below-given command to find the service:

kubectl get svc -o wide

Both services are successfully exposed on “NodePort”.

Apache & Custom Services
Apache & Custom Services

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
Create the ingress.yaml file
Create the ingress.yaml file

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
Paste the Code to ingress.yaml file
Paste the Code to ingress.yaml file

Step 4: Create an “ingress.yaml” file using the below-given command:

kubectl create -f ingress.yaml
Create the Ingress
Create the Ingress

The “ingress” deployment will be created.

Step 5: Run the below-given command to view the ingress:

kubectl get ing
View the Ingress
View the Ingress

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.

Port Forwarding
Port Forwarding

The port will be successfully forwarded.

Paste the 46131 After the IP Addresses
Paste the 46131 After the IP Addresses

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:

Apache Website
Apache Website

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:

How to Deploy a Sample Website on Kubernetes Using Ingress — Kubernetes Case Study
Custom Website

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

Create Two Server Groups & Install Apache on The First Group & NGINX on the Second Group — Ansible Case Study

--

--