Skip to main content

Nomad Job Configuration

Central, Hongkong

How to add Configuration to your Application

To configure a task you can:

  • Add Commandline Arguments that are passed to your application when you run it.
  • Add Environment Variables that are accessible to Nomad to be passed on.
  • Use Artifacts to download and unzip required files
  • Use Templates to generate variables

Job Arguments

The http-echo application from is started - this time without Docker - using the Exec Driver with the /bin/http-echo command. The application is then configured using Arguments to run on port 5678 and return a Hello World when receiving an HTTP GET request:

group "example" {
task "server" {
driver = "exec"

config {
command = "/bin/http-echo"

args = [
"-listen",
":5678",
"-text",
"Hello World",
]
}
}
}

Environment Variables

Environment Variables can be passed on to our application inside the task block of our Nomad job file:

job "frontend" {
group "example" {
task "server" {
env {
DB_HOST = "my-db-host.com"
DB_USER = "db_user"
DB_PASS = "dbpassword"
}
}
}
}

Configuration Artifacts

Download a configuration file from a Git Repository using the Artifacts Stanza:

artifact {
# The git:: prefix forces go-getter's protocol detection to use the git ssh
# protocol. It can also automatically detect the protocol from the domain of
# some git hosting providers (such as GitHub) without the prefix.
source = "git::git@bitbucket.org:example/nomad-examples"
destination = "local/repo"
options {
# Make sure that the Nomad user's known hosts file is populated:
# ssh-keyscan github.com | sudo tee -a /root/.ssh/known_hosts
# https://github.com/hashicorp/go-getter/issues/55
sshkey = "${base64encode(file(pathexpand("~/.ssh/id_rsa")))}"
}
}

To download from a private repo, sshkey needs to be set. The key must be base64-encoded string. On Linux, you can run base64 -w0 <file> to encode the file.

Templates

The template block instantiates an instance of a template renderer. This creates a convenient way to ship configuration files that are populated from environment variables, Consul data, Vault secrets, or just general configurations within a Nomad task:

job "docs" {
group "example" {
task "server" {
artifact {
source = "https://example.com/redis.conf.tpl"
destination = "local/redis.conf.tpl"
}
template {
source = "local/redis.conf.tpl"
destination = "local/redis.conf"
change_mode = "signal"
change_signal = "SIGINT"
}
}
}
}

You can also utilize inline templates and turn them into environment variables like this:

template {
data = <<EOH
---
bind_port: {{ env "NOMAD_PORT_db" }}
scratch_dir: {{ env "NOMAD_TASK_DIR" }}
node_id: {{ env "node.unique.id" }}
service_key: {{ key "service/my-key" }}
EOH

destination = "local/file.env"
env = true
}