Blue/Green and canary deployments
Sometimes rolling updates do not offer the required flexibility for updating an application in production. Often organizations prefer to put a "canary" build into production or utilize a technique known as a "blue/green" deployment to ensure a safe application roll-out to production while minimizing downtime.
Blue/Green deployments
Blue/Green deployments have several other names including Red/Black or A/B, but the concept is generally the same. In a blue/green deployment, there are two application versions. Only one application version is active at a time, except during the transition phase from one version to the next. The term "active" tends to mean "receiving traffic" or "in service".
Imagine a hypothetical API server which has five instances deployed to production at version 1.3, and you want to safely update to version 1.4. You want to create five new instances at version 1.4 and in the case that they are operating correctly you want to promote them and take down the five versions running 1.3. In the event of failure, you can quickly rollback to 1.3.
To start, you examine your job which is running in production:
Notice that the job has an update
stanza with the canary
count equal to the
desired count. This allows a Nomad job to model blue/green deployments. When you
change the job to run the "api-server:1.4" image, Nomad will create five new
allocations while leaving the original "api-server:1.3" allocations running.
Observe how this works by changing the image to run the new version:
Next, plan these changes. Save the modified jobspec with the new version of api-server
to a file name docs.nomad.hcl
.
Run the changes.
The plan output states that Nomad is going to create five canaries running the "api-server:1.4" image and ignore all the allocations running the older image. Now, if you examine the status of the job you will note that both the blue ("api-server:1.3") and green ("api-server:1.4") set are running.
Now that the new version is running in production, you can route traffic to it and validate that it is working properly. If so, you would promote the deployment and Nomad would stop allocations running the older version. If not, you would either troubleshoot one of the running containers or destroy the new containers by failing the deployment.
Promote the deployment
After deploying the new image along side the old version you have determined it is functioning properly and you want to transition fully to the new version. Doing so is as simple as promoting the deployment:
If you inspect the job's status, you can observe that after promotion, Nomad stopped the older allocations and is only running the new one. This now completes the blue/green deployment.
Fail a deployment
After deploying the new image alongside the old version you have determined it is not functioning properly and you want to roll back to the old version. Doing so is as simple as failing the deployment:
After failing the deployment, check the job's status. Confirm that Nomad has stopped the new allocations and is only running the old ones, and that the working copy of the job has reverted back to the original specification running "api-server:1.3".
Deploy with canaries
Canary updates are a useful way to test a new version of a job before beginning
a rolling update. The update
stanza supports setting the number of canaries
the job operator would like Nomad to create when the job changes via the
canary
parameter. When the job specification is updated, Nomad creates the
canaries without stopping any allocations from the previous job.
This pattern allows operators to achieve higher confidence in the new job version because they can route traffic, examine logs, etc, to determine the new application is performing properly.
In the example above, the update
stanza tells Nomad to create a single canary
when the job specification is changed.
You can experience how this behaves by changing the image to run the new version:
Next, plan these changes.
Run the changes.
Note from the plan output, Nomad is going to create one canary that will run the
"api-server:1.4" image and ignore all the allocations running the older image.
After running the job, The nomad status
command output shows that the canary
is running along side the older version of the job:
Now if you promote the canary, this will trigger a rolling update to replace the
remaining allocations running the older image. The rolling update will happen at
a rate of max_parallel
, so in this case, one allocation at a time.
Check the status.
Alternatively, if the canary was not performing properly, you could abandon the
change using the nomad deployment fail
command, similar to the blue/green
example.