In the world of modern DevOps, deploying and managing applications efficiently is crucial. HashiCorp Nomad is a flexible, easy-to-use orchestrator that can deploy applications across multiple environments. When combined with Traefik, a dynamic reverse proxy and load balancer, you can efficiently manage and route traffic to your web applications. This article will guide you through setting up a simple web application using Nomad and Traefik.
Prerequisites
Before we begin, ensure you have the following:
- Nomad: Installed and running on your system. You can follow the official installation guide to set it up.
- Traefik: Installed and configured. Refer to the Traefik documentation for installation instructions.
- Docker: Installed on your system, as we’ll be deploying a Dockerized web application.
Step 1: Define the Nomad Job
First, create a Nomad job file to define how your application should be deployed. Below is an example of a Nomad job file for a simple web application:
job "web-app" {
datacenters = ["dc1"]
group "web" {
count = 3
network {
port "http" {
static = 8080
}
}
task "web" {
driver = "docker"
config {
image = "nginx:latest"
ports = ["http"]
}
service {
name = "web-app"
port = "http"
tags = [
"traefik.enable=true",
"traefik.http.routers.web-app.rule=Host(`example.com`)",
"traefik.http.services.web-app.loadbalancer.server.port=8080"
]
}
}
}
}
Explanation
- Job Definition: The job is named “web-app” and is set to run in the “dc1” datacenter.
- Group: The “web” group specifies that three instances of the task should be run.
- Network: The network block defines a static port (8080) for the HTTP service.
- Task: The task uses the Docker driver to run an NGINX container.
- Service: The service block registers the task with Nomad’s service discovery, and Traefik uses tags to configure routing.
Step 2: Deploy the Job
To deploy the job, save the above configuration to a file named web-app.nomad
and run the following command:
nomad run web-app.nomad
Nomad will schedule the job and deploy the specified number of instances across available nodes.
Step 3: Configure Traefik
Ensure Traefik is configured to use Nomad’s service discovery. You can set this up in the traefik.toml
or traefik.yml
configuration file:
providers:
nomad:
endpoint: "http://localhost:4646"
watch: true
constraints: "tag==traefik.enable=true"
Explanation
- Endpoint: The Nomad API endpoint.
- Watch: Enables watching for changes in Nomad services.
- Constraints: Filters services based on tags, ensuring only those with
traefik.enable=true
are considered.
Step 4: Access the Application
With everything set up, you can now access your web application through Traefik. Ensure your DNS is configured to point example.com
to your Traefik instance. Open a browser and navigate to http://example.com
to see your NGINX welcome page.
Conclusion
By leveraging HashiCorp Nomad and Traefik, you can efficiently deploy and manage web applications with dynamic load balancing and service discovery. This setup provides a scalable and resilient architecture, ideal for modern cloud-native applications.