An API Gateway is used for controlling access at entry and traffic management. You can read more about it the API gateways overview documentation.
This repository contains a working example of how to deploy an API Gateway using Consul and Nomad. It includes the following:
This example uses Nomad's Workload Identity to authorize a Consul task to bootstrap the Envoy gateway task and correctly register it with Consul. The API Gateway is deployed in its own Nomad namespace. You'll add a Consul ACL binding rule that matches for that Nomad namespace and that will grant the appropriate permissions to the API Gateway.
Please refer to Deploy a Consul API Gateway on Nomad for the accompanying tutorial for this repo.
If you already have running Nomad and Consul clusters with Workload Identity as described in the Consul ACL with Nomad Workload Identities tutorial, you can skip this section.
If you've skipped this section because you already have a Nomad and Consul cluster with Workload Identity configured, you should ensure your terminal is configured with environment variables to connect to both the Consul and Nomad APIs, and that you have a management token for both. Typically this should include all the following variables:
CONSUL_CACERT
CONSUL_CLIENT_CERT
CONSUL_CLIENT_KEY
CONSUL_HTTP_ADDR
CONSUL_HTTP_TOKEN
NOMAD_CACERT
NOMAD_CLIENT_CERT
NOMAD_CLIENT_KEY
NOMAD_ADDR
NOMAD_TOKEN
Return to the root of the repository if you haven't already.
Create a Nomad namespace:
nomad namespace apply \
-description "namespace for Consul API Gateways" \
ingress
Create a Consul ACL binding rule for the API Gateway that assigns the
builtin/api-gateway
templated policy to Nomad workloads deployed into the Nomad
namespace ingress
that you just created.
consul acl binding-rule create \
-method 'nomad-workloads' \
-description 'Nomad API gateway' \
-bind-type 'templated-policy' \
-bind-name 'builtin/api-gateway' \
-bind-vars 'Name=${value.nomad_job_id}' \
-selector '"nomad_service" not in value and value.nomad_namespace==ingress'
The API Gateway job needs Consul mTLS certificates to communicate with Consul. The job specification uses Nomad Variables to store these securely, but you could also use Vault secrets. These variables need to be written to the same namespace that the job will be deployed to.
nomad var put -namespace ingress \
nomad/jobs/ingress/gateway/setup \
consul_cacert=@$CONSUL_CACERT \
consul_client_cert=@$CONSUL_CLIENT_CERT \
consul_client_key=@$CONSUL_CLIENT_KEY
Look at the api-gateway.nomad.hcl
job specification file at the root of this
repository. Edit the main listener port network.port.http.static
so that it's
listening on whichever port you'd like.
Run the Nomad job.
nomad job run ./api-gateway.nomad.hcl
If you have specific Docker images or Nomad namespace you'd like to use, pass
the -var
option to the nomad job run
command. For example:
nomad job run \
-var="consul_image=hashicorp/consul:1.18.1" \
-var="envoy_image=hashicorp/envoy:1.28.1" \
-var="namespace=ingress" \
./api-gateway.nomad.hcl
Once the deployment is complete, you can check the Consul UI to see the API Gateway registered.
This section will run an example upstream application hello
and configure
access to it from the API Gateway.
Add intentions to allow traffic from the API Gateway to hello
by running
consul config write example/hello-app-intentions.hcl
Register a listener for the API Gateway by running consul config write example/gateway-listeners.hcl
Register http routes for the API Gateway so that Envoy knows how and where to
write the traffic by running consul config write example/my-http-route.hcl
Start the hello
app by running nomad run example/hello-app.nomad.hcl
Once the deployment is complete, you can test the API Gateway.
nomad job status -namespace ingress ingress
to find the allocation for
the API gateway.nomad alloc -namespace ingress status :allocID
to find the address for that
allocation.curl -v http://<api-gateway-address>:<api-gateway-port>/hello
For additional debugging, you can dive into Envoy configs through the Envoy
admin url and nomad alloc exec
, Nomad job logs and Consul catalog service
definition.