How to Set Up a Blue-Green Deployment Methodology with AppFog
This article was sponsored by AppFog. Thank you for supporting the sponsors who make SitePoint possible.
There are several steps involved in building a large scale web application. One of the most notoriously difficult steps of that process is deployment. Your engineering team will often face situations where code needs to be shipped quickly, and your production environment needs to update as soon as possible. In such an environment, the deployment process must be smooth enough to avoid disrupting user experience, but rapid enough to avoid downtime and preserve business goals.
The team at AppFog understands how important it is to have a strong architecture to support maintaining and scaling out your application, which is why they’ve built a tool that allows you to focus on writing code, while they handle the infrastructure related details. With AppFog, things like run time environments, load balancing, and reporting are no longer issues that require so much of your time. AppFog can even help you deploy your application seamlessly, regardless of how many users you have, or how big your deployments usually are.
If you’ve ever found yourself struggling to provide your engineering team with an effortless deployment strategy, then a Blue-Green deployment methodology provided by AppFog may be right for you and your team.
What is the Blue-Green Deployment Methodology?
A known challenge involved in automated deployment is often referred to as “the cutover”, which refers to taking software from the final stage of testing to live production. The cutover needs to happen quickly, so that downtime is prevented or at least minimized. A Blue-Green deployment strategy allows for safe deployments by ensuring that your application has two separate production environments, as close to identical as possible. At any time, one of these environments is live, while the other receives no user traffic. If your ‘Blue’ environment is live, then your pre-deployment testing will take place on your ‘Green’ environment. Once your tests pass on the Green environment, you reroute your traffic from the Blue environment to the Green environment which now has the updated code that has passed all QA and other testing.
The Blue-Green deployment strategy allows for immediate rollbacks in case an error is found in the live environment, since you can simply reroute your traffic to whichever of your two identical production environments is inactive. You can read more about the Blue-Green deployment methodology here.
How to Get Started with AppFog
As outlined in our previous article covering AppFog and the 12 Factor App Methodology, AppFog is a product for managing network resources, middleware, operating systems, virtualization, servers, storage, and the runtime. All you need to do is focus on writing your code. AppFog is built by CenturyLinkand relies on the versatile open source Cloud Foundry platform.
To enable AppFog in your CenturyLink control panel, Go to https://control.ctl.io, login, and you will see “AppFog” listed in the drop down navigation bar:
Then, enable deployment to AppFog regions, US East and/or US West. Each region has its own “add region” button to enable it in your AppFog:
Once you have AppFog enabled on your account you can manage membership to regions and spaces, login from the terminal, and deploy an application.
Using the Cloud Foundry CLI, deploying is as easy as navigating to the directory where your source code is located and running:
cf push [name of app]
Updating that application while following a Blue-Green deployment strategy requires a few extra steps.
After the initial deployment of your application, an update to your application can be deployed by running the command:
cf push myapp-Green -n myapp-temp
This command tells Cloud Foundry to push your code to a temporary route rather than your main route, and assuming that your application was in AppFog’s east region, the URL to access this temporary path would be:
So far, we have a Blue environment, which is the application that is currently active and has not been updated, and a Green environment, which is the temporary environment that has just been deployed.
Now that the two environments exist, we can put the temporary environment into rotation, which means it will be automatically load balanced by AppFog, so that traffic will be delivered to one of the two environments.
To do this, we would run:
cf map-route myapp-Green useast.appfog.ctl.io -n myapp
Once the new updated version of the application is behaving as expected, we can divert all traffic to that version, which in Blue-Green deployment terms, would mean diverting traffic from the Blue environment, to the recently updated Green environment:
cf unmap-route myapp-Blue useast.appfog.ctl.io -n myapp.
After the Blue-Green switch has been made, we can remove the temporary route by running:
cf unmap-route myapp-Green useast.appfog.ctl.io -n myapp-temp
To see more on Blue-Green deployments with AppFog, see their documentation. In particular, read up on the considerations that should be made when working with data services or services that provide state — this is a complex issue that should approached with care.
While there are plenty of challenges involved in running a successful large scale web application, deployment does not need to be one of them. A successful Blue-Green deployment methodology can make the difference between a successful product with zero downtime and one with issues. AppFog’s tools will get you to a Blue-Green deployment in just four commands, so you can get back to building an amazing product.