Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views16 pages

K8s - Deploying Apache App and Custom App Using Ingress

The document outlines the steps to deploy an Apache application and a custom app using Kubernetes with Ingress. It details the implementation of a microservice architecture by deploying two replicas of an Apache2 application, containerizing a GitHub project, and setting up Ingress rules for routing. Additionally, it provides instructions for installing Kubernetes using Kubeadm, creating deployments, and verifying the setup on AWS EC2 instances.

Uploaded by

starck839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

K8s - Deploying Apache App and Custom App Using Ingress

The document outlines the steps to deploy an Apache application and a custom app using Kubernetes with Ingress. It details the implementation of a microservice architecture by deploying two replicas of an Apache2 application, containerizing a GitHub project, and setting up Ingress rules for routing. Additionally, it provides instructions for installing Kubernetes using Kubeadm, creating deployments, and verifying the setup on AWS EC2 instances.

Uploaded by

starck839
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

KUBERNETES - DEPLOYING APACHE APPLICATION AND

CUSTOM APP USING INGRESS

You have just joined a startup Ventura Software as a Devops Lead Engineer. The company
relies on a Monolithic Architecture for its product. Recently, the senior management was hired.
The new CTO insists on having a Microservice 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. Deploy an Apache2 deployment of 2 replicas


2. Sample code has been checked-in at the following Git-Hub 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

3. Deploy Ingress with the following rules:


i) */apache* should point to the apache pods
ii) */custom* should point to the GitHub application

What is Kubernetes (k8s)?

In its simplest terms, Kubernetes is an open source container orchestration platform. It automates
the deployment, scaling, and management of containerized applications.

Once you can implement one Master/Worker Node configuration, you can implement many
clusters, quickly, with the use of automated tools and Infrastructure as a Service (IaC), such as
Terraform, AWS Cloud Formation, Azure Resource Manager (ARM) templates, etc.

Project Author: Marianne Gleason Page 1


First let’s look at a basic architecture diagram for a cluster in Kubernetes to get used to the
structure for a cluster.

Project Author: Marianne Gleason Page 2


Now, let’s look at a more detailed architecture, in which I have defined some of the terms for this
project and kubernetes, in general.

Project Author: Marianne Gleason Page 3


Ways To Install Kubernetes:
There are numerous ways to install Kubernetes. Below are four popular ways:

1. Kubeadm – Bare Metal Installation


2. Minikube – Virtualized Environment for Kubernetes
3. Kops – Kubernetes on AWS
4. Kubernetes on GCP – Kubernetes running on Google Cloud Platform (GCP).

For the purposes for this project, I will be using the Kubeadm method.

Installation Architecture Using Kubeadm:

Project Author: Marianne Gleason Page 4


Kubernetes Architecture (Master/Worker Node) For This
Project?

Steps For This Project

Step1. Launch 2 AWS EC2 instances (t2.medium (master) & t2.micro (workers)) and install
kubernetes on master and worker node(s).

Project Author: Marianne Gleason Page 5


Step 2. Installation Docker and Kubernetes, Kubeadm, Kubelet, and Kubectl on the various
nodes below:

On Master & Worker nodes


Master server t2.med , t2.micro for worker nodes.
sudo su
apt-get update &&
apt-get install docker.io -y &&
service docker restart &&
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - &&
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" >/etc/apt/sources.list.d/kubernetes.list
&&
apt-get update &&
apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

On Master & Worker Nodes:

sudo su
create a script file k.sh

sudo nano k.sh

apt-get update
apt-get install docker io –y
service docker restart
curl –s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
echo “deb http://apt.kuberneter.io/ kubernetes-xenial main” >/etc/apt/sources.list.d/kubernetes.list
apt-get update
apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 –y

To execute the script file: ./k.sh

On Master:
(Bootstrapping The Cluster):

kubeadm init --pod-network-cidr=192.168.0.0/16


exit

Join worker nodes by running the following on each node:

kubeadm token create --print-join-command (if you lose the token use this command to recover)

Project Author: Marianne Gleason Page 6


Copy and Paste to the worker node:

kubeadm join 172.31.9.44:6443 --token 4qcgi4.7boiia35u539msc1 \


--discovery-token-ca-cert-hash
sha256:befa9ca440ee5afb280282105667018c464000d7b91359bd267fb11fb4b1fdf4

On Master:

(Set up the kubeconfig):

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

(Configuring Networking):

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml


kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-
v0.49.0/deploy/static/provider/baremetal/deploy.yaml

Verify successful install and that all nodes have joined the cluster:

Project Author: Marianne Gleason Page 7


Step 3. Create Apache pods deployment with 2 replicas on the cluster:

ubuntu@ip-172-31-87-27:~$ sudo nano apache.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: apache-deployment
labels:
app: apache
spec:
replicas: 2
selector:
matchLabels:
app: apache
template:
metadata:
labels:
app: apache
spec:
containers:
- name: apache
image: ubuntu/apache2
ports:
- containerPort: 80

Step 4. Create pods for apache deploy

sudo su

root@ip-172-31-87-27:/home/ubuntu# kubectl create -f apache.yml

Verify pods are deployed and running by the below command in the screen shot:

Project Author: Marianne Gleason Page 8


Step 5. Fork GitHub Repo & Clone

https://github.com/hshar/website.git

Fork into your GitHub account:

Clone to repository using HTTPS and push to Docker Hub

In GitHub
Code > HTTPS
Copy HTTPS link

On master clone:

root@ip-172-31-87-27:/home/ubuntu# git clone https://github.com/magwork14/website.git

Project Author: Marianne Gleason Page 9


Build a dockerfile to containerize the code then verify file is in the correct subdirectory:

root@ip-172-31-87-27:/home/ubuntu/website# sudo nano Dockerfile

FROM ubuntu
RUN apt-get update
RUN DEBIAN_FRONTEND="noninteractive" apt-get -y install tzdata
RUN apt-get -y install apache2
ADD . /var/www/html
ENTRYPOINT apachectl -D FOREGROUND

Build Repository and Push to DockerHub:

Build:

root@ip-172-31-87-27:/home/ubuntu/website# sudo docker build . -t <YOUR USERNAME>/k8_casestudy

replace <YOUR USERNAME> with with your Docker ID

root@ip-172-31-87-27:/home/ubuntu/website# sudo docker build . -t magdevops22/k8_casestudy

Project Author: Marianne Gleason Page 10


Push To DockerHub:

root@ip-172-31-87-27:/home/ubuntu/website# docker login


Username: Your Docker ID
Below command docker push <Your DockerID/Your Docker Repository>

DockerHub verification:

Project Author: Marianne Gleason Page 11


Step 6. Create a custom deployment from custom image:

root@ip-172-31-87-27:/home/ubuntu/website# cd ..
root@ip-172-31-87-27:/home/ubuntu# ls
apache.yml website
root@ip-172-31-87-27:/home/ubuntu# sudo nano custom.yml

custom.yml file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: custom-deployment
labels:
app: custom
spec:
replicas: 2
selector:
matchLabels:
app: custom
template:
metadata:
labels:
app: custom
spec:
containers:
- name: custom
image: devopsintellipaat/kub8
ports:
- containerPort: 80

Deployment created and verified:

Project Author: Marianne Gleason Page 12


Step 7. Create the services

Step 8. Create & Deploy ingresses & rules which should point to apache pods:

st
1 service that we want to modify is apache so see below commands and ingress.yml file

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
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

Project Author: Marianne Gleason Page 13


When running the below command:

root@ip-172-31-87-27:/home/ubuntu# kubectl create -f ingress.yml

I was getting the following error message:

I ran the below command:

The ingress was now created successfully, as shown below:

Project Author: Marianne Gleason Page 14


Check all services and they are running successfully:

The below is the GitHub custom image from the ingress:

Access The Application: http://<node-ip>:<ingress-nodeport>/custom

http://3.87.22.13:31895/custom

Project Author: Marianne Gleason Page 15


Access The Application: http://<node-ip>:<ingress-nodeport>/apache

http://3.87.22.13:31895/apache

The below is Apache image from the ingress:

Project Author: Marianne Gleason Page 16

You might also like