Skip to content

theranaji/nextjs-docker-k8s-deployment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

DevOps Assignment - Next.js with Docker & Kubernetes

A containerized Next.js application with CI/CD pipeline and Kubernetes deployment.

πŸš€ Features

  • Next.js 14 with TypeScript and Tailwind CSS
  • Multi-stage Docker build for optimized production images
  • GitHub Actions CI/CD pipeline with GHCR integration
  • Kubernetes deployment with health checks and auto-scaling
  • Security best practices with non-root user and minimal attack surface

πŸ“‹ Prerequisites

  • Node.js 18+
  • Docker
  • Minikube
  • kubectl
  • Git

πŸ› οΈ Local Development

1. Clone and Setup

git clone <your-repo-url>
cd <repo-name>
npm install

2. Run Development Server

npm run dev

Visit http://localhost:3000

3. Build Production

npm run build
npm start

🐳 Docker Commands

Build Image

docker build -t nextjs-devops-app .

Run Container

docker run -p 3000:3000 nextjs-devops-app

Health Check

docker ps  # Check container health status

βš™οΈ GitHub Actions Setup

  1. Enable GitHub Container Registry:

    • Go to repository Settings β†’ Actions β†’ General
    • Enable "Read and write permissions" for GITHUB_TOKEN
  2. Update Image Reference:

    • Edit k8s/deployment.yaml
    • Replace YOUR_USERNAME with your GitHub username
  3. Push to Main Branch:

    git add .
    git commit -m "Initial commit"
    git push origin main

The workflow will automatically:

  • Build Docker image
  • Push to GitHub Container Registry
  • Tag with branch name and commit SHA

☸️ Kubernetes Deployment

1. Start Minikube

minikube start
minikube addons enable ingress

2. Update Deployment Image

Edit k8s/deployment.yaml and replace the image URL:

image: ghcr.io/YOUR_USERNAME/nextjs-docker-k8s-deployment:latest

3. Deploy to Kubernetes

kubectl apply -f k8s/

4. Verify Deployment

# Check pods
kubectl get pods

# Check services
kubectl get services

# Check deployment status
kubectl rollout status deployment/nextjs-app

5. Access Application

Option 1: NodePort Service

minikube service nextjs-service --url

Option 2: Port Forward

kubectl port-forward service/nextjs-service 8080:80

Visit http://localhost:8080

Option 3: Minikube IP

minikube ip
# Visit http://<minikube-ip>:30080

πŸ” Monitoring & Debugging

View Logs

kubectl logs -l app=nextjs-app

Describe Resources

kubectl describe deployment nextjs-app
kubectl describe service nextjs-service

Scale Application

kubectl scale deployment nextjs-app --replicas=5

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   GitHub Repo   │───▢│  GitHub Actions  │───▢│      GHCR       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                         β”‚
                                                         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Minikube     │◀───│   kubectl apply  │◀───│  K8s Manifests  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Container Registry

After successful GitHub Actions build, your image will be available at:

ghcr.io/YOUR_USERNAME/nextjs-docker-k8s-deployment:latest

πŸ”§ Troubleshooting

Common Issues

  1. Image Pull Error:

    # Make repository public or configure image pull secrets
    kubectl create secret docker-registry ghcr-secret \
      --docker-server=ghcr.io \
      --docker-username=YOUR_USERNAME \
      --docker-password=YOUR_TOKEN
  2. Service Not Accessible:

    # Check if minikube tunnel is running
    minikube tunnel
  3. Pod CrashLoopBackOff:

    kubectl logs <pod-name>
    kubectl describe pod <pod-name>

πŸ“ Assignment Checklist

  • βœ… Next.js application created
  • βœ… Multi-stage Dockerfile with best practices
  • βœ… GitHub Actions workflow for CI/CD
  • βœ… GHCR integration with proper tagging
  • βœ… Kubernetes deployment with replicas
  • βœ… Health checks (liveness & readiness probes)
  • βœ… Service configuration for external access
  • βœ… Comprehensive documentation

🎯 Production Considerations

  • Security: Non-root user, minimal base image
  • Performance: Multi-stage build, standalone output
  • Reliability: Health checks, resource limits
  • Scalability: Horizontal pod autoscaling ready
  • Monitoring: Structured logging, metrics endpoints

Repository URL: https://github.com/YOUR_USERNAME/nextjs-docker-k8s-deployment
GHCR Image URL: ghcr.io/YOUR_USERNAME/nextjs-docker-k8s-deployment:latest# Trigger workflow

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages