Introduction
Part One covered the use of the docker-host Terraform module to provision a Docker host (in Swarm mode) on Hetzner Cloud.
We can now again make use of Terraform modules to deploy a Traefik container on this (or any) host to serve as a load balancer/reverse proxy for other containered applications running on the same host.
In this tutorial we’ll setup Traefik to route incoming requests for a Vault backend service.
Network Layout - Traefik routing HTTP requests based on host name.
Two modules are used in a Terraform configuration to achieve this.
- (traefik-v2) for provisioning a Traefik container on the Docker host.
- (vault-dev) for provisioning a Vault container on the Docker host.
The docker-traefik-v2 module deploys a pre-configured Traefik (2.3.x) reverse proxy container. The same configuration deploys the HashiCorp Vault application using the docker-vault-dev module and connects it to the traefik
overlay network so Traefik can route incoming requests to it.
Prerequisites
- A Docker host (in Swarm mode)
- Terraform ≥ v0.13
- Knowledge of Docker & Terraform
Step 1 - Create the Terraform Configuration
Clone the example repository https://github.com/colinwilson/example-terraform-modules/tree/docker-traefik-and-vault
Step 2 - Set Values for the Required Input Variables
Using the example below, update the input variables in the terraform.tfvars
file to match your own.
The
docker_host
variable should be set to the connection string (required by the Docker provider).Set a password for accessing the Traefik UI,
traefik_password
. (The Traefik module hashes your password using Bcrypt)Set the hostnames via which both Traefik and Vault will be publicly accessible e.g.
traefik.example.com
,vault.example.com
(set DNS records for both hostnames pointing to the IP address of your Docker host). This allows Traefik to automatically configure an SSL certificate for both services via Let’s Encrypt using
httpChallenge
.Note: The DNS records for both hostnames should be configured BEFORE deploying your configuration (running
terraform apply
)Finally set the
acme_email
variable to an email address you wish to register with Let’s Encrypt for SSL certificate requests:
Step 3 - Deploy Traefik & Vault on the Docker Host
Switch to your example-terraform-modules
directory and initialize your configuration by running terraform init
.
Terraform will proceed to download the required provider plugins for both modules.
EXAMPLE OUTPUT - (click to expand)
Now run terraform apply
to create the Traefik and Vault containers.
EXAMPLE OUTPUT - (click to expand)
Respond to the prompt with yes
to apply the changes and continue.
Terraform will now create the Traefik and Vault containers and you should see a message confirming the creation of resources (line 1 below). The output
values generated by both modules should also be returned.
Step 4 - Confirm Traefik and Vault have been successfully deployed
After a few minutes Traefik should be proxying both its own UI and that of the Vault container. You should be able to visit the hostnames configured earlier to access each service.
Note: Both modules by default configure a staging SSL certificate, so expect to see browser warnings. Setting a
live_cert
variable in either module totrue
will configure a live cert for the corresponding service.
Visiting vault.example.com should present you with the default setup page to begin initialising the Vault install:
When visiting traefik.example.com, before being granted access to the Traefik dashboard your browser will prompt you to authenticate using the password configured in the terraform.tfvars
file earlier:
Note: The Traefik module has the RedirectScheme middleware configured by default so all non-secure HTTP requests are automatically redirected to HTTPS
Step 5. Clean up
You can destroy both containers by running terraform destroy
. After you respond to the prompt with yes
, Terraform will destroy all the containers and Docker volumes created prior.
EXAMPLE OUTPUT - (click to expand)
Conclusion
That’s it! You’ve now successfully deployed a Traefik load balancer/proxy container for routing incoming requests to backend services/containers. In this case to a Vault application service backend. The pre-built Terraform modules demonstrate how easy it is to automate the speedy deployment of infrastructure.