Securely connect your services with Consul service mesh
In the previous tutorial, you deployed Consul client agents and registered services to your Consul catalog.
In this tutorial, you will introduce zero trust security in your network by implementing Consul service mesh. This will enable secure service-to-service communication and allow you to leverage Consul's full suite of features.
In order to do this, you will edit the service definitions on your Consul clients, launch Envoy sidecar proxies, and create service intentions to allow traffic across your services in your network.
In this tutorial, you will use a test environment to:
- Review and create intentions to manage traffic permissions
- Modify Consul services' configuration for Consul service mesh
- Start Envoy sidecar proxy for each service in the mesh
- Restart the services to listen on localhost interface
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 Consul datacenter with one server and four clients running on VMs. The services connect directly to each other using the VMs address and access every service in the network.
By the end of this tutorial, you will have a fully deployed Consul service mesh with Envoy sidecar proxies running alongside each service. The services will be configured so they cannot be reachable, unless explicitly allowed through Consul service intentions.
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.
Verify that the VM you want to deploy the Consul server on has the Consul binary.
Verify Envoy installation on the VMs
In the interactive lab environment, click on each tab associated with the client nodes (NGINX, Frontend, API, and Database) and verify Envoy is installed.
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.
Review and create intentions
The initial Consul configuration denies all service connections by default. We recommend this setting in production environments to follow the "least-privilege" principle, by restricting all network access unless explicitly defined.
Intentions let you allow and restrict access between services. Intentions are destination-orientated — this means you create the intentions for the destination, then define which services can access it.
The following intentions are required for HashiCups:
Tip
Notice these descriptions show traffic going from the destination to the source service.
- The
db
service needs to be reached by theapi
service. - The
api
service needs to be reached by thenginx
services. - The
frontend
service needs to be reached by thenginx
service.
Use the provided script to generate service intentions.
Check the files generated by the script.
Finally apply the intentions to your Consul datacenter.
Use config write
to create the following intentions.
Create the intentions for the db
service.
Create the intentions for the api
service.
Create the intentions for the frontend
service.
Apply new Consul client configuration
Before you can apply the new service configuration, you must copy the definition files into each Consul client VMs.
Tip
In the interactive lab environment, the HashiCups application nodes have 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, configure the Consul configuration directory.
Then, copy the configuration into the client VMs.
Start sidecar proxies on client VMs
Once you copied the configuration files on the different VMs, login on each Consul client VMs and restart the Consul agent.
Start sidecar proxy for Database
Select the tab that corresponds with the service — in this case, Database.
Define the Consul configuration directory.
Setup a valid token to interact with Consul agent.
Reload consul configuration.
Finally, start the Envoy sidecar proxy for the service.
The command starts the Envoy sidecar proxy in the background to not lock the
terminal. You can access the Envoy log through the /tmp/sidecar-proxy.log
file.
Start sidecar proxy for API
Select the tab that corresponds with the service — in this case, API.
Define the Consul configuration directory and setup a valid token to interact with Consul agent.
Reload consul configuration.
Finally, start the Envoy sidecar proxy for the service.
The command starts the Envoy sidecar proxy in the background to not lock the
terminal. You can access the Envoy log through the /tmp/sidecar-proxy.log
file.
Start sidecar proxy for Frontend
Select the tab that corresponds with the service — in this case, Frontend.
Define the Consul configuration directory and setup a valid token to interact with Consul agent.
Reload consul configuration.
Finally, start the Envoy sidecar proxy for the service.
The command starts the Envoy sidecar proxy in the background to not lock the
terminal. You can access the Envoy log through the /tmp/sidecar-proxy.log
file.
Start sidecar proxy for NGINX
Select the tab that corresponds with the service — in this case, NGINX.
Define the Consul configuration directory and setup a valid token to interact with Consul agent.
Reload consul configuration.
Finally, start the Envoy sidecar proxy for the service.
The command starts the Envoy sidecar proxy in the background to not lock the
terminal. You can access the Envoy log through the /tmp/sidecar-proxy.log
file.
Restart services to listen on localhost
Now that the service configuration is applied, intentions are applied, and Envoy sidecars are started for each service, all the components for the Consul service mesh are in place. The Consul sidecar proxies will route the services' traffic to the target destination.
Since traffic is flowing through the sidecar proxies, you no longer need to expose your services externally. As a result, reconfigure them to listen on the loopback interface only to improve overall security.
Select the Bastion Host tab.
Reload the services to operate on the localhost
interface.
This tutorial still configures the NGINX service to listen on the VM's IP so you can still access it remotely. For production, we recommend using an ingress gateway to manage access to the service.
Click on the HashiCups tab.
Confirm that HashiCups still works despite its services being configured to
communicate on localhost
. The Envoy sidecar proxies route each service's local
traffic to the relevant upstream.
Next steps
In this tutorial, you learned how to migrate your Consul services from service
discovery to Consul service mesh by updating each service's definitions,
starting Envoy sidecar proxies for each service, and updating the services'
dependencies to bind to localhost
.
In the process, you integrated Zero Trust Security in your network and learned how to define explicitly define service-to-service permissions using intentions.
At this point, the NGINX service used to expose the application externally is still accessible over an insecure connection. While it is possible to configure it for secure traffic, using TLS, Consul offers an integrated solution for this.
In the next tutorial, you will learn how to add a Consul API Gateway to your service mesh and secure external network access to applications and services running in your Consul service mesh.
For more information about the topics covered in this tutorial, refer to the following resources: