Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide

In modern DevOps practices, continuous delivery and zero-downtime deployments are critical for ensuring high availability and a seamless user experience. One of the best approaches to achieve this is through Blue-Green Deployments, a deployment strateg…


This content originally appeared on DEV Community and was authored by mark mwendia

In modern DevOps practices, continuous delivery and zero-downtime deployments are critical for ensuring high availability and a seamless user experience. One of the best approaches to achieve this is through Blue-Green Deployments, a deployment strategy that reduces downtime and the risk associated with deploying new features. With tools like Argo CD and Helm, automating blue-green deployments becomes more efficient, reliable, and scalable.

This guide will walk you through the process of implementing blue-green deployments using Argo CD, a declarative GitOps continuous delivery tool, and Helm, a package manager for Kubernetes.

Why Blue-Green Deployments?
Blue-green deployment is a strategy where two environments—one labeled "blue" and the other "green"—are maintained. While one environment (blue) serves live traffic, the other (green) is prepared for the new release. After verifying the green environment works correctly, traffic is shifted to it, and the previous (blue) environment can be retained as a fallback in case issues arise. The result is a zero-downtime deployment, reducing the risks of releasing new changes.

Prerequisites
Before we dive into the implementation, ensure you have the following in place:

  • Basic knowledge of Kubernetes and experience with Helm and Argo CD.

  • A working Kubernetes cluster (minikube, GKE, or any cloud-based solution).

  • Argo CD installed and configured in your Kubernetes cluster.

  • Helm installed in your local development environment.

  • A Git repository for storing the Helm charts and Argo CD manifests.

For this tutorial, we'll use a simple example where we deploy a sample application using a Helm chart and manage the deployment with Argo CD.

Step 1: Setting Up Argo CD in Your Cluster

First, let's install Argo CD in your Kubernetes cluster. Argo CD allows you to declaratively manage your Kubernetes applications by syncing your cluster state with the desired state defined in Git.

  1. Install Argo CD ```bash

kubectl create namespace argocd
kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml

Wait for all Argo CD components to be deployed:

```bash


kubectl get pods -n argocd


  1. Access the Argo CD UI To access the Argo CD UI, expose the Argo CD server:


kubectl port-forward svc/argocd-server -n argocd 8080:443


Visit https://localhost:8080 in your browser, and log in using the default admin credentials:



kubectl get secret argocd-initial-admin-secret -n argocd -o jsonpath="{.data.password}" | base64 -d


Step 2: Create a Helm Chart for Your Application

Now, let's create a simple Helm chart for a demo application.

Create the Helm Chart



helm create my-app


This command generates a basic Helm chart structure. You can modify the generated files to suit your application. In the values.yaml file, define two sets of replicas—one for the blue environment and another for the green environment.

Example values.yaml



replicaCount: 1

image:
  repository: nginx
  tag: "1.19"
  pullPolicy: IfNotPresent

service:
  type: ClusterIP
  port: 80

environment: blue

ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
  hosts:
    - host: my-app.local
      paths:
        - /


In this example, we've specified the environment as blue. We'll switch it to green during the deployment process.

Step 3: Create Argo CD Application

Next, we need to configure Argo CD to manage this Helm chart and perform the blue-green deployment.

Create Argo CD Application Manifest
Create a manifest file to define your Argo CD application. This will tell Argo CD where to find your Helm chart and how to deploy it.



apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: my-app
  namespace: argocd
spec:
  project: default
  source:
    repoURL: 'https://github.com/your-username/my-helm-charts'
    targetRevision: HEAD
    path: my-app
    helm:
      valueFiles:
        - values.yaml
  destination:
    server: 'https://kubernetes.default.svc'
    namespace: default
  syncPolicy:
    automated:
      prune: true
      selfHeal: true


This manifest defines the source of the Helm chart and ensures that Argo CD will automatically sync the state of the application to match what is defined in Git.

Apply the manifest:



kubectl apply -f my-app-argo.yaml


You can now view your application in the Argo CD UI.

Step 4: Implement the Blue-Green Deployment Logic

The blue-green deployment logic will allow us to switch between the blue and green environments easily.

Modify Helm Template for Blue-Green
In the deployment.yaml file inside your Helm chart, modify the deployment spec to use the environment variable from the values.yaml file:



apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "my-app.fullname" . }}-{{ .Values.environment }}
  labels:
    app: {{ include "my-app.name" . }}
    environment: {{ .Values.environment }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ include "my-app.name" . }}
      environment: {{ .Values.environment }}
  template:
    metadata:
      labels:
        app: {{ include "my-app.name" . }}
        environment: {{ .Values.environment }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          ports:
            - containerPort: 80


In this configuration, the deployment name and labels change depending on whether the environment is blue or green.

Step 5: Switching Between Blue and Green Environments

To implement blue-green switching, update the values.yaml file to change the environment:

  • For the blue environment: ```yaml

environment: blue

* For the green environment:
```yaml


environment: green


You can manually switch environments by updating the values file and syncing the changes in Argo CD. Alternatively, automate the process through Argo CD hooks or custom scripts.

Example Argo CD Sync Command
To sync changes after modifying the environment to green:



argocd app sync my-app


This will deploy the green environment, and you can switch traffic to it once verified.

Step 6: Testing and Validation

Once the green environment is deployed, you can test the application’s functionality. If everything works as expected, update the ingress to point to the green environment. If issues occur, you can roll back to the blue environment by syncing the blue configuration.

Conclusion

Implementing blue-green deployments with Argo CD and Helm provides a powerful mechanism for zero-downtime deployments. By leveraging GitOps practices with Argo CD and Helm's templating system, you can manage complex Kubernetes deployments while minimizing risk and downtime. The flexibility of this setup allows you to easily switch between environments, ensuring smooth rollouts and the ability to quickly recover from any issues.

Key Takeaways:

  • Argo CD and Helm enable automation of blue-green deployments with Kubernetes, allowing for seamless environment switching.

  • By managing your deployments declaratively through Git, you maintain full control of the deployment process and minimize manual interventions.

  • Blue-green deployments significantly reduce the risk of errors and downtime, making them ideal for mission-critical applications.


This content originally appeared on DEV Community and was authored by mark mwendia


Print Share Comment Cite Upload Translate Updates
APA

mark mwendia | Sciencx (2024-10-06T18:30:48+00:00) Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide. Retrieved from https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/

MLA
" » Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide." mark mwendia | Sciencx - Sunday October 6, 2024, https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/
HARVARD
mark mwendia | Sciencx Sunday October 6, 2024 » Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide., viewed ,<https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/>
VANCOUVER
mark mwendia | Sciencx - » Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/
CHICAGO
" » Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide." mark mwendia | Sciencx - Accessed . https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/
IEEE
" » Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide." mark mwendia | Sciencx [Online]. Available: https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/. [Accessed: ]
rf:citation
» Implementing Blue-Green Deployments with Argo CD and Helm: A Practical Guide | mark mwendia | Sciencx | https://www.scien.cx/2024/10/06/implementing-blue-green-deployments-with-argo-cd-and-helm-a-practical-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.