Access services in your service mesh
In the previous tutorial, you deployed a Consul datacenter with service mesh enabled and an application running.
In this tutorial, you will learn how to add a Consul API Gateway in your service mesh and secure external network access to applications and services running in your Consul service mesh.
Note
This tutorial is part of the Get Started collection, for this reason all the steps used to configure Consul agents and services are shown and require to be executed manually. If you are setting up a production environment you should codify and automate the installation and deployment process. Refer to the VM production patterns tutorial collection for Consul production deployment best practices.
Tutorial scenario
This tutorial uses HashiCups, a demo coffee shop application made up of several microservices running on VMs.
At the beginning of the tutorial, you have a fully deployed Consul service mesh with Envoy sidecar proxies running alongside each service.
By the end of this tutorial, you will have enabled Consul API gateway and configured it to permit access to the HashiCups application over port 8443. You will also generate an SSL certificate to be exposed by the application.
Prerequisites
This tutorial uses an interactive lab to guide you through how to setting up service mesh on your VM workloads. The lab environment includes all required binaries and sample configurations.
Launch Terminal
This tutorial includes a free interactive command-line lab that lets you follow along on actual cloud infrastructure.
Configure environment
This tutorial and interactive lab environment uses scripts in the tutorial's GitHub repository to generate the Consul configuration files for your client agents.
The interactive lab environment includes these scripts. In Bastion Host,
list the files in the ops
directory.
The scripts rely on default parameters to generate the configuration files. Set the following default values. Ensure you have permission to write in the specified paths.
Tip
We suggest using an unprivileged user to run Consul for security reasons.
The lab contains two environment files that will help you configure the terminal and generate the required configuration files.
Import the two files in your environment.
The script creates all the files in a destination folder. Export the path where you wish to create the configuration files for the scenario.
Make sure the folder exists.
Verify your Consul CLI can interact with your Consul server.
Add API gateway node to Consul datacenter
Consul API Gateway uses the same components as the rest of the service mesh client nodes to join the Consul datacenter. This means that you need a Consul agent running and an Envoy proxy instance to act as a proxy for the services you want to expose outside your service mesh.
Generate Consul configuration for API gateway
First, define the Consul node name.
Then, generate the Consul configuration for the API Gateway node.
To complete Consul agent configuration, you need to setup tokens for the client. For this tutorial, you are using the bootstrap token. We recommend using more restrictive tokens for your Consul client agents in production.
Check generated files
Once you have generated your configuration files, your directory should look like the following:
The scripts generated multiple configuration files to separate the configuration so it is easier to read and tune them for your environment. The following are the generated files and a description of what they do:
- The
agent-acl-tokens.hcl
file contains tokens for the Consul agent. - The
agent-gossip-encryption.hcl
file configures gossip encryption. - The
consul-agent-ca.pem
file is the public certificate for Consul CA. - The
consul.hcl
file contains node specific configuration and it is needed, with this specific name, if you want to configure Consul as a systemd daemon.
Visit the agent configuration documentation to interpret the files or to modify them when applying them to your environment.
After the script generates the client configuration, you will copy these files into the API gateway node.
Tip
In the lab environment, the node has a running SSH server. As a result, you
can use ssh
and scp
commands to perform the following operations. If the
nodes in your personal environment does not have an SSH server, you may need to
use a different approach to create the configuration directories and copy the files.
First, define the Consul configuration directory.
Then, remove existing configuration from the VM.
Finally copy the configuration files into the remote VM.
Start Consul on API GW
Select the tab that corresponds with the service — in this case, API Gateway.
Define the Consul configuration and data directories.
Ensure your user has write permission to the Consul data directory.
Finally, start the Consul server process.
The process is started in background to not lock the terminal. Consul server log
can be accessed in the /tmp/consul-client.log
file.
Verify Consul API Gateway successfully joined the datacenter using the consul members
command.
The output should show an extra node, named gateway-api
among the datacenter
members.
Select the Bastion Host tab to continue with the tutorial.
Generate API Gateway rules
Now that the Consul agent for the API Gateway successfully joined the datacenter, it is time to create the configuration for the Consul Data Plane.
Consul API Gateway is configured using Consul global configuration entries so you can configure it from a remote node. For this scenario, you will use the bastion host VM to generate, store, and apply the configuration.
To configure a Consul API Gateway you need two configuration entries:
- An API Gateway configuration entry, defining the listeners that the gateway exposes externally and the certificates associated with them.
- An inline certificate to make the certificate available to the gateway.
Note
Consul v1.19 introduces the file-system-certificate
configuration entry to secure the Consul API Gateway on VMs. This configuration entry specifies the file path to a certificate and private key that must be present in the file system of the API gateway. The Consul server never sees the contents of these files. File system certificates require that you have access to the gateway's file system in order to place the certificate or update it.
Generate API Gateway configuration
The following API Gateway configuration entry includes listener configuration and a reference to a TLS certificate that the gateway exposes.
Generate API Gateway certificate
The gateway configuration includes a reference to api-gw-certificate
, a TLS
certificate. You can create the certificate using an internal or
public CA so your services can be compliant with your internal standards.
For this tutorial, you will use openssl
to generate a valid certificate for the
HashiCups application.
Define the certificate common name.
Create a configuration file for openssl.
Generate a private key.
Create a certificate signing request.
Finally, sign the certificate and save it to a crt
file.
You can now generate the certificate configuration using the .key
and .crt
files.
First populate two environment variables with the files content.
Then populate the configuration file with the inline certificate and key.
Apply the configuration to Consul datacenter
You can now apply the configuration to Consul datacenter.
Example output:
Start API gateway
Now that you have configured API Gateway, you can start the Envoy process that will serve external requests.
Select the tab that corresponds with the service — in this case, API Gateway.
Then, configure the token for the Envoy process.
Finally, start the Envoy process.
Once the Envoy process is started, select the Bastion Host tab to continue with the tutorial.
Apply route
At this point, Consul API Gateway is ready to serve requests but no route is configured to expose services in the mesh externally.
For this tutorial, you will expose the HashiCups application using the
hashicups-nginx
service as entry point.
Generate API Gateway route
From the bastion host, create a route to redirect ingress traffic to the
hashicups-nginx
service.
Then, apply the configuration to Consul.
Create intention for service access
To allow access to your NGINX service that serves the HashiCups application,
create an intention that allows traffic from gateway-api
service to
hashicups-nginx
service.
First make sure the folder exists.
Then create the intention configuration file.
After creating the configuration file for the intention, apply it.
Verify HashiCups is now reachable using API Gateway
After applying the route, you are able to access the HashiCups application using the Consul API gateway address.
Select the HashiCups API GW tab.
You now have exposed HashiCups using Consul API Gateway and the application is now TLS protected. Verify the certificate exposed by the application is the one you created earlier in this tutorial.
The output will be similar to the following:
The public access to your HashiCups application instance is now TLS secured.
Next steps
With this tutorial, you completed an important step towards the Zero Trust security model for a VM based infrastructure. You started from a scenario where the HashiCups application was deployed with no security features and introduced Consul service mesh gradually to the scenario.
At this moment:
- the HashiCups application is accessible only using the Consul API Gateway
- the internal communication across the different services that compose the application is fully secured
- you can expose a custom certificate for the externally exposed services and rotate it using Consul
In the next tutorial, you will learn how to monitor the services in your Consul service mesh using the Grafana suite.
For more information about the topics covered in this tutorial, refer to the following resources:
- API Gateway on Virtual Machines
- API Gateway configuration entry reference
- Inline certificate configuration entry reference
If you want to learn more about the File system certificate, introduced by Consul 1.9, refer to File system certificate configuration reference.