Deploying a Netflix Clone on AWS using Kubernetes and Minikube: A Step-by-Step Guide

Kanav GatheKanav Gathe
3 min read

In this tutorial, we'll walk through the process of deploying a Netflix clone application using Kubernetes (k8s) and Minikube on AWS. We'll cover everything from setting up the development environment to deploying the application using Kubernetes configurations.

Prerequisites

  • AWS account

  • Basic understanding of Docker and Kubernetes

  • Git installed on your local machine

  • Docker Hub account

Step 1: Local Setup and Docker Image Creation

First, clone the Netflix Clone repository and create a Docker image:

# Clone the repository
git clone https://github.com/devandres-tech/Netflix-Clone.git
cd Netflix-Clone

Create a Dockerfile in the root directory with the following content:

# Build stage
FROM node:18-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm install --legacy-peer-deps
COPY . .
RUN npm run build:prod

# Production stage
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Build and push the Docker image:

docker build -t slayerop15/netflix-clone .
docker push slayerop15/netflix-clone

Step 2: AWS EC2 Instance Setup

  1. Launch a t2.medium EC2 instance with Ubuntu

  2. Configure security groups to allow inbound traffic on ports 22 (SSH) and 80 (HTTP)

  3. Connect to your instance:

ssh -i your-key.pem ubuntu@your-instance-ip

Step 3: Installing Required Tools

Install Docker, kubectl, and Minikube:

# Clone the installation scripts
git clone https://github.com/SlayerK15/Scripts.git
cd Scripts
chmod 777 *.sh

# Run the setup script to install required tools
sudo ./setup.sh docker minikube kubernetes

# Add user to docker group
sudo usermod -aG docker $USER
newgrp docker

# Start Minikube
minikube start

Step 4: Creating Kubernetes Configuration Files

Create a new directory for your Kubernetes configurations:

mkdir netflix-deployment
cd netflix-deployment
mkdir k8s
cd k8s

Create the following Kubernetes configuration files:

namespace.yaml:

apiVersion: v1
kind: Namespace
metadata:
  name: netflix-clone

configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: netflix-clone-config
  namespace: netflix-clone
data:
  API_KEY: "your-api-key"

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: netflix-clone
  namespace: netflix-clone
spec:
  replicas: 1
  selector:
    matchLabels:
      app: netflix-clone
  template:
    metadata:
      labels:
        app: netflix-clone
    spec:
      containers:
      - name: netflix-clone
        image: slayerop15/netflix-clone:latest
        ports:
        - containerPort: 80
        envFrom:
        - configMapRef:
            name: netflix-clone-config

service.yaml:

apiVersion: v1
kind: Service
metadata:
  name: netflix-clone-service
  namespace: netflix-clone
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: netflix-clone

ingress.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: netflix-clone-ingress
  namespace: netflix-clone
spec:
  rules:
  - host: netflix.example.com  # Replace with your domain
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: netflix-clone-service
            port:
              number: 80

Step 5: Deploying the Application

Apply the Kubernetes configurations:

# Apply configurations
kubectl apply -f namespace.yaml
kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml

# Verify deployment
kubectl get all -n netflix-clone

# Port forward to access the application
kubectl port-forward --address 0.0.0.0 service/netflix-clone-service 3000:80 -n netflix-clone

Verifying the Deployment

To verify that everything is working correctly:

  1. Check pod status:
kubectl get pods -n netflix-clone
  1. Check service status:
kubectl get services -n netflix-clone
  1. Access the application:

Troubleshooting

If you encounter any issues:

  1. Check pod logs:
kubectl logs <pod-name> -n netflix-clone
  1. Describe the pod for more details:
kubectl describe pod <pod-name> -n netflix-clone
  1. Common issues:
  • Image pull errors: Verify Docker Hub credentials

  • Pod pending: Check node resources

  • Service unreachable: Verify security group settings

Conclusion

You've successfully deployed a Netflix clone application on AWS using Kubernetes and Minikube. This setup provides a scalable and maintainable infrastructure for your application. Remember to clean up your AWS resources when you're done to avoid unnecessary charges.

0
Subscribe to my newsletter

Read articles from Kanav Gathe directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kanav Gathe
Kanav Gathe