DEV Community

Bartłomiej Danek
Bartłomiej Danek

Posted on • Originally published at bard.sh

Nomad

Nomad

  • workload orchestrator (containers, java, vm, windows, binaries)
  • single binary (linux, arm, osx, windows)
  • unified workflow for deployments
    • zero downtime (blue-green, canary, rolling-release)
    • batch
    • service / system
  • easily integrates with Consul and Vault
  • plugin system: CSI, CNI
  • uses HCL to define workloads

installation

asdf plugin-add nomad
asdf install nomad 1.8.0
asdf local nomad 1.8.0
Enter fullscreen mode Exit fullscreen mode

or developer.hashicorp.com/nomad/downloads

CLI

nomad run    <job>       # run/update a job
nomad stop   <job>       # stop a job
nomad status [job]       # check status
nomad agent  -dev        # run agent in dev mode
nomad exec   <alloc> sh  # exec into allocation
nomad -help              # global help
Enter fullscreen mode Exit fullscreen mode

architecture

  • Job - declared workload; defines desired state
  • Task Group - set of tasks that must run together
  • Driver - tool to run a workload (docker, exec, java...)
  • Task - smallest unit of work
  • Client - machine where tasks run
  • Allocation - mapping of task group to client
  • Evaluation - scheduling decision mechanism
  • Server - brain of the cluster
job
  \_ group
        \_ task
Enter fullscreen mode Exit fullscreen mode

job spec

job "api" {
  datacenters = ["dc1"]
  type        = "service"

  update {
    max_parallel = 2
    stagger      = "30s"
  }

  group "api" {
    count = 3

    network {
      port "http" {}
    }

    task "server" {
      driver = "docker"

      config {
        image = "my-org/api:1.2.3"
        ports = ["http"]
      }

      resources {
        cpu    = 500
        memory = 256
      }

      service {
        name = "api"
        port = "http"

        check {
          type     = "http"
          path     = "/health"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

dev mode

$ nomad agent -dev
# UI at http://localhost:4646
# do NOT use in production
Enter fullscreen mode Exit fullscreen mode

deployments

rolling update

update {
  max_parallel     = 1
  min_healthy_time = "10s"
  healthy_deadline = "3m"
}
Enter fullscreen mode Exit fullscreen mode

canary

update {
  canary       = 1
  max_parallel = 2
  auto_promote = false
}
Enter fullscreen mode Exit fullscreen mode

Promote manually after verifying:

nomad deployment promote <deployment-id>
Enter fullscreen mode Exit fullscreen mode

blue-green

update {
  canary       = 3   # equal to group count
  max_parallel = 3
  auto_promote = false
}
Enter fullscreen mode Exit fullscreen mode

plan and apply

$ nomad job plan api.nomad
Job Modify Index: 7
To submit with version verification:
  nomad run -check-index 7 api.nomad

$ nomad run -check-index 7 api.nomad
Enter fullscreen mode Exit fullscreen mode

history and revert

$ nomad job history api
Version = 3  Stable = true  Submit Date = ...
Version = 2  Stable = true  Submit Date = ...
Version = 1  Stable = false Submit Date = ...

$ nomad job revert api 2
Enter fullscreen mode Exit fullscreen mode

scheduled batch jobs

job "nightly-report" {
  type = "batch"

  periodic {
    crons = ["0 2 * * *"]
  }

  group "report" {
    task "run" {
      driver = "docker"
      config {
        image   = "my-org/reporter:latest"
        command = "/app/report"
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

For service discovery and load balancing with Consul - see Consul service discovery.


Originally published at https://bard.sh/posts/nomad_and_consul/

Top comments (0)