Explore plans starting at ₹699/mo →
Kubernetes & Containers

How to Deploy a Web Application on Kubernetes

S
ServerRaja
10 min read
#Infrastructure#Tutorial#Guide#Docker#Containers#Kubernetes
How to Deploy a Web Application on Kubernetes

Deploying a web application on Kubernetes involves several steps: containerizing the application, creating Kubernetes manifests, deploying to the cluster, exposing it to the internet, and setting up scaling and monitoring.

Step 1: Containerize the Application

Create a Dockerfile for your application:

FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --production COPY . . EXPOSE 3000 CMD ["node", "server.js"]

Build and push the image: docker build -t registry.example.com/my-app:v1 . docker push registry.example.com/my-app:v1

Step 2: Create a Deployment

The Deployment manages your application pods:

apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app image: registry.example.com/my-app:v1 ports: - containerPort: 3000 resources: requests: memory: 256Mi cpu: 250m limits: memory: 512Mi cpu: 500m readinessProbe: httpGet: path: /health port: 3000 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: httpGet: path: /health port: 3000 initialDelaySeconds: 15 periodSeconds: 20

This deploys 3 replicas with resource constraints and health checks.

Step 3: Create a Service

The Service provides a stable endpoint for your pods:

apiVersion: v1 kind: Service metadata: name: my-app spec: selector: app: my-app ports: - port: 80 targetPort: 3000

Step 4: Create an Ingress

An Ingress routes external HTTP traffic to your Service:

apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-app annotations: cert-manager.io/cluster-issuer: letsencrypt spec: tls: - hosts: - app.example.com secretName: my-app-tls rules: - host: app.example.com http: paths: - path: / pathType: Prefix backend: service: name: my-app port: number: 80

Step 5: Apply and Verify

kubectl apply -f deployment.yaml kubectl apply -f service.yaml kubectl apply -f ingress.yaml

Verify: kubectl get pods, kubectl get svc, kubectl get ingress

Step 6: Configure Secrets and ConfigMaps

Store configuration separately from the deployment:

kubectl create configmap my-app-config --from-literal=DATABASE_URL=postgres://... kubectl create secret generic my-app-secrets --from-literal=API_KEY=...

Reference these in your Deployment using environment variables or volume mounts.

Step 7: Set Up Scaling

Horizontal Pod Autoscaler:

apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: my-app spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app minReplicas: 3 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70

Step 8: Monitor

Deploy monitoring (Prometheus + Grafana) to track: - Pod CPU and memory usage - Request latency and error rates - Replica count over time - Resource utilization

Conclusion

Kubernetes provides a powerful platform for running web applications with built-in scaling, self-healing, and rolling updates. The initial setup requires more configuration than a single-server deployment, but the operational benefits for production applications are substantial.

Deploy a Web App on Kubernetes: Step by Step | ServerRaja