DRY Nomad job specs with Levant
Nomad Pack is a new package manager and templating tool that can be used instead of Levant. Nomad Pack is currently in Tech Preview and may change during development.
With Levant you can create and submit Nomad job specifications from template job specifications. These template job specifications reduce the overall amount of boilerplate that you have to manage for job files that contain a significant amount of repeated code.
In this tutorial, you iteratively modify a template for rendering with Levant with the goal of deploying a Zookeeper node while reducing the overall job specification size and increasing modularity of job elements.
Prerequisites
You need:
A Nomad cluster with:
Consul integrated
Docker installed and available as a task driver
a Nomad host volume named
zk1
configured on a Nomad client agent to persist the Zookeeper data
You should be:
Familiar with Go's text/template syntax. You can learn more about it in the Learn Go Template Syntax tutorial.
Comfortable in your shell of choice, specifically adding executables to the path, editing text files, and managing directories.
Download Levant
Install Levant using the instructions found in the README of its GitHub
repository. Use one of the methods that provides you with a binary, rather than
the Docker image. Verify that you have installed it to your executable path by
running levant version
.
Build the starting job file
Create a text file called zookeeper.nomad with the following content.
This job creates a single Zookeeper instance, with dynamically selected values for the ZK client, quorum, leader election, and admin ports. These ports are all registered in Consul. The Zookeeper configuration is written out as a simple template and the Zookeeper cluster member configuration is generated by the template stanza using the addresses and ports advertised in Consul.
Verify your configuration
Run this template in Nomad
Submit the job to the Nomad cluster.
Verify Zookeeper
To verify your Zookeeper is up and responsive, connect to its admin port and
run the srvr
four letter command.
Get Zookeeper's admin port
You can use one of several ways to get the Zookeeper admin port. Click on one of these techniques for more details.
This output indicates that the service is accessible at 10.0.2.51:26284
Run nomad job status zookeeper
and make a note of the running allocation's ID.
In this case the ID
is b1111699
.
Run nomad alloc status
and supply the allocation ID.
Consult the Allocation Addresses section of the output. In this example, the admin address is at 10.0.2.51:26284.
Open the Nomad UI in your browser. Select the running zookeeper job. From the job details screen, select the zk1 task group, select the running allocation's ID.
Once you are on the allocation detail page, you can get the port mappings by scrolling down to the Ports table.
Run srvr
command via Zookeeper admin
Now that you have located the port that the admin server is running on, make a
request to run the srvr
command. The following is an example using curl.
Stop and purge the job
Run nomad stop -purge zookeeper
to stop the Zookeeper instance. Using the
-purge
flag while stopping reduces the amount of data you have to consult
if you have to debug a non-starting instance.
Make a template
Back up the original template by creating a copy named zookeeper.nomad.orig.
Optimize the template
Open zookeeper.nomad in a text editor. There is some file-like content that you might want to extract from the template for ease of maintenance, like the template used to generate the Zookeeper configuration in the zoo.cfg file. Also, notice that there are many repeated elements in the services and network ports. The following steps cover techniques for removing repeated or reusable material from your job specification.
Include zoo.cfg rather than inline it
One improvement involves making zoo.cfg external to the job template. For a lengthy configuration file, it's better to keep the template content separate from the rest of the configuration when possible. Navigate to the template stanza that creates the zoo.cfg file in the job.
Create a text file named zoo.cfg
with the contents of the data
element.
Remove this content from the data element and replace it with a call to Levant's
fileContents
function. This loads the content from the zoo.cfg file and inserts
it in place of the action. Don't forget that Levant uses double square brackets
as its left and right delimiters for actions. Your template stanza should look
like the following.
Externalize the consul-template
Perform the same sort of change to the template stanza that builds the Zookeeper cluster membership.
Create a file called template.go.tmpl
with the contents of the template.
Update the template stanza to use fileContents
to import the template from file.
Validate your updates
Use the levant render
command to validate that your updates to the job spec
are working as you expect.
Barring any errors, the template renders to the screen with the zoo.cfg and dynamic template content embedded in the rendered output.
Did you know? Levant automatically processes single files with a .nomad extension. Naming the backup zookeeper.nomad.orig prevented Levant from considering it when looking for a file to render.
Reduce boilerplate with iteration
Range over a list
Both the network stanza and the services contain configuration based on the
Zookeeper protocol. Start with the network
stanza.
Create a list with values suitable to range over. Immediately before the network stanza and after the empty line, insert a line with this content.
This action creates a list with the protocol types named $Protocols
. Note the
right delimiter does not suppress the whitespace. This allows the network
stanza to render in the correct place in the output. For outputs that do not
require strict placement—like HCL—you could chose to do less whitespace
management and end with functional, but perhaps less readable outputs.
Now, remove all of the content inside of the network stanza and replace it with
this template code that ranges over the list and creates the same configuration.
Recall that the range function iterates over each element in a list and allows
for saving the value into a variable along with the list index. Also, note the
use of [[-
and -]]
delimiters along with some actions ([[ "" ]]
) that do
nothing to control whitespace.
Re-render your template by running the levant render
command and ensuring that
your network stanza contains client
, peer
, and election
ports with to
values set to -1; and it contains an admin
port with a to
value of 8080.
Services stanzas
Locate the service stanzas in the job specification and note the amount of
boilerplate. Use the same list to create corresponding service
stanzas. The
goal being to replace the static list given in the job file with a template
driven version.
This time, try to create a template that preserves the whitespace. If you get stuck, you can refer to this solution.
Test your solution by rendering the template using the levant render
command.
Remove the existing service
stanzas and replace them with the following
template. This solution builds a list and uses toJson
to format it for output
as the tags
value; however, you could also use printf
to build a string to
output as the tags
value
Submit the job with Levant
Levant also implements a Nomad client capable of submitting a rendered template directly to Nomad. Now that you have an equivalent CPU template, use Levant to submit it to Nomad.
Levant uses the NOMAD_ADDR
environment variable or -address=
flag to
determine where to submit the job. Set the NOMAD_ADDR environment variable to an
appropriate value for your environment.
Run the levant deploy
command. Levant monitors the deployment and waits
until it is complete before returning to the command line.
Validate that your Zookeeper node is running properly by using the steps presented earlier in this tutorial.
Learn more
In this tutorial, you reduced the size of a Nomad job file by approximately one- third by externalizing certain elements of the job specification. By externalizing these elements, you can:
- Make them reusable in other jobs
- Reduce the visual noise for editors who may not be familiar with Nomad
- Allow for tighter controls in source control due to more granular artifacts
- Enable the creation of abstract job templates
Next, consider doing the Make Abstract Job Specs with Levant tutorial since it continues with this job specification—enhancing it to be abstract, configurable via a JSON file, and deployed via Levant.
Clean up
To clean up, stop the Zookeeper job with nomad job stop zookeeper
Keep your files and client configuration if you are continuing to the Make Abstract Job Specs with Levant tutorial.
Otherwise, you can clean up the files you used in this tutorial by
removing the zookeeper.nomad.orig
, zookeeper.nomad
, zoo.cfg
, and
template.include
files from your machine.
Optionally, remove the zk1
host_volume from your client configuration and
delete the directory that backed it.