DEV Community

Deep Datta
Deep Datta

Posted on

Building a Production-Ready 3-Tier Application with Kubernetes Blue-Green Deployment

Introduction

I took a basic 3-tier Node.js and MySQL application and transformed it into a production-ready system using DevOps practices. The goal was to implement a zero-downtime deployment strategy using Kubernetes' blue-green deployment pattern, containerize the application, automate the CI/CD pipeline with Jenkins, and provision infrastructure with Terraform.


Understanding the Architecture

The 3-Tier Application

The application consists of three layers:

  1. Frontend (React): A user management interface.
  2. Backend (Node.js/Express): An API server handling CRUD operations.
  3. Database (MySQL): Persistent data storage.

Step 1: Containerizing with Docker

I used a multi-stage Dockerfile to keep the final image lightweight and efficient.

FROM node:14-alpine

WORKDIR /usr/src/app/client
COPY client/package*.json ./
RUN npm install
COPY client/ ./
RUN npm run build

WORKDIR /usr/src/app/server
COPY server/package*.json ./
RUN npm install
COPY server/ ./
RUN mkdir -p ./public && cp -R /usr/src/app/client/dist/* ./public/

EXPOSE 5000
CMD ["npm", "start"]
Enter fullscreen mode Exit fullscreen mode

Step 2: Orchestrating with Kubernetes

Key Resources

I created two identical deployments—blue and green. The selector field allows us to switch traffic.

apiVersion: v1
kind: Service
metadata:
  name: app
spec:
  ports:
  - port: 80
    targetPort: 5000
  selector:
    app: app
    version: blue  # Points to the active environment
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

Step 3: Blue-Green Deployment Strategy

The Jenkinsfile contains the logic for switching traffic by patching the service:

stage('Switch Traffic') {
    when { expression { return params.SWITCH_TRAFFIC } }
    steps {
        script {
            def newEnv = params.DEPLOY_ENV
            withKubeConfig(...) {
                sh "kubectl patch service app -p '{\"spec\":{\"selector\":{\"version\":\"${newEnv}\"}}}' --type merge -n ${KUBE_NAMESPACE}"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Troubleshooting: Real Issues I Encountered

To keep this guide readable, I've categorized the hurdles I faced. Click to expand each issue.

Issue 1: Docker Build Failures (Node Cache)
Problem: The build stage would fail intermittently with npm dependency resolution errors.
Solution: Added explicit cache busting using --no-cache and a .dockerignore file.

Issue 2: Application Pod Can't Connect to MySQL
Problem: App pod crashed because MySQL wasn't ready.
Solution: Added a retry loop in the Node.js connection logic and used a Kubernetes Headless Service for stable DNS.


Here are some screenshots from the Jenkins build pipeline


The application is deployed in the blue environment (above image)


The application is now deployed in the green environment (above 2 images)

User Management App

This is a full-stack application for managing users with a front-end built using HTML, CSS, and JavaScript, and a back-end powered by Node.js, Express, and MySQL.

Table of Contents

Features

  • Add new users with a name, email, and role (User/Admin).
  • View a list of all users.
  • Edit user details.
  • Delete users.
  • Responsive and user-friendly UI.
  • Smooth animations and minimalistic design.

Prerequisites

Before setting up this project, ensure you have the following installed on your machine:

  • Node.js (version 12.x or higher)
  • MySQL (version 5.7 or higher)

Setup Instructions

1. Setting Up MySQL Server

First, you need to set up a MySQL server on your local machine.

  1. Update the package index:

    sudo apt update
    Enter fullscreen mode Exit fullscreen mode
  2. Install the MySQL server:

    sudo apt install mysql-server
    Enter fullscreen mode Exit fullscreen mode
  3. Log in to the





Top comments (0)