Blue-Green Deployment
Blue-Green deployment is a technique to reduce app downtime and risk during deployment time.
Before going to deep dive let’s stick to the basic fundamental step i.e at any point in time blue app is live and the green app is a newly deployed beta app.
With the Blue-Green deployment technique, you can deploy the newly developed code as a green app in the prod environment, then configure the beta app user's traffic to it and monitor the app performance and behavior. Once we got confidence and found no issues we can make the green app live.
Let’s see in the worst case after mapping live user's traffic to green app, unknown errors are occurring then we can immediately flip the live traffic to the blue app. By this way can reduce the app downtime and risk.
Note: If your app uses a relational database, blue-green deployment can lead to discrepancies between your Green and Blue databases during an update. To maximize data integrity, configure a single database for backward and forward compatibility.
Let’s see the step by step implementation of the blue-green deployment technique in PCF.
1. Deploy an App
Use cloud foundry CLI to deploy the app. In our case the app name is App-Blue and the subdomain is “demo”.
cf push App-Blue -d example.com -n demo
once the app is deployed successfully, then the CF Router sends all traffic demo.example.com
to Blue. Here example.com
is the domain name.
2. Deploy a Green App
Now deploy the green app which has the latest code changes, but with a different route than the blue app route.
cf push AppGreen -d example.com -n demo-temp
Now the green app got deployed with route “demo-temp.example.com”.
3. Configure Beta users traffic to the green app
Configure API gateway or the service which has control over beta user’s traffic routing to the green app. By doing this, the green app will get traffic and we can monitor the app behavior.
4. Flip Green app to Blue app
Once we gained confidence over green app stability, now point live traffic route to the green app. Here is the magic comes into the picture.
We can do this by following the below commands.
cf map-route App-Green -d example.com -n demo
After the cf map-route
command :
- The CF Router continues sending traffic for
demo-temp.example.com
to Green. - Within a few seconds, the CF Router begins load balancing traffic for
demo.example.com
between Blue and Green.
4.1 Unmap Route to Blue
Once you verify Green is running as expected, stop routing requests to Blue using the cf unmap-route command:
cf unmap-route App-Blue -d example.com -n demo
Now the green app is serving both live users and beta users and the blue app is idle as there is no route configured to it.
4.2 Remove Temporary Route to Green
Remove the route demo-temp.example.com
from Green. The route can be deleted using cf delete-route
or reserved for later use. You can also decommission Blue, or keep it in case you need to roll back your changes.
cf unmap-route App-Green example.com -hostname demo-temp
cf delete-route App-Green example.com --hostname demo-temp
4.3 Take the Blue app backup
Take the backup of the blue app so that in the worst case we can simply roll back to the earlier release code app simply by re-routing. We can do this by simply renaming the blue app.
cf rename App-Blue App-backup
cf stop App-backup
Here we are stopping the backup app as it’s has no route configured and not serving any requests, this will save cloud resources.
4.4 Rename Green app to Blue app
As the green app is serving live users and it’s stable, we are sticking to our ground rule i.e blue app serves live user traffic.
cf rename App-Green App-Blue
Note: There can a possibility of small downtime during this green to the blue flipping process, but it can be negligible as it takes only 10 sec for the entire process to complete. For the safer side, we can turn on the maintenance page.