Nomad Job Scheduler

Sets up HashiCorp Nomad for container orchestration and job scheduling

Script Author

Rowan de Haas's avatar
Rowan de Haas
Script Author

Script Details

Created 7 months ago
Size 2 KB

Tags

Script Content

Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#cloud-config
package_update: true
package_upgrade: true

packages:
  - wget
  - unzip
  - docker.io

users:
  - name: nomad
    system: true
    shell: /bin/false
    home: /opt/nomad
    groups: docker

write_files:
  - path: /etc/nomad.d/nomad.hcl
    content: |
      datacenter = "dc1"
      data_dir = "/opt/nomad/data"
      log_level = "INFO"
      
      server {
        enabled = true
        bootstrap_expect = 1
      }
      
      client {
        enabled = true
        servers = ["127.0.0.1"]
      }
      
      ui_config {
        enabled = true
      }
      
      plugin "docker" {
        config {
          allow_privileged = false
        }
      }
    permissions: '0640'
    owner: nomad:nomad

  - path: /etc/systemd/system/nomad.service
    content: |
      [Unit]
      Description=Nomad
      Documentation=https://www.nomadproject.io/docs/
      Wants=network-online.target
      After=network-online.target
      
      [Service]
      Type=notify
      User=nomad
      Group=nomad
      ExecReload=/bin/kill -HUP $MAINPID
      ExecStart=/usr/local/bin/nomad agent -config /etc/nomad.d
      KillMode=process
      Restart=on-failure
      LimitNOFILE=65536

      [Install]
      WantedBy=multi-user.target

runcmd:
  # Download and install Nomad
  - cd /tmp
  - wget https://releases.hashicorp.com/nomad/1.6.0/nomad_1.6.0_linux_amd64.zip
  - unzip nomad_1.6.0_linux_amd64.zip
  - mv nomad /usr/local/bin/
  - chmod +x /usr/local/bin/nomad
  # Create directories
  - mkdir -p /opt/nomad/data
  - mkdir -p /etc/nomad.d
  - chown -R nomad:nomad /opt/nomad /etc/nomad.d
  # Start Docker
  - systemctl enable docker
  - systemctl start docker
  - usermod -aG docker nomad
  # Enable and start Nomad
  - systemctl enable nomad
  - systemctl start nomad
  # Configure firewall
  - ufw allow 4646/tcp
  - ufw allow 4647/tcp
  - ufw allow 4648/tcp

How to Use This Script

Cloud Provider Examples

Amazon EC2

aws ec2 run-instances
  --image-id ami-12345678
  --instance-type t3.micro
  --user-data file://script.yaml

DigitalOcean

doctl compute droplet create
  --image ubuntu-22-04-x64
  --size s-1vcpu-1gb
  --user-data-file script.yaml
  my-droplet

Google Cloud

gcloud compute instances create
  my-instance
  --metadata-from-file
  user-data=script.yaml