Deploy HashiCups
In this tutorial, you will deploy the initial containerized version of the HashiCups application.
HashiCups is a demo application that showcases product operations in our tutorials. It has several components that have already been converted to microservices that run in Docker containers. If you want to follow these steps with your own application, the minimum prerequisite is a Docker container that runs the monolithic application with Docker Compose.
The six components that make up the application are database
, payments-api
, product-api
, public-api
, frontend
, and nginx
.
database
stores the application data including products and orders. It does not have any upstream dependencies.payments-api
processes payments and does not have any upstream dependencies.product-api
serves product information and depends on thedatabase
service.public-api
serves as an interface for both theproduct-api
andpayments-api
services. It is dependent on bothproduct-api
andpayments-api
.frontend
renders and serves the application's web pages. It does not have any upstream dependencies.nginx
serves as the public entry point to the application. It is dependent on bothfrontend
andpublic-api
.
Containerized application services
Each of the services run as a Docker container. HashiCups runs with Docker Compose, which is a good starting point for migrating an application to Nomad.
A Nomad job specification file, also known as a jobspec, has many of the same configuration attributes as a Docker Compose file. These attributes share names as well, which simplifies the process to convert a Docker Compose file to a Nomad jobspec.
The file snippets and chart below demonstrate how attributes map between Docker Compose and Nomad jobspec files for HashiCups.
Docker Compose | Nomad jobspec | Definition |
---|---|---|
public-api | task "public-api" | Defines the name of the service. |
image | config > image | Defines the image location and version. |
environment | env | Defines environment variables for the container. |
links | upstreams | Defines service dependencies. |
ports | config > ports | Defines the ports for the container. |
Infrastructure overview
At the beginning of this tutorial you have a Nomad and Consul cluster with three server nodes, three private client nodes, and one publicly accessible client node. Each node runs a Consul agent and a Nomad agent.
The initial version of HashiCups represents a conversion from a Docker Compose file to a Nomad jobspec. It has the following attributes:
- All services run on the same node: the Nomad public client node
- Services are configured to use the Nomad client IP address or
localhost
- No service health monitoring
- No scaling of services
- No secure connection (HTTPS)
Prerequisites
This tutorial uses the infrastructure set up in the previous tutorial of this collection, Set up the cluster. Complete that tutorial to set up the infrastructure if you have not done so.
Deploy HashiCups
To deploy HashiCups, you will review the jobspec that describes it, submit the job to Nomad, and then verify that the job ran successfully.
Review the jobspec
In your terminal, change to the jobs
directory.
Open the 01.hashicups.nomad.hcl
jobspec file to view the contents.
The first section defines variables for the region and datacenter, Docker image versions, database configurations, and service port numbers.
The top level job "hashicups"
block has a nested group "hashicups"
block. The group
includes each service definition as a separate task
.
These blocks help organize a jobspec. A job can contain one or more groups and each group can contain one or more tasks. Nomad schedules tasks that are in the same group on the same client node.
The constraint
block instructs Nomad to schedule the job on a node that has a meta attribute nodeRole
set to ingress
. Nodes with this ingress
value are publicly accessible, which is necessary for the nginx
service. The constraint
block can be placed at the job, group, or task level.
Services use the client node IP address (NOMAD_IP_<label>
) or the full address with port (NOMAD_ADDR_<label>
) and the label of the service to get a resolvable address. Nomad provides these runtime environment variables to each task when the scheduler places them on the node.
Run the job
Submit the job to Nomad.
Verify deployment
After the job is deployed, you can verify that it is running.
Use the nomad job
command to retrieve information about the hashicups
job.
View the application by navigating to the public IP address of the nginx
service. The following compound command finds the node on which the hashicups
allocation is running (nomad job allocs
) and uses the ID of the found node to retrieve the public IP address of the node (nomad node status
). It then formats the output with the HTTP protocol.
Output from the above command.
Copy the IP address and open it in your browser to view the HashiCups application. You do not need to specify a port because nginx is running on port 80
.
You can click coffees and add them to a cart to test the application.
Cleanup
Before proceeding with the next tutorial, clean up the job in Nomad using the nomad job
command.
Next steps
In this tutorial, you deployed the initial containerized version of the HashiCups application.
In the next tutorial, you will integrate Consul service discovery into the HashiCups application.