Using Lattice with Terraform
What is Lattice? In the words of the website?—?Lattice aspires to make clustering containers easy.
Using components from Cloud Foundry, it includes the following:
- Cluster scheduler
- HTTP Load Balancing & health-checking
- Centralised logging
It’s a cloud-native platform small enough to run on a laptop, perfect for agile development and testing of microservices.
While you can run Lattice on a laptop, you can also deploy it to a cloud platform using Terraform. And what is Terraform? As you’ll see from the website, the concept is Infrastructure as Code?—?launching infrastructure on a cloud platform, using simple configuration files. If you’re used to using AWS, this might remind you of CloudFormation?—?the difference with Terraform is that it can be used with multiple providers, so you can create very similar configuration files for Google Compute Engine or DigitalOcean, as well as AWS and other third party services such as DNS. At the time of writing, VMware vCloud is not supported as a Terraform Provider?—?perhaps this is something we can contribute to the project in future.
Enough explanation?—?on with the setup.
First, download Terraform for your choice of OS: https://www.terraform.io/downloads.html. I am running it from a CentOS server, so I have downloaded the Linux version.
As shown, a number of binaries are extracted and their folder is then added to my PATH. Next, visit the Lattice Github page and download the Terraform file?—?here I have downloaded the AWS file.
We need to edit this file and add a couple of values:
- AWS access key & secret key: used when making API requests to AWS. You can create these using the AWS Management Console?—?once downloaded the secret key will no longer be available in the Management Console, so keep it safe!
- SSH key name & path: used to securely log into an EC2 instance without a password.
- AWS region: this will be used to look up the ID of an AMI, later used to create the servers.
Here we have specified a key name, the path to the .pem file locally, and the eu-west-1 region.
The Terraform configuration file we have downloaded is for a module?—?see the documentation for more information?—?essentially this means it’s a package of configurations, managed as a group, and that before we can launch it, we need to download it with terraform get:
Next, run terraform plan to see what changes Terraform will make.
As expected, it is planning on creating the Lattice module. We can now execute the plan using terraform apply?—?this will deploy the Lattice coordinator, the Lattice cells, and finally give you the details you need to connect to the new cluster. NB: even though these are being created with a couple of simple commands, these are real EC2 instances that can cost you real money!
Now that the cluster has been created?—?we need to connect to it. Download ltc?—?the Lattice CLI?—?from here and make it executable.
As shown there are a couple of commands. First, we need to target a lattice cluster using the details we were given earlier?—?then we can deploy a test application.
Very easy?—?I can now browse to the URL supplied (http://lattice-app.18.104.22.168.xip.io) and see the sample website.
However, to start with I’ve only got one container running?—?let’s scale things up.
Just like that, I now have five containers that are automatically being load-balanced via the same URL?—?if I refresh the page I can see the index changing between 0–4. I can also deliberately kill one of the containers, by browsing to http://lattice-app.22.214.171.124.xip.io/exit:
Monitoring the logs using ltc logs lattice-app shows that this was detected, and a new container was created to replace the one that was shut down:
Let’s tidy things up by removing the app with ltc remove lattice-app?—?the next step will be to remove the entire cluster. We can check what Terraform will do with terraform plan -destroy:
Finally, we can destroy the cluster with terraform destroy.
And there you have it?—?in a matter of minutes we have created a load-balanced, self-healing container cluster, and taken it down just as quickly.
- Lattice: http://lattice.cf/docs
- Terraform: https://www.terraform.io/intro/getting-started/install.html
Got a question?
Our experts have an answer