This set of Packer and Terraform scripts enable an easy deployment of a High Availability All Active Auto Scaling NGINX Plus Load Balancing configuration on Google Cloud. Adaptation of a guide found here.
Packer is a tool developed by Hashicorp to automate the creation of any type of machine/golden images in a variety of infrastructure providers. It follows an Infrastructure as Code design pattern that allows developers to write a simple json file describing the target machine image.
In this implementation, Packer is used to create a Google Cloud NGINX Plus image configured to work as a Load Balancer (ngx-plus) and a Google Cloud NGINX Open Source image configured to work as an application web server (ngx-oss).
Terraform is a tool developed by Hashicorp to write, plan, and create infrastructure on a variety of infrastructure providers. It follows an Infrastructure as Code design pattern that allows developers to write a simple Terraform configuration file describing the target infrastructure details.
In this implementation, Terraform is used to create a set of Google Cloud instance templates and instance group managers using the images previously created with Packer that can autoscale and recover in case of an unexpected instance crash. A set of networking rules are also defined to allow clients to access the instances via a static external IP or via the dynamic IPs created on instance startup.
- Install Packer.
- Install Terraform.
- Create a Google Cloud account.
- Create a Google Cloud project.
- Download the corresponding credentials for the previously created project. Terraform has a good guide on how to do this. You will need to wait until the Compute Engine has initialized before you are able to download the default account credentials. Copy and rename the credentials to
~/.gcloud/gcloud_credentials.json
. - Install the Google Cloud SDK.
- Install the Google Cloud SDK beta commands.
- Clone or download the files in the Packer Terraform All Active Nginx Plus LB folder in the NGINX Demos GitHub repo.
- Change the variables in
variables.tf
andpacker.json
to include your project id, the region/zone of your project, the desired machine type for your instances, the folder containing your NGINX Plus license, and the Google Cloud credentials file path. - Open a terminal, navigate to the location where you cloned the repository, and run
./setup.sh
. - To test the autoscaling features, install wrk and run
wrk -t12 -c400 -d30s http://$static_external_ip
where$static_external_ip
is the output of the Terraform script. - Once you're done with the demo and/or you want to delete the Google Cloud environment you've just created run
./cleanup.sh
.
The whole Packer configuration is included in the packer.json
file in the packer
folder. It is structured in three distinct sections.
variables
: This sections defines variables that are employed throughout thepacker.json
file such as the project id or project region.builders
: This section describes the target images to be built by packer. One LB image and two web servers images in this case.provisioners
: This section comprises the bulk of thepacker.json
file. It is here that the steps required to build each machine image are included.
The Terraform configuration is split into various *.tf
files in the terraform
folder. Each file is responsible for a distinct functionality.
autoscaler.tf
: Creates and setups Google Cloud autoscalers.healthcheck.tf
: Creates and setups Google Cloud healthchecks.instance.tf
: Creates and setups the Google Cloud Engine instances used to deploy the machine images previously created by Packer.network.tf
: Creates and setups the Google Cloud firewall, forwarding rules and external static IP addresses.output.tf
: Outputs the external static IP addresses to the terminal.providers.tf
: Defines the parameters required to establish and authenticate a connection with Google Cloud.variables.tf
: Defines various variables used throughout the rest of the Terraform files.versions.tf
: Set the version of Terraform required for this demo.