Secure Nomad jobs with Consul service mesh
Nomad's first-class integration with Consul allows operators to design jobs that natively leverage Consul service mesh and transparent proxy. In this tutorial, you will learn how to configure Consul to allow access between workloads and run a sample Consul service mesh workload.
Prerequisites
- Nomad v1.8.0 or greater. All recent versions of Nomad support Consul service mesh, but only Nomad 1.8.0 or greater supports the transparent proxy block demonstrated here.
Create the Consul and Nomad clusters
This section uses development agents for Consul and Nomad as a quick way to get started. Development agents have ephemeral state and should not be used in production environments. They run in the foreground of your terminal so be sure not to close the terminal window or the agent configuration steps will need to be rerun again.
This setup uses hard-coded tokens in the Consul configuration. We do not recommend this method for production clusters. Follow the Secure Consul with ACLs tutorial to configure production Consul clusters.
Create a directory for the tutorial on your local machine, change into that
directory, and create a file named consul.hcl
to store the Consul agent
configuration. Add the following contents to it and replace the two instances of
the placeholder "<< IP ADDRESS >>"
with the IP address of your
local machine. Save the file.
Start a Consul dev agent using consul.hcl
as the configuration file.
Open another terminal window in the same directory and set the Consul management token as an environment variable.
Create a file named consul-policy-nomad-agents.hcl
to store the Consul ACL
rules that grant the necessary permissions to Nomad agents. Add the following
contents to it and save the file.
Create a Consul ACL policy named nomad-agents
with the rules defined in the
consul-policy-nomad-agents.hcl
file.
Create a Consul ACL token for the Nomad agent using the nomad-agents
ACL
policy.
Save the value of SecretID
for the Consul ACL token.
Download the consul-cni
CNI
plugin. Unzip the archive and move the consul-cni
binary to wherever you
install the CNI reference plugins as described in Nomad's Post Installation
Steps. A commonly used path is
/opt/cni/bin
.
Create a file named nomad.hcl
. Add the following contents to it and replace the
placeholder <Consul token SecretID>
text with the value of SecretID
. Save the file.
Open another terminal window in the same directory and start the Nomad dev
agent using nomad.hcl
as the configuration file.
Bootstrap the Nomad ACL system.
Copy the value of Secret ID
and set it as the environment variable
NOMAD_TOKEN
.
Use the nomad setup
command to configure Consul to use Nomad workload
identity. The nomad.hcl
file contains the recommended configuration set by
the command.
Verify Nomad client Consul configuration
Verify that the Nomad client nodes detect Consul and the correct configuration
for Consul service mesh using nomad node status -verbose
. Specifically,
verify that consul.connect
is true
, consul.dns.addr
and consul.dns.port
are enabled (not set to -1
), and plugins.cni.version.consul-cni
is showing
version 1.4.2 or above.
The Nomad client agents will periodically fingerprint the Consul agent every few minutes. Restarting the Nomad agent will force Nomad to immediately detect any changes.
Alternative architectures (non-x86/amd64)
If you are using an ARM or another non-x86/amd64 architecture, go to the Alternative Architectures section of this tutorial for additional setup details.
Run a connect
-enabled job
Create the job specification
Create the "countdash" job by copying this job specification into a file named
countdash.nomad.hcl
. Save the file.
Create a service intention
In Consul, the default intention behavior is defined by the default ACL policy. If the default ACL policy is "allow all", then all service mesh connections are allowed by default. If the default ACL policy is "deny all", then all service mesh connections are denied by default.
To avoid unexpected behavior around this, it is better to create an explicit intention. Create an intention to allow traffic from the count-dashboard service to the count-api service.
First, create a file for a config entry definition named intention-config.hcl
.
Initialize the intention rules. For more information, check out the Service Intentions documentation.
Run the job
Run the job by calling nomad run countdash.nomad.hcl
.
The command will output the result of running the job and show the allocation IDs of the two new allocations that are created.
Get the address of the dashboard application from the allocation status output.
Navigating to the dashboard on port 9002
of the Nomad client host shows
a green "Connected" badge.
Stop the job. The stop
command will stop the allocations in the background
and output evaluation information about the stop request.
Advanced considerations
Alternative architectures
Nomad provides a default link to a pause image. This image, however, is architecture specific and is only provided for the amd64 architecture. In order to use Consul service mesh on non-x86/amd64 hardware, you will need to configure Nomad to use a different pause container. If Nomad is trying to use a version of Envoy earlier than 1.16, you will need to specify a different version it as well. Read through the section on airgapped networks below. It explains the same configuration elements that you will need to set to use alternative containers for service mesh.
Special thanks to @GusPS
, who reported this working configuration.
Envoy 1.16 now has ARM64 support. Configure it as your sidecar image by setting
the connect.sidecar_image
meta
variable on each of your ARM64
clients.
The rancher/pause
container has versions for several different architectures
as well. Override the default pause container and use it instead. In your client
configuration, add an infra_image
to your docker plugin configuration
overriding the default with the rancher version.
If you came here from "Alternative Architectures" note above, return there now.
Airgapped networks or proxied environments
If you are in an airgapped network or need to access Docker Hub via a proxy, you will have to perform some additional configuration on your Nomad clients to enable Nomad's Consul service mesh integration.
Set the "infra_image" path
Set the infra_image
configuration option for the Docker driver plugin on
your Nomad clients to a path that is accessible in your environment. For
example,
Changing this value will require a restart of the Nomad client agent.
Set the "sidecar_image" path
You will also need the Envoy proxy image used for Consul service mesh
networking. Configure the sidecar image on your Nomad clients to override the
default container path by adding a "connect.sidecar_image"
value to the
client.meta
block of your Nomad client configuration. If you do
not have a meta
block inside of your top-level client
block, add one as
follows.
Changing this value will require a restart of the Nomad client
agent. Alternately, you can set the value with the nomad node meta apply
command.
Next steps
Learn more about Nomad's Consul service mesh integration by checking out these resources.
- Nomad Consul Service Mesh documentation
consul
block in job specificationconnect
block in job specificationconsul
block in agent configuration
Learn more about Consul service mesh with these tutorials.
Learn more about Consul ACLs.