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

Nginx on Kubernetes Architecture External Traffic Ingress Controller Nginx Service Nginx Pod 1 Nginx Pod 2 Nginx Pod 3 ConfigMap PersistentVolume Load balancing across multiple Nginx pods ConfigMap provides configuration, PV stores persistent data

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.

Updated on: 2026-03-17T09:01:38+05:30

537 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements