Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Deploy Nginx on a Kubernetes Cluster?
Nginx is a high-performance web server widely used for load balancing, reverse proxying, and serving static content. Kubernetes is a container orchestration platform that automates deployment, scaling, and management of containerized applications. Deploying Nginx on Kubernetes provides scalability, high availability, and efficient resource management for web applications.
Prerequisites
Before deploying Nginx on Kubernetes, ensure you have the following
Kubernetes Cluster Access A running cluster (local minikube, cloud provider like GKE, EKS, or AKS)
kubectl CLI Command-line tool configured to connect to your cluster
Basic Kubernetes Knowledge Understanding of Pods, Services, Deployments, and Ingress
Nginx Familiarity Basic knowledge of nginx.conf and common directives
Step 1 Create Namespace and ConfigMap
First, create a dedicated namespace for organizing your Nginx deployment
kubectl create namespace nginx-demo
Create a ConfigMap to store custom Nginx configuration
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
namespace: nginx-demo
data:
nginx.conf: |
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
}
kubectl apply -f nginx-configmap.yaml
Step 2 Create Nginx Deployment
Create a Deployment manifest to run Nginx pods with the custom configuration
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx-demo
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
kubectl apply -f nginx-deployment.yaml
Step 3 Create Service
Expose the Nginx deployment through a Service to enable internal cluster access
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: nginx-demo
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
kubectl apply -f nginx-service.yaml
Step 4 Configure Ingress for External Access
Create an Ingress resource to allow external traffic to reach your Nginx service
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: nginx-demo
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: nginx.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
kubectl apply -f nginx-ingress.yaml
Verification and Testing
Verify that your Nginx deployment is running successfully
kubectl get pods -n nginx-demo kubectl get services -n nginx-demo kubectl get ingress -n nginx-demo
Test the deployment by port-forwarding to access Nginx locally
kubectl port-forward svc/nginx-service 8080:80 -n nginx-demo
Open your browser and navigate to http://localhost:8080 to see the Nginx welcome page.
Scaling and Management
Scale the Nginx deployment to handle more traffic
kubectl scale deployment nginx-deployment --replicas=5 -n nginx-demo
Monitor the deployment status
kubectl rollout status deployment/nginx-deployment -n nginx-demo
Deployment Architecture
Conclusion
Deploying Nginx on Kubernetes provides a scalable, resilient web server solution with automated management capabilities. The combination of Deployments, Services, and Ingress controllers ensures high availability and efficient traffic distribution. This architecture enables easy scaling, configuration management, and seamless updates for production web applications.
