Register and discover services within namespaces
Enterprise Only
The namespace functionality demonstrated here requires HashiCorp Cloud Platform (HCP) or self-managed Consul Enterprise. If you've purchased or wish to try out Consul Enterprise, refer to how to access Consul Enterprise.
Namespaces allow multiple teams within the same organization to share the same Consul datacenter(s) by separating services, Consul KV data, and other Consul data per team. This provides operators with the ability to more easily provide Consul as a service. Namespaces also enable operators to delegate ACL management.
Any service that is not registered to a namespace is added to the default
namespace. All services are added to namespaces in Consul version 1.7 and
later.
By the end of this tutorial, you will register two services in the Consul catalog:
one in the default
namespace and one in an operator-configured namespace.
After you have registered the services, you will then use the Consul CLI, API
and UI to discover all the services registered in the Consul catalog.
Prerequisites
To complete this tutorial, you will need a Consul Enterprise version 1.13.1 cluster or newer.
Tip
The content of this tutorial also applies to Consul clusters hosted on HashiCorp Cloud (HCP).
You should also have at least one namespace configured. Review the Setup Secure Namespaces tutorial for a complete example or use the below example for a quick setup.
Create a HCL file called app-team.hcl
with a name and description for the namespace.
Next, use the Consul CLI to create the namespace by providing Consul with the namespace definition file.
Register services in namespaces
You register services in a namespace by adding namespace information to the registration. This should not disrupt your existing workflow. The namespace information can be added to the registration using one of the following methods.
- add the
namespace
option to the service registration file. - use the
-namespace
flag with the API or CLI at registration time.
If you would like to migrate an existing service into a new namespace, re-register the service with the new namespace information.
Default namespace service registration
To register a service in the default
namespace, use your existing registration
workflow; you do not need to add namespace information as long as a namespace
ACL token isn't provided. In the example below, you will register the mysql
service in the default namespace.
First, create a service registration file for the MySQL service.
Next, register the service using the Consul CLI.
App-team namespace service registration
To register a service in a user-defined namespace, include the namespace in the registration file or pass it with a flag at registration time. In this tutorial, we will include the namespace in the file.
First, create the service registration file named wordpress.hcl
. Paste in the
following registration, which includes the service name, port, and namespace.
Next register the service.
Discover services
Finally, you will discover your services using the Consul CLI, UI, DNS interface, and HTTP API.
Consul CLI
To get a list of services in the default namespace use the consul catalog
CLI
command.
Notice that the command does not list services that are registered in the app-team namespace.
Add the -namespace
flag to discover services within a user-created namespace.
Notice that the command does not list services that are registered in the default namespace. To discover all services in the catalog, you will need to query all Consul namespaces individually.
Consul UI
You can also view namespace services in the Consul UI. Select a namespace using the drop-down menu at the top of the top navigation. Then go to the “Services” tab to see the services within the namespace.
DNS interface
Note
The default DNS query behavior allows you to query by datacenter,
<service-name>.service.<datacenter>.consul
, or datacenter and namespace,
<service-name>.service.<namespace>.<datacenter>.consul
. To use only the namespace
parameter in the DNS query, you must
set the prefer_namespace
option to true
in the agent's configuration.
With the option set to true, the new query structure will be, <service-name>.service.<namespace>.consul
. This will disable
the ability to query by datacenter only. However, with the option set to true
you can still use both namespace and
datacenter to the query.
Next, use dig
to discover the location of wordpress service instance.
In the output, the query returns one answer.
If you don’t specify a namespace in the query, you will get results from the default namespace.
The output returns 0 answers, the wordpress service does not exist in the default namespace.
Consul HTTP API
Finally, use the Consul HTTP API to discover more information about the wordpress service. To discover service information
within a namespace, add the ns=
query parameter to the query.
The HTTP API is more verbose than the DNS interface; it allows you to discover the service locations and additional metadata.
Next steps
In this tutorial, you registered two services: the wordpress service in the
app-team
namespace and the mysql service in the default
namespace. You then
used the Consul CLI, UI, and HTTP API to discover services in both namespaces.
For a complete example on service and sidecar proxy registration, review the Secure Service Communication with Consul Service Mesh and Envoy.