Share the love

One option is to use feature flagging with Kubernetes, which allows you to gradually roll out new versions of your application to a small percentage of users before rolling it out to the entire user base. This allows you to test the new version of the application and make sure it is stable before deploying it to all users. Additionally, you can use Kubernetes Deployments and Services to create new replica sets and update the services to point to the new replica set. This way you can keep old version running and test the new version with a small percentage of users before rolling out to all users.

Sample Code

Here is an example of using Kubernetes Deployments and Services to implement blue-green deployment in AKS:

Create two Deployments, one for the “blue” version of the application and one for the “green” version.

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

Create a Service that directs traffic to the “blue” version of the application.

apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  selector:
    app: myapp
    version: blue
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: ClusterIP

Update the Service to direct traffic to the “green” version of the application by changing the selector to match the “green” version.

apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  selector:
    app: myapp
    version: green
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: ClusterIP

In this example, the Deployments and Service are configured to use the labels “version: blue” and “version: green” to determine which version of the application to route traffic to. By updating the Service to match the “green” version, traffic will be directed to the green Deployment. You can use feature flagging to roll out the new version of the application to a small percentage of users before rolling it out to the entire user base.

Automate deployment using Azure DevOps

To automate blue-green deployment for production workloads in AKS, you can use a combination of Azure DevOps, Helm, and Kubernetes Deployment resources.

Here is an example of how you can set up a pipeline in Azure DevOps to automate the process:

  1. Create a new pipeline in Azure DevOps and configure it to trigger on code pushes to your source control repository.
  2. In the pipeline, create a Helm task to package your application into a chart.
  3. Use the Kubernetes Deployment resource to create two replicas of your application, one for the blue environment and one for the green environment.
  4. Use the Kubernetes Deployment resource to configure the replicas to roll out the blue version of the application to the blue environment and the green version of the application to the green environment.
  5. Use the Kubernetes Service resource to create a load balancer that routes traffic to the blue environment.
  6. Use the Kubernetes Deployment resource to configure the replicas to roll out the green version of the application to the green environment and the blue version of the application to the blue environment.
  7. Use the Kubernetes Service resource to switch the load balancer to route traffic to the green environment.
  8. Use Azure Monitor and Log Analytics to monitor and troubleshoot the deployment process.

Sample YAML file

Here is an example of what the pipeline could look like in YAML:

trigger:
- main

jobs:
- job: Deploy
  pool:
    vmImage: ubuntu-latest
  steps:
  - task: HelmInstaller@0
    inputs:
      helmVersion: 2.16.3
  - task: HelmDeploy@0
    inputs:
      command: upgrade
      chartType: filePath
      chartPath: charts/mychart-0.1.0.tgz
      releaseName: mychart
      namespace: mychart
      values: |
        replicaCount: 2
        service:
          type: LoadBalancer
        blueGreen:
          enabled: true
          service:
            name: mychart-blue-green
            type: LoadBalancer