Select Page

Robert Boule

|
originally published on May 31, 2024
Share

In today’s fast-paced software development landscape, achieving zero downtime during deployments is crucial. One popular method to achieve this is through Blue/Green deployments, where two identical environments are maintained to ensure a smooth transition between different versions of your application. FluxCD, a GitOps tool for Kubernetes, provides a robust and automated way to implement Blue/Green deployments. This blog post will guide you through the process of using FluxCD to set up and manage Blue/Green deployments.

What is Blue/Green Deployment?

Blue/Green deployment is a technique that reduces downtime and risk by running two identical production environments, referred to as Blue and Green. Only one of the environments (say Blue) is live at any time, serving production traffic. When a new version of the application is ready, it is deployed to the idle environment (Green). After thorough testing, the traffic is switched from Blue to Green, making Green the live environment. If anything goes wrong, it’s easy to revert to the Blue environment.

Prerequisites

Before we dive into the implementation, make sure you have the following set up:

  • A Kubernetes cluster
  • FluxCD installed and configured
  • kubectl CLI configured to interact with your cluster
  • Basic understanding of Kubernetes and GitOps principles

Step-by-Step Guide

Step 1: Install FluxCD

First, ensure FluxCD is installed and configured in your Kubernetes cluster. If you haven’t installed FluxCD yet, you can follow the official installation guide.

Step 2: Set Up Your Git Repository

FluxCD operates on a GitOps model, where your application’s desired state is defined in a Git repository. Create a new repository or use an existing one to store your Kubernetes manifests.

Your repository structure should look something like this:

				
					my-fluxcd-repo/
├── blue/
│   ├── deployment.yaml
│   └── service.yaml
├── green/
│   ├── deployment.yaml
│   └── service.yaml
└── flux/
    ├── kustomization.yaml
    └── gotk-sync.yaml

				
			

Step 3: Define Blue and Green Environments

Create separate folders for Blue and Green environments in your repository. Each folder should contain the necessary Kubernetes manifests (deployments, services, etc.).

Example: blue/deployment.yaml

				
					apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-blue
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
      version: blue
  template:
    metadata:
      labels:
        app: my-app
        version: blue
    spec:
      containers:
        - name: my-app
          image: my-app:latest
          ports:
            - containerPort: 80
				
			

Example: green/deployment.yaml

				
					apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-green
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
      version: green
  template:
    metadata:
      labels:
        app: my-app
        version: green
    spec:
      containers:
        - name: my-app
          image: my-app:latest
          ports:
            - containerPort: 80

				
			

Step 4: Configure FluxCD to Monitor Your Repository

In the flux/ directory, configure FluxCD to sync with your Git repository.

flux/kustomization.yaml

				
					apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 5m0s
  path: "./blue"
  prune: true
  sourceRef:
    kind: GitRepository
    name: my-repo
    namespace: flux-system

				
			

flux/gotk-sync.yaml

				
					apiVersion: source.toolkit.fluxcd.io/v1beta1
kind: GitRepository
metadata:
  name: my-repo
  namespace: flux-system
spec:
  interval: 1m0s
  url: ssh://git@github.com/your-username/my-fluxcd-repo
  ref:
    branch: main
  secretRef:
    name: flux-system
				
			

Commit and push these changes to your repository.

Step 5: Deploy and Test Blue Environment

With FluxCD monitoring your repository, it will automatically apply the manifests from the Blue environment. Verify that the Blue environment is up and running:

				
					kubectl get deployments -l app=my-app,version=blue
				
			

Test your application in the Blue environment to ensure everything is working as expected.

Step 6: Switch to Green Environment

Once you are ready to deploy the new version, update the FluxCD kustomization.yaml to point to the Green environment.

Updated flux/kustomization.yaml

				
					apiVersion: kustomize.toolkit.fluxcd.io/v1beta1
kind: Kustomization
metadata:
  name: my-app
  namespace: flux-system
spec:
  interval: 5m0s
  path: "./green"
  prune: true
  sourceRef:
    kind: GitRepository
    name: my-repo
    namespace: flux-system
				
			

Commit and push this change to your repository. FluxCD will automatically apply the Green environment manifests, creating the Green deployment.

Step 7: Switch Traffic to Green

Update your service to point to the Green deployment. This can be done by modifying the selector in the service manifest.

				
					apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
    version: green
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

				
			

Commit and push this change to your repository. FluxCD will update the service, redirecting traffic to the Green environment.

Step 8: Verify and Clean Up

Verify that your application is running correctly in the Green environment:

				
					kubectl get deployments -l app=my-app,version=green
				
			

If everything works as expected, you can delete the Blue deployment to free up resources:

				
					kubectl delete deployment my-app-blue
				
			

Conclusion

Implementing Blue/Green deployments with FluxCD allows you to achieve zero downtime deployments while minimizing risk. By leveraging FluxCD’s GitOps capabilities, you can automate the deployment process, making it more reliable and repeatable. This guide provides a starting point for setting up Blue/Green deployments with FluxCD, but you can customize and extend it to fit your specific needs and workflows.

Happy deploying!

About OpsMx

OpsMx is a leading innovator and thought leader in the Continuous Delivery space. OpsMx’s Solution for Flux helps DevOps teams manage their Flux deployments at scale.

Talk to OpsMx’s FluxCD experts about any questions that you may have around Flux, GitOps-style deployments, Kubernetes, or DevSecOps. Our services enable the largest and most innovative companies to optimize their (GitOps) delivery pipelines.

Tags :

Robert Boule

Robert Boule is a dynamic technology enthusiast... Not just doing this for a living, but have a PASSION for technology and making things work along with a knack for helping other understand how things work!

Link

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.