Learn KOP - Blue-Green Deployment Pattern - Rafay Product Documentation

Blue-Green

This is Part 4 of a multi-part, self-paced quick start exercise.

What Will You Do

In this exercise, you will perform a basic "blue-green" deployment pattern using GitOps.

Background

Note that a Blue-Green (sometimes also referred to as red/black) deployment strategy type is not natively included in the ".spec.strategy.type" of Kubernetes. However, it can be realized without having to install any specialized controllers.

We will create two Deployments with different versions on the application. Both deployments will be using a Service with the same set of labels in its selector.

Advantages

Disadvantages

Estimated Time

Estimated time burden for this part is 5 minutes.

Step 1: Baseline

This assumes that you have already completed at least "Part-1" and have an operational GitOps pipeline syncing artifacts from your Git repository.

Note that we have "two deployments". The first one called "echo-blue" is active and has two replicas. The second one is called "echo-green" and has zero replicas.

apiVersion: v1
kind: Service
metadata:
  name: echo
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 5678
    protocol: TCP
  selector:
    app: echo
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-blue
spec:
  selector:
    matchLabels:
      app: echo
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 1
  replicas: 2
  template:
    metadata:
      labels:
        app: echo
        version: "1.0"
    spec:
      containers:
      - name: echo
        image: hashicorp/http-echo
        args:
        - "-text=Blue"
        ports:
        - containerPort: 5678
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: echo-green
spec:
  selector:
    matchLabels:
      app: echo
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 1
  replicas: 0
  template:
    metadata:
      labels:
        app: echo
        version: "1.0"
    spec:
      containers:
      - name: echo
        image: hashicorp/http-echo
        args:
        - "-text=Green"
        ports:
        - containerPort: 5678

Once the GitOps pipeline has reconciled, you should see something like the following via the zero trust kubectl

kubectl get po -n echo

NAME                         READY   STATUS    RESTARTS   AGE
echo-blue-558b67ffd6-5r7xx   1/1     Running   0          17h
echo-blue-558b67ffd6-qld8r   1/1     Running   0          17h

Step 2: Setup

To test the switch from Blue -> Green, we will execute cURL against the configured IP of the Service every second.

while true; do sleep 1; curl http://<External-IP from above>;done

You should see something like the following:

Blue
Blue
Blue....

At this point all traffic is being routed to the "Blue" replicas

Step 3: Switch to Green

Step 4: View Behavior

The Git commit will trigger the GitOps pipeline. It will automatically update the workload on the cluster switching the "Blue" deployment with the "Green" deployment. You should start seeing responses from the Green replicas "Green" like the following in a few minutes with zero downtime.

Blue
Blue
Blue
Blue
Blue
Green
Green

If you check the pods via the zero trust kubectl, you should see something like the following:

kubectl get po -n echo

NAME                          READY   STATUS        RESTARTS   AGE
echo-blue-558b67ffd6-5r7xx    1/1     Terminating   0          18h
echo-blue-558b67ffd6-qld8r    1/1     Terminating   0          18h
echo-green-7fbf46c447-ndg7r   1/1     Running       0          28s
echo-green-7fbf46c447-w5cx9   1/1     Running       0          28s

At this point, all traffic has been seamlessly switched and being routed to the "Green" replicas

Recap

In this part, you tested and experienced how a "Blue-Green" deployment pattern/strategy can be implemented easily on Kubernetes.