到目前為止,我們的環境都準備就緒了,今天來啟動一個Nomad job.
記得剛開始學寫Kubernetes object時,是從網路上抄別人的來改,再慢慢越學越多變化,在Nomad有提供一個job init的指令可以產出一個範例,從範例來學習怎麼寫job specification.
執行下列的指令,會幫你產出一個名為example.nomad的job file, 這份job file裡面已有大部分常用的語法了,並附上說明。
$ nomad job init
Example job file written to example.nomad
如果覺得一大堆東西覺得太複雜,可以加入option -short, 命名為example-short.nomad, 內容會變得比較簡單好入門。
$ nomad job init -short example-short.nomad
Example job file written to example-short.nomad
# example-short.nomad
job "example" {
  datacenters = ["Nomad-ithome"]
  group "cache" {
    task "redis" {
      driver = "docker"
      config {
        image = "redis:3.2"
        port_map {
          db = 6379
        }
      }
      resources {
        cpu    = 500
        memory = 256
        network {
          mbits = 10
          port "db" {}
        }
      }
    }
  }
}
job: 一個job file, 只有一個job.
datacenters: 設定job要部屬到哪一個dc上。
group: 一個job可以有多個group.(類似pod)
task: 一個group可以有多個task.(最小的單位container或是batch等等)
The general hierarchy for a job is:
job \_ group \_ taskEach job file has only a single job, however a job may have multiple groups, and each group may have multiple tasks. Groups contain a set of tasks that are co-located on a machine.
driver: 設定使用docker container.
config: 設定docker image, port等等的資訊。
resources: 設定resource使用限制。
當一切都準備好,就送出我們第一個job吧!
在開始前nomad提供一個dry-run的指令,可以測試job被提交後是否可以被部屬。
$ nomad job plan example-short.nomad
+/- Job: "example"
- NomadTokenID: "723414cf-d755-1f35-e55c-d157253ffc1e"
  Task Group: "cache" (1 in-place update)
    Task: "redis"
Scheduler dry-run:
- All tasks successfully allocated.
Job Modify Index: 98542
To submit the job with version verification run:
nomad job run -check-index 98542 example-short.nomad
When running the job with the check-index flag, the job will only be run if the
job modify index given matches the server-side version. If the index has
changed, another user has modified the job and the plan's results are
potentially invalid.
看到job plan後的結果All tasks successfully allocated.我們可以放心的submit job了
$ nomad job run example-short.nomad
==> Monitoring evaluation "019407d7"
    Evaluation triggered by job "example"
    Evaluation within deployment: "b8ca5a9d"
    Allocation "4ed3652c" created: node "728069bc", group "cache"
    Evaluation status changed: "pending" -> "complete"
==> Evaluation "019407d7" finished with status "complete"
檢查job status
$ nomad job status
ID               Type     Priority  Status   Submit Date
example          service  50        running  2020-09-18T23:14:16+08:00
想查看更詳細的資訊可以執行
$ nomad job inspect example
{
    "Job": {
        "Affinities": null,
        "AllAtOnce": false,
        "Constraints": null,
        "ConsulToken": "",
        "CreateIndex": 96387,
        "Datacenters": [
            "Nomad-WHQ-DEV"
        ],
        "Dispatched": false,
        "ID": "example",
        "JobModifyIndex": 98542,
        "Meta": null,
        "Migrate": null,
        "ModifyIndex": 98550,
        "Multiregion": null,
        "Name": "example",
        "Namespace": "default",
....
....
....
透過Nomad Web UI查看,job有成功部屬並運行中,點入example後,還有更多資訊可以查看。

透過Nomad Web UI也可以stop job,或是進入container裡。