Switch Traffic Between Blue-Green Deployments
Blue-green deployments make your release processes safer and less prone to error by using an API gateway to switch production traffic between a live environment (blue
) and a new version (green
).
You can route specific traffic to the green environment for final tests, and once you make the switch to green, you can roll back to blue if you find issues.
ngrok makes blue-green deployments easier by helping you:
- Specify in your which environments are currently live and testing, and switch between them freely for each new blue-green deployment.
- Allow requests with specific headers to access your testing environment.
- Handle both and .
In this example, the blue
deployment is your live deployment, while green
is where you're testing new features.
You can also add an x-deployment-version
header to requests to route requests to the testing deployment manually.
1. Create endpoints for your services
Start internal Agent Endpoints, replacing $PORT
based on where your service listens, for each of your services on the systems where they run.
You can also use one of our SDKs or the Kubernetes Operator.
Loading…
2. Reserve a domain
Navigate to the Domains section of the ngrok dashboard and click New + to reserve a free static domain like https://your-service.ngrok.app
or a custom domain you already own.
We'll refer to this domain as $NGROK_DOMAIN
from here on out.
3. Create a Cloud Endpoint
Navigate to the Endpoints section of the ngrok dashboard, then click New + and Cloud Endpoint.
In the URL field, enter the domain you just reserved to finish creating your Cloud Endpoint.
4. Add routing to your blue environment with Traffic Policy
While still viewing your new Cloud Endpoint in the dashboard, copy and paste the policy below into the Traffic Policy editor, depending on how you architected your services and deploy them.
Loading…
What's happening here?
This policy first assigns blue
/green
values to the appropriate environment.
It then plumbs those values into the rest of the policy so that you don't have to manually change your headers or internal URLs each time you do a blue-green deployment.
The policy then checks whether the x-deployment-version
header exists and its value equals the current test
variable.
If both are true
, it routes the request to your test environment.
The policy then sends all other requests to your current live environment.
5. Switch services or environments over
Edit your policy again to switch your environments and their states, where green
becomes live and blue
is your new testing environment.
Loading…
If you need to roll back, change the variables back to their original state.
What's next?
- View your Traffic Inspector to see how your blue and green environments are behaving, particularly after making the switch, to know exactly when and why to roll back.
- Learn how to do canary deployments with custom traffic splitting as a lower-cost, but more complex, alternative to blue-green.
- Explore other common gateway setups, like multiplexing to many services or shipping a custom "maintenance mode" during an outage or planned downtime a blue-green deployment can't help with.
- Start automating your deployment strategies with the ngrok API or our Kubernetes Operator.