Deploy HCP Consul Dedicated with AKS using Terraform
HashiCorp Cloud Platform (HCP) Consul is a fully managed Service Mesh as a Service (SMaaS) version of Consul. The HCP Portal has a quickstart template that deploys an end-to-end development environment so you can see HCP Consul Dedicated in action. This Terraform configuration:
- Creates a new HashiCorp virtual network (VNet) and single-node Consul development server
- Connects the HVN with your Azure virtual network (VNet)
- Provisions an Azure Kubernetes Service (AKS) cluster and installs a Consul client
- Deploys HashiCups, a demo application that uses Consul service mesh
In this tutorial, you will use the HCP Consul Dedicated Terraform automation workflow to deploy an end-to-end deployment environment. In the process, you will review the Terraform configuration to better understand how the various components of the development environment interact with each other. This will equip you with the skills to deploy and adopt HCP Consul Dedicated for your own workloads.
Prerequisites
To complete this tutorial you will need the following.
- Terraform v1.0.0+ CLI installed
- An HCP account configured for use with Terraform
- an Azure account
- the Azure CLI
In order for Terraform to run operations on your behalf, login into Azure.
Generate Terraform configuration
You can generate a Terraform configuration for the end-to-end deployment directly from the Overview page in your HCP organization.
Click on the tab(s) below to go through each step to select the Terraform Automation deployment method.
Once you have selected the Terraform automation workflow, the HCP Portal presents two options:
- Use an existing virtual network (VNet)
- Create a new virtual network (VNet)
Select the tab for your preferred deployment method.
Fill in all the fields. The HCP region must be the same as your VNet region to reduce latency between the HCP Consul Dedicated server cluster and the Consul client running on the AKS cluster.
The wizard will use this to customize your Terraform configuration, so it can deploy an HVN and peer it to your existing VNet.
Tip
Click on the Where can I find this? links to get help in locating the right values for each fields.
Once you have filled in all the fields, scroll down to the Terraform
Configuration section to find the generated Terraform configuration. Click on
Copy code to copy it to your clipboard and save it in a file named
main.tf
.
Click on the accordion to find an example Terraform configuration. This example is not guaranteed to be up-to-date. Always refer to and use the configuration provided by the HCP UI.
The locals
block reflects the values of your existing VNet and resource group,
in addition to pre-populated fields with reasonable defaults.
- The
hvn_region
defines the HashiCorp Virtual Network (HVN) region. - The
hvn_id
defines your HVN ID. HCP will pre-populate this with a unique name that uses this pattern:consul-quickstart-UNIQUE_ID-hvn
. - The
cluster_id
defines your HCP Consul Dedicated cluster ID. HCP will pre-populate this with a unique name that uses this pattern:consul-quickstart-UNIQUE_ID
. - The
subscription_id
defines your Azure subscription ID. - The
vnet_rg_name
defines the resource group your VNet is in. - The
vnet_id
defines your VNet ID. Terraform will use this to set up a peering connection between the HVN and your VNet. - The
vnet_subnets
defines your subnet IDs. Terraform will use this to set up a peering connection between the HVN and your subnets. In addition, it will deploy the AKS cluster into these subnets.
Tip
The hvn_id
and cluster_id
must be unique within your HCP
organization.
Deploy resources
Now that you have the Terraform configuration saved in a main.tf
file, you are
ready to deploy the HVN, HCP Consul Dedicated cluster, and end-to-end development
environment.
Verify that you have completed all the steps listed in the Prerequisites.
Note
If you are deploying into an existing VNet, ensure the subnet has internet connectivity.
Initialize your Terraform configuration to download the necessary Terraform providers and modules.
Deploy the resources. Enter yes
when prompted to accept your changes.
Once you confirm, it will take a few minutes for Terraform to set up your end-to-end development environment. While you are waiting for Terraform to complete, proceed to the next section to review the Terraform configuration in more detail to better understand how to set up HCP Consul Dedicated for your workloads.
Review Terraform configuration
The Terraform configuration deploys an end-to-end development environment by:
- Creating a new HashiCorp virtual network (VNet) and single-node Consul development server
- Connecting the HVN with your Azure virtual network (VNet)
- Provisioning an AKS cluster and installing a Consul client
- Deploying HashiCups, a demo application that uses Consul service mesh
Prior to starting these steps, Terraform first retrieves information about your Azure environment.
Terraform uses a data resource to retrieve information about your current Azure subscription and your existing resource group.
The Terraform configuration also defines an Azure network security group. When Terraform configures a peering connection, it will add Consul-specific rules to this network security groups.
Create HVN and HCP Consul Dedicated
This Terraform configuration defines
hcp_hvn
and
hcp_consul_cluster
to deploy your HVN and HCP Consul Dedicated.
The HVN resource references the
hvn_id
andhvn_regions
local values. The resource also uses172.25.32.0/20
as a default for its CIDR block. Your HVN's CIDR block should not conflict with your VNet CIDR block.main.tfThe HCP Consul Dedicated resource references the HVN's ID. This is because HashiCorp will deploy the HCP Consul Dedicated cluster into the HVN. The HCP Consul Dedicated cluster has a public endpoint and is in the
development
cluster tier. Development tier HCP Consul Dedicated clusters only have one server agent.For production workloads, we do not recommend public endpoints for HCP Consul Dedicated.
Note
HCP Consul Dedicated Azure only supports
development
cluster tiers for public beta.main.tf
Connect HVN with VNet configuration
This Terraform configuration uses the
hashicorp/hcp-consul/azurerm
Terraform module to connect the HVN with your VNet configuration. This module:
- creates and accepts a peering connection between the HVN and VNet
- creates HVN routes that direct HCP traffic to subnet's CIDR ranges
- creates the necessary Azure ingress rules for HCP Consul Dedicated to communicate with the Consul clients
Notice that the module references the HVN and network security group in addition to your existing resource group, VNet, subnet.
Provision Azure AKS and install Consul client configuration
The quickstart configuration defines the AKS resource with 3 nodes.
This Terraform configuration uses the
hashicorp/hcp-consul/azurerm//modules/hcp-aks-client
Terraform module to install the Consul client on the AKS cluster.
In this tutorial, you will apply HCP Consul Dedicated's secure-by-default design with
Terraform by configuring your AKS cluster with the gossip encryption key,
the Consul CA cert, and a permissive ACL token. As a result, the hcp-aks-client
module requires the HCP Consul Dedicated cluster token (root ACL token) and HCP Consul Dedicated
client configuration (CA certificate and gossip encryption key).
The HCP Consul Dedicated cluster token bootstraps the cluster's ACL system. The configuration uses
hcp_consul_cluster_root_token
to generate a cluster token.Note
The resource will generate a cluster token, a sensitive value. For production workloads, refer to a list of recommendations for storing sensitive information in Terraform.
main.tfThe
hcp_consul_cluster
resource has attributes that store the cluster's CA certificate, gossip encryption key, private CA file, private HCP Consul Dedicated URL and more.main.tf
The hcp-aks-client
module deploys a Consul client onto the AKS by acting as a
wrapper for the Consul Helm chart. Refer to the
module source
for a complete list of resources deployed by the module.
Deploy HashiCups configuration
The hashicorp/hcp-consul/azurerm//modules/k8s-demo-app
Terraform module deploys the HashiCups demo app. The module source
has a complete list of YAML files that define the HashiCups services, intention CRDs, and ingress gateway.
Since HCP Consul Dedicated on Azure is secure by default, the datacenter is created with a "default deny" intention in place. This means that, by default, no services can interact with each other until an operator explicitly allows them to do so by creating intentions for each inter-service operation they wish to allow. The intentions.yaml
file defines service intentions between the HashiCups services through the ServiceIntentions
CRD, enabling them to communicate with each other.
Verify created resources
Once Terraform completes, you can verify the resources using the HCP Consul Dedicated UI or through the Consul CLI.
Consul UI
Retrieve your HCP Consul Dedicated dashboard URL and open it in your browser.
Next, retrieve your Consul root token. You will use this token to authenticate your Consul dashboard.
In your HCP Consul Dedicated dashboard, sign in with the root token you just retrieved.
You should find a list of services that include consul
and your HashiCups services.
Consul CLI configuration
In order to use the CLI, you must set environment variables that store your ACL token and HCP Consul Dedicated cluster address.
First, set your CONSUL_HTTP_ADDR
environment variable.
Then, set your CONSUL_HTTP_TOKEN
environment variable.
Retrieve a list of members in your datacenter to verify your Consul CLI is set up properly.
HashiCups application
The end-to-end development environment deploys HashiCups. Visit the hashicups
URL to verify that Terraform deployed HashiCups successfully, and its services
can communicate with each other.
Retrieve your HashiCups URL and open it in your browser.
Clean up resources
Now that you completed the tutorial, destroy the resources you created with
Terraform. Enter yes
to confirm the destruction process.
Next steps
In this tutorial, you have deployed an end-to-end deployment and review the Terraform configuration that defines the deployment.
If you encounter any issues, please contact the HCP team at support.hashicorp.com.