DEV Community

YADNESH RAUT
YADNESH RAUT

Posted on

Containerizing & Orchestrating a Full-Stack E-commerce Platform with Docker and Kubernetes

The diagram below provides a visual representation of the services used in this setup to deploy the EasyShop application. This project is hosted on a local Virtual machine, which serves as the base infrastructure. We utilize Docker to build application images and to run Kind (Kubernetes in Docker), which creates a lightweight Kubernetes cluster within the Local VM.

The application images are stored in Docker Hub. Within the Kind cluster, external traffic is managed by the NGINX Ingress Controller, which routes requests to the EasyShop application pods. The application then connects to a MongoDB StatefulSet for data persistence.

High level Architecture

What Do We Need?

  • Local VM (Ubuntu x64): A virtual machine running Ubuntu x64 acts as the primary infrastructure. It serves as the host for deploying the cluster and running all necessary Docker commands.
  • Git & GitHub: Git is used for version control to manage the codebase locally, while GitHub serves as the remote repository to host the source code of the EasyShop application.
  • Docker: Used to containerize the application by building Docker Images from the Dockerfile. It is also required to run the Kind nodes, which operate as Docker containers.
  • Docker Hub: The container registry used to store and share the built images (Application and Migration images), making them accessible to the cluster.
  • Kind (Kubernetes in Docker): A tool for running local Kubernetes clusters using Docker container "nodes". It simulates the production environment inside your local VM.

Prerequisites Installation:

Linux:

Install Docker

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
rm get-docker.sh
Enter fullscreen mode Exit fullscreen mode

Install Kind

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.27.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
Enter fullscreen mode Exit fullscreen mode

Install kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Enter fullscreen mode Exit fullscreen mode

Environment Setup

  1. Clone the repository and navigate to the project directory
git clone https://github.com/YadneshR/Easy_Shop_Containerization_and_Orchestration.git
cd EasyShop
Enter fullscreen mode Exit fullscreen mode
  1. Build and Push Docker Images

First, login to Docker Hub (create an account at hub.docker.com if you haven't :

docker login
Enter fullscreen mode Exit fullscreen mode

Build Application Image

# Build the application image
docker build -t your-dockerhub-username/easyshop:latest .
Enter fullscreen mode Exit fullscreen mode

Docker build stage

Build successfully

# Push to Docker Hub
docker push your-dockerhub-username/easyshop:latest
Enter fullscreen mode Exit fullscreen mode

Docker push

Build Migration Image

# Build the migration image
docker build -t your-dockerhub-username/easyshop-migration:latest -f Dockerfile.migration /scripts .
Enter fullscreen mode Exit fullscreen mode

Migration build

# Push to Docker Hub
docker push your-dockerhub-username/easyshop-migration:latest
Enter fullscreen mode Exit fullscreen mode

Migration push

Kind Cluster Setup

Create new cluster

kind create cluster --name easyshop --config kubernetes/00-kind-config.yaml
Enter fullscreen mode Exit fullscreen mode

This command creates a new Kind cluster using our custom configuration with one control plane.

cluster creation

Create namespace

kubectl apply -f kubernetes/01-namespace.yaml
Enter fullscreen mode Exit fullscreen mode

Setup storage

kubectl apply -f kubernetes/02-mongodb-pv.yaml
kubectl apply -f kubernetes/03-mongodb-pvc.yaml
Enter fullscreen mode Exit fullscreen mode

Create ConfigMap

Create kubernetes/04-configmap.yaml with the following content:

apiVersion: v1
kind: ConfigMap
metadata:
  name: easyshop-config
  namespace: easyshop
data:
  MONGODB_URI: "mongodb://mongodb-service:27017/easyshop"
  NODE_ENV: "production"
  NEXT_PUBLIC_API_URL: "http://easyshop.local/api"  # Add custom local  domain or EC2 ip 
  NEXTAUTH_URL: "http://easyshop.local"             # Add custom local domain or EC2 ip
  NEXTAUTH_SECRET: "Generate your own NEXTAUTH secret"
  JWT_SECRET: "Generate your own JWT secret"
Enter fullscreen mode Exit fullscreen mode

ADD CUSTOM DOMAIN IF ON LOCAL VM

vim /etc/hosts

Enter fullscreen mode Exit fullscreen mode

Add this line at the bottom in the file

127.0.0.1 easyshop.local

Enter fullscreen mode Exit fullscreen mode

custom local domain
Verify :

ping easyshop.local

Enter fullscreen mode Exit fullscreen mode

Ping

To generate secure secret keys, use these commands in your terminal:

# For NEXTAUTH_SECRET
openssl rand -base64 32

# For JWT_SECRET
openssl rand -hex 32
Enter fullscreen mode Exit fullscreen mode

After making changes in configmap.yml

   kubectl apply -f kubernetes/04-configmap.yaml
Enter fullscreen mode Exit fullscreen mode

Setup configuration

kubectl apply -f kubernetes/05-secrets.yaml
Enter fullscreen mode Exit fullscreen mode

Deploy MongoDB

kubectl apply -f kubernetes/06-mongodb-service.yaml
kubectl apply -f kubernetes/07-mongodb-statefulset.yaml
Enter fullscreen mode Exit fullscreen mode

Deploy EasyShop

Make necessary changes such as:

  • Change image name
kubectl apply -f kubernetes/08-easyshop-deployment.yaml
Enter fullscreen mode Exit fullscreen mode
kubectl apply -f kubernetes/09-easyshop-service.yaml
Enter fullscreen mode Exit fullscreen mode

Install NGINX Ingress Controller

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Deploy Ingress and HPA

Make necessary changes such as:

  • Change the host to http://easyshop.local
   kubectl apply -f kubernetes/10-ingress.yaml
   kubectl apply -f kubernetes/11-hpa.yaml
Enter fullscreen mode Exit fullscreen mode

HPA

Update Migration Job with your docker image :

apiVersion: batch/v1
kind: Job
metadata:
  name: db-migration
  namespace: easyshop
spec:
  template:
    spec:
      containers:
      - name: migration
        image: your_username/easyshop-migration:latest  # update with the name that you have build.
        imagePullPolicy: Always
        env:
        - name: MONGODB_URI
          value: "mongodb://mongodb-service:27017/easyshop"
      restartPolicy: OnFailure
Enter fullscreen mode Exit fullscreen mode
   # Run database migration
   kubectl apply -f kubernetes/migration-job.yaml
Enter fullscreen mode Exit fullscreen mode

JOB
Verify it on your machine browser

http://easyshop.local
Enter fullscreen mode Exit fullscreen mode

Final

Top comments (0)