## 📝 Task 1. **Write two HTTP services** - **Service A** sends a request to **Service B** every **5 seconds**. * **Service B** waits a **random 2 – 10 seconds** before replying “Processed!”. 2. **Containerise** both services with **Podman** (so no Docker daemon needed). ([GitHub](https://github.com/hashicorp/nomad-driver-podman?utm_source=chatgpt.com)) 3. **Run both in Nomad dev‑mode** (`nomad agent ‑dev`). The agent is a single binary that acts as server + client for easy experiments. ([HashiCorp Developer](https://developer.hashicorp.com/nomad/tutorials/manage-clusters/clustering?utm_source=chatgpt.com)) 4. **Add a scaling rule** so that Nomad’s **Autoscaler** increases (and later shrinks) the replica count of Service B whenever its 90‑percentile response time rises above 6 s. * Autoscaler policies live in the `scaling` block of a job spec. ([HashiCorp Developer](https://developer.hashicorp.com/nomad/docs/job-specification/scaling?utm_source=chatgpt.com), [HashiCorp Developer](https://developer.hashicorp.com/nomad/tools/autoscaling/policy)) * Metrics come from a tiny Prometheus you’ll also run in Nomad; the Autoscaler has a built‑in Prometheus APM plug‑in. ([HashiCorp Developer](https://developer.hashicorp.com/nomad/tools/autoscaling/plugins/apm/prometheus?utm_source=chatgpt.com)) Give yourself 30 minutes to attempt this. Afterwards compare with the fully worked solution below. --- ## 🛠️ Detailed Solution ### 1. Install prerequisites ```bash # Nomad, Podman & Podman driver (Debian example) sudo apt-get install nomad podman -y curl -L -o /usr/local/bin/nomad-driver-podman \ https://releases.hashicorp.com/nomad-driver-podman/latest/nomad-driver-podman_linux_amd64 chmod +x /usr/local/bin/nomad-driver-podman ``` Add the plugin to your client config: ```hcl # /etc/nomad.d/podman.hcl plugin_dir = "/usr/local/bin" plugin "nomad-driver-podman" { config { socket_path = "unix:///run/podman/podman.sock" } } ``` Restart the agent, then confirm the driver is **ready** with `nomad node status -verbose | grep podman` ([support.hashicorp.com](https://support.hashicorp.com/hc/en-us/articles/20752437319955-Configuring-nomad-podman-driver-with-a-Nomad-Cluster-and-Running-a-Nomad-Job-Using-the-Podman-Driver?utm_source=chatgpt.com)) ### 2. Start Nomad & Autoscaler in dev mode ```bash nomad agent -dev & nomad-autoscaler agent -dev & ``` `-dev` runs a single‑binary server/client and ships sensible defaults for the Autoscaler too. ([HashiCorp Discuss](https://discuss.hashicorp.com/t/getting-to-know-the-nomad-autoscaler/16676?utm_source=chatgpt.com)) ### 3. Code the services Service A ```python from flask import Flask import requests, time, os B_HOST = os.environ.get("SERVICE_B_HOST", "service-b") B_PORT = os.environ.get("SERVICE_B_PORT", "5000") URL = f"http://{B_HOST}:{B_PORT}/" app = Flask(__name__) @app.route("/") def call_b(): try: r = requests.get(URL, timeout=15) return f"A got: {r.text}\n" except Exception as e: return f"A error: {e}\n", 503 if __name__ == "__main__": while True: try: print(requests.get(URL, timeout=15).text.strip()) except Exception: pass time.sleep(5) ``` Service B ```python from flask import Flask import random, time from prometheus_client import start_http_server, Histogram HIST = Histogram("request_latency_seconds", "B latency") app = Flask(__name__) @app.route("/") def reply(): delay = random.uniform(2,10) # 2 ‑ 10 s with HIST.time(): time.sleep(delay) return f"Processed in {delay:.1f}s\n" if __name__ == "__main__": start_http_server(9100) # expose Prometheus metrics app.run(host="0.0.0.0", port=5000) ``` ### 4. Build local images with Podman ```bash podman build -t local/service-a ./service-a podman build -t local/service-b ./service-b ``` ### 5. Tiny Prometheus job (metrics only) ```hcl # prometheus.nomad job "prom" { datacenters = ["dc1"] group "prom" { network { port "http" { static = 9090 } } task "prom" { driver = "podman" config { image = "prom/prometheus:latest" args = ["--config.file=/etc/prometheus/prom.yml"] } template { data = <<-EOF global: scrape_interval: 5s scrape_configs: - job_name: 'service_b' static_configs: - targets: ['${NOMAD_IP_http}:9100'] EOF destination = "local/prom.yml" } resources { cpu = 50 memory = 128 } } } } ``` Run it: `nomad job run prometheus.nomad` ### 6. Two‑service Job Spec with scaling ```hcl # two-services.nomad job "two-services" { datacenters = ["dc1"] type = "service" group "talkers" { network { port "a" { static = 5000 } port "b" { static = 5001 } } # ---------- Service B (slow) ---------- task "service-b" { driver = "podman" config { image = "local/service-b"; ports = ["b"] } service { name = "service-b"; port = "b" } resources { cpu = 200 memory = 256 } } # ---------- Service A (caller) -------- task "service-a" { driver = "podman" config { image = "local/service-a"; ports = ["a"] } env { SERVICE_B_HOST = "${NOMAD_IP_b}" SERVICE_B_PORT = "${NOMAD_PORT_b}" } resources { cpu = 100 memory = 128 } } # ---------- Autoscaling block ---------- scaling { enabled = true min = 1 max = 5 policy { evaluation_interval = "15s" cooldown = "1m" check "p90_latency" { source = "prometheus" query = "histogram_quantile(0.9, rate(request_latency_seconds_bucket[30s]))" strategy "target-value" { target = 6 } # seconds } } } } } ``` Why it works: * `${NOMAD_IP_b}` and `${NOMAD_PORT_b}` are **native env‑vars** injected by Nomad for intra‑job discovery. ([HashiCorp Developer](https://developer.hashicorp.com/nomad/docs/networking/service-discovery?utm_source=chatgpt.com), [HashiCorp Discuss](https://discuss.hashicorp.com/t/nomad-job-spec-environment-variable-best-practices/32736?utm_source=chatgpt.com)) * `histogram_quantile` gives the 90‑th percentile latency Prometheus metric. * `target-value` strategy scales replicas to keep the metric ≈ 6 s. ([HashiCorp Discuss](https://discuss.hashicorp.com/t/clarifying-nomad-autoscalers-target-value-strategy-plugin-behaviour/51754?utm_source=chatgpt.com), [Stack Overflow](https://stackoverflow.com/questions/78622233/nomad-auto-scaling-using-prometheus-metric?utm_source=chatgpt.com)) ### 7. Deploy & observe ```bash nomad job run two-services.nomad watch -n2 nomad job status two-services # observe count change watch -n2 curl -s http://localhost:5001/ # hit Service B ``` Within a couple of minutes you’ll see **new Service B allocations** spawn when latency grows and vanish after the cooldown once load eases. ([hub.docker.com](https://hub.docker.com/r/hashicorp/nomad-autoscaler?utm_source=chatgpt.com)) --- ## 📚 What you just learned | Concept | Where to read more | |---------|--------------------| | Podman driver plugin | GitHub repo & HashiCorp KB ([GitHub](https://github.com/hashicorp/nomad-driver-podman?utm_source=chatgpt.com), [support.hashicorp.com](https://support.hashicorp.com/hc/en-us/articles/20752437319955-Configuring-nomad-podman-driver-with-a-Nomad-Cluster-and-Running-a-Nomad-Job-Using-the-Podman-Driver?utm_source=chatgpt.com)) | | Dev‑mode agent | Nomad docs ([HashiCorp Developer](https://developer.hashicorp.com/nomad/tutorials/manage-clusters/clustering?utm_source=chatgpt.com)) | | Native service discovery | Nomad docs & forum Q&As ([HashiCorp Developer](https://developer.hashicorp.com/nomad/docs/networking/service-discovery?utm_source=chatgpt.com), [HashiCorp Discuss](https://discuss.hashicorp.com/t/i-dont-understand-networking-between-services/24470?utm_source=chatgpt.com)) | | Scaling block & policies | Nomad docs ([HashiCorp Developer](https://developer.hashicorp.com/nomad/docs/job-specification/scaling?utm_source=chatgpt.com), [HashiCorp Developer](https://developer.hashicorp.com/nomad/tools/autoscaling/policy)) | | Autoscaler & Prometheus APM | Nomad docs & plugin page ([HashiCorp Developer](https://developer.hashicorp.com/nomad/tools/autoscaling/plugins/apm/prometheus?utm_source=chatgpt.com)) | Try tweaking `target`, `min`, `max`, or switch to a **passthrough** strategy to scale directly on request‑rate instead of latency. Happy hacking!