Nginx is a powerful tool for handling web traffic with ease. As a reverse proxy, it forwards requests to the appropriate service to be handled according to defined rules. It works as a load balancer by distributing incoming traffic across multiple instances, preventing overload on any one service.
Pre-requisite:
AWS EC2: t2. Medium
Docker
Kind:
Kubernetes config.yaml:
Step 1 : SetUp Kubernetes Cluster
mkdir k8s-files cd k8s-files vim config.yaml
kind: Cluster apiVersion: kind.x-k8s.io/v1alpha4 nodes: - role: control-plane image: kindest/node:v1.32.1 - role: worker image: kindest/node:v1.32.1 - role: worker image: kindest/node:v1.32.1
kind create cluster --config config.yml --name <any name as you want >
kubectl get nodes
if your getting any problem to set Kubernetes cluster you can check this (https://how-to-set-up-a-kubernetes-cluster.hashnode.dev/how-to-set-up-a-kubernetes-cluster-using-kind-a-comprehensive-guide)
Step 2 : Create a script for Installation (Docker,Kind,Kubectl)
mkdir k8s-files
cd k8s-files
vim install.sh
Step 3: Copy this script
#!/bin/bash
set -e # Exit on error
echo " Updating package lists..."
sudo apt update -y
# Install dependencies
echo "Installing dependencies..."
sudo apt install -y curl wget apt-transport-https ca-certificates gnupg lsb-release
# Install Docker
if ! command -v docker &> /dev/null; then
echo " Installing Docker..."
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo tee /etc/apt/keyrings/docker.asc > /dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update -y
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER
echo "Docker installed successfully! Please log out and log back in to apply group changes."
else
echo "✔️ Docker is already installed."
fi
# Install Kind
if ! command -v kind &> /dev/null; then
echo "Installing Kind..."
curl -Lo ./kind "https://kind.sigs.k8s.io/dl/v0.22.0/kind-linux-amd64"
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
echo " Kind installed successfully!"
else
echo "✔️ Kind is already installed."
fi
# Install Kubectl
if ! command -v kubectl &> /dev/null; then
echo "🚀 Installing Kubectl..."
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
echo " Kubectl installed successfully!"
else
echo "Kubectl is already installed."
fi
# Verify installations
echo " Verifying installations..."
docker --version && kind --version && kubectl version --client
echo "🎉 Installation completed successfully!"
chmod + x install.sh
./install.sh
Step 4 : Create namespace for creating different work environment
vim namespace.yaml
kind: Namespace
apiVersion: v1
metadata:
name: nginx
Step 5: Apply this yaml file
kubectl apply -f namespace.yaml
Step 6: Create Deployment.yaml
vim deployment.yaml
kind: Deployment
apiVersion: apps/v1
metadata:
name: nginx-deployment
namespace: nginx
labels:
app: nginx-app # Matching the Service selector
spec:
replicas: 3
selector:
matchLabels:
app: nginx-app # Matching the Service selector
template:
metadata:
labels:
app: nginx-app # Matching the Service selector
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Step 7 : Apply deployment.yaml
kubectl apply -f deployment.yaml
Step 8: Create pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: nginx-pod
namespace: nginx
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
Step 9: Apply pod.yaml
kubectl apply -f pod.yaml
Step 10: Create service.yaml
The service.yaml is a YAML configuration file used to describe a Kubernetes Service. It contains descriptions of how a service in Kubernetes should be created and how it will communicate with other components, such as Pods, in your cluster.
The Service resource is the one that provides network connectivity to a group of Pods, and it can expose your application either internally within the cluster or externally to outside clients. There are several types of services: ClusterIP, NodePort, LoadBalancer, and ExternalName.
kind: Service
apiVersion: v1
metadata:
name: nginx-service
namespace: nginx
spec:
selector:
app: nginx-app # Change this to match Deployment label
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30000
type: NodePort
Step 11: Apply service.yaml
kubectl apply -f service.yaml
Now We need Port-Forward which help to access nginx on web browser
kubectl get svc -n nginx
# Check if the service is running
kubectl port-forward service/nginx-service -n nginx 8080:80 --address=0.0.0.0 &
Step 12: Now we Need to open EC2 port to access the nginx
Open Firewall Rules in Security Group .Go to AWS EC2 Console → Security Groups.
Find the security group attached to your EC2 instance.
Edit Inbound Rules:
Protocol: TCP
Port Range: 30000-32767 (for NodePort) or 8080 (if using port-forward)
Source: 0.0.0.0/0 (or restrict to your IP)
Save changes.
Output Should be :