Blue-Green Deployment with Spring Boot : Strategy

When deploying Spring Boot applications in production, downtime or buggy rollouts can damage user trust. Blue-Green Deployment is a proven strategy that allows for zero-downtime deployments, quick rollback, and smooth version transitions.

In this guide, youโ€™ll learn:

  • What Blue-Green deployment is
  • How to implement it for Spring Boot using Kubernetes, NGINX, or cloud load balancers
  • Full YAML and CI/CD integration examples
  • Best practices for observability and rollback

All examples use com.kscodes.springboot.containers as the base package.

Blue-Green Deployment with Spring Boot

๐Ÿ”„ What is Blue-Green Deployment?

Blue-Green deployment maintains two environments:

  • Blue โ€“ current live (production) environment
  • Green โ€“ new version (inactive until ready)

You:

  1. Deploy new version to Green
  2. Run tests, health checks
  3. Switch traffic from Blue โ†’ Green
  4. Keep Blue available for rollback

๐Ÿงฐ Prerequisites

  • Dockerized Spring Boot app
  • Kubernetes cluster (Minikube, GKE, etc.)
  • Ingress controller (or NGINX)
  • CI/CD pipeline (optional)
  • Helm (optional)

๐Ÿ“ฆ Step 1: Spring Boot Sample App

Weโ€™ll deploy two versions of the app with minor changes.

Version 1 (Blue):


@GetMapping("/")
public String home() {
    return "Welcome to the BLUE version!";
}

Version 2 (Green):


@GetMapping("/")
public String home() {
    return "Welcome to the GREEN version!";
}

Build and push Docker images:


docker build -t kscodes/springboot-blue:1.0 .
docker push kscodes/springboot-blue:1.0

docker build -t kscodes/springboot-green:2.0 .
docker push kscodes/springboot-green:2.0

๐Ÿงพ Step 2: Kubernetes Deployments

Blue Deployment


apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-blue
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot
      version: blue
  template:
    metadata:
      labels:
        app: springboot
        version: blue
    spec:
      containers:
        - name: springboot
          image: kscodes/springboot-blue:1.0
          ports:
            - containerPort: 8080

Green Deployment


apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot-green
spec:
  replicas: 2
  selector:
    matchLabels:
      app: springboot
      version: green
  template:
    metadata:
      labels:
        app: springboot
        version: green
    spec:
      containers:
        - name: springboot
          image: kscodes/springboot-green:2.0
          ports:
            - containerPort: 8080

๐ŸŒ Step 3: Dynamic Routing with Ingress

Use an Ingress controller (e.g., NGINX) to switch routing based on label:


apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: springboot-ingress
spec:
  rules:
    - host: springboot.kscodes.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: springboot-service-green
                port:
                  number: 80

When ready to switch:

  • Change serviceName from springboot-service-blue to springboot-service-green

You can automate this in your CI/CD pipeline.

โš™๏ธ Step 4: Create Services

Create separate Services for both:


# blue-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: springboot-service-blue
spec:
  selector:
    app: springboot
    version: blue
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080


# green-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: springboot-service-green
spec:
  selector:
    app: springboot
    version: green
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

๐Ÿงช Step 5: Validate Green Before Switching

Test with:


kubectl port-forward svc/springboot-service-green 8080:80
curl http://localhost:8080

Ensure logs, metrics, and endpoints respond correctly.

๐Ÿ” Step 6: Switch Traffic

Update the Ingress YAML or route controller to point to springboot-service-green.

Rollback? Just switch back to springboot-service-blue.

๐Ÿš€ Optional: Automate with GitOps / CI/CD

You can integrate Blue-Green deployment in:

  • GitHub Actions
  • Argo CD
  • Jenkins pipelines

Sample GitHub Action:


- name: Switch Ingress to Green
  run: |
    kubectl apply -f ingress-green.yaml

โœ… Benefits Recap

BenefitExplanation
Zero downtimeNo impact to users during deployment
Easy rollbackJust point traffic back to Blue
Test in productionValidate real traffic on Green before switching
Better release safetyIdeal for mission-critical apps

๐Ÿ“˜ Summary

In this guide, you implemented the strategy of Blue-Green Deployment with Spring Boot using Kubernetes and NGINX. This method ensures zero downtime, allows quick rollbacks, and improves release confidence. With YAML, labels, and traffic switching, you can easily automate your deployment process.

In the next post, weโ€™ll automate Spring Boot deployments using Jenkins CI/CD pipelines.