Rolling updates
Nomad supports rolling updates as a first class feature. To enable rolling
updates a job or task group is annotated with a high-level description of the
update strategy using the update
stanza. Under the hood, Nomad handles
limiting parallelism, interfacing with Consul to determine service health and
even automatically reverting to an older, healthy job when a deployment fails.
Enable rolling updates for job
Rolling updates are enabled by adding the update
stanza to the job
specification. The update
stanza may be placed at the job level or in an
individual task group. When placed at the job level, the update strategy is
inherited by all task groups in the job. When placed at both the job and group
level, the update
stanzas are merged, with group stanzas taking precedence
over job level stanzas. There is more information about
inheritance in the update
stanza documentation.
In this example, by adding the simple update
stanza to the "api-server" task
group, you inform Nomad that updates to the group should be handled with a
rolling update strategy.
Thus when a change is made to the job file that requires new allocations to be made, Nomad will deploy 2 allocations at a time and require that the allocations be running in a healthy state for 30 seconds before deploying more versions of the new group.
By default Nomad determines allocation health by ensuring that all tasks in the group are running and that any service check the tasks register are passing.
Check the planned changes
Suppose you make a change to a file to update the version of a Docker container that is configured with the same rolling update strategy from above.
The nomad job plan
command allows you to visualize the series of steps the
scheduler would perform. You can analyze this output to confirm it is correct:
Here you can observe that Nomad will begin a rolling update by creating and
destroying two allocations first; for the time being ignoring four of the old
allocations, consistent with the configured max_parallel
count.
Inspect a deployment
After running the plan you can submit the updated job by running nomad run
.
Once run, Nomad will begin the rolling update of the service by placing two
allocations at a time of the new job and taking two of the old jobs down.
You can inspect the current state of a rolling deployment using nomad status
:
The output indicates that Nomad has created a deployment to conduct the rolling update from job version 0 to 1. It has placed four instances of the new job and has stopped four of the old instances. Consult the list of deployed allocations, and note that Nomad has placed four instances of job version 1 but only considers 2 of them healthy. This is because the two newest placed allocations haven't been healthy for the required 30 seconds yet.
Wait for the deployment to complete, and then re-issue the command. You will receive output similar to the following:
Nomad has successfully transitioned the group to running the updated canary and did so with no downtime to your service by ensuring only two allocations were changed at a time and the newly placed allocations ran successfully. Had any of the newly placed allocations failed their health check, Nomad would have aborted the deployment and stopped placing new allocations. If configured, Nomad can automatically revert back to the old job definition when the deployment fails.
Use auto-revert on failed deployments
In the case you do a deployment in which the new allocations are unhealthy, Nomad will fail the deployment and stop placing new instances of the job. It optionally supports automatically reverting back to the last stable job version on deployment failure. Nomad keeps a history of submitted jobs and whether the job version was stable. A job is considered stable if all its allocations are healthy.
To enable this, add the auto_revert
parameter to the update
stanza:
Now imagine you want to update your image to "geo-api-server:0.3" but you instead update it to the below and run the job:
Running nomad job deployments
will show that the deployment fails, and Nomad
auto-reverted to the last stable job:
Nomad job versions increment monotonically. Even though Nomad reverted to the
job specification at version 1, it creates a new job version. You can observe the
differences between a job's versions and how Nomad auto-reverted the job using
the job history
command:
This output describes the process of a reverted deployment. Starting at the end of the output and working backwards, Nomad shows that version 0 was submitted. Next, version 1 was an image change from 0.1 to 0.2 of geo-api-server and was flagged as stable. Version 2 of the job attempted to update geo-api-server from 0.2 to 0.33; however, the deployment failed and never became stable. Finally, version 3 of the job is created when Nomad automatically reverts the failed deployment and redeploys the last healthy version--geo-api-server version 0.2.