tjtjtjのメモ

自分のためのメモです

nomad チュートリアル Clustering

https://www.nomadproject.io/intro/getting-started/cluster.html

Starting the Server

サーバー構成ファイルを作成 server.hcl

# Increase log verbosity
log_level = "DEBUG"

# Setup data dir
data_dir = "/tmp/server1"

# Enable the server
server {
    enabled = true

    # Self-elect, should be 3 or 5 for production
    bootstrap_expect = 1
}

server.hcl で新エージェントを起動

vagrant@nomad:~$ nomad agent -config server.hcl
==> WARNING: Bootstrap mode enabled! Potentially unsafe operation.
==> Loaded configuration from server.hcl
==> Starting Nomad agent...
==> Nomad agent configuration:

       Advertise Addrs: HTTP: 10.0.2.15:4646; RPC: 10.0.2.15:4647; Serf: 10.0.2.15:4648
            Bind Addrs: HTTP: 0.0.0.0:4646; RPC: 0.0.0.0:4647; Serf: 0.0.0.0:4648
                Client: false
             Log Level: DEBUG
                Region: global (DC: dc1)
                Server: true
               Version: 0.8.6

==> Nomad agent started! Log data will stream in below
:

Starting the Clients

クライアント構成ファイル client1.hcl, 2.hcl 作成。ディレクトリ /tmp/client1,2 も作成。

# Increase log verbosity
log_level = "DEBUG"

# Setup data dir
data_dir = "/tmp/client1"          <--- client2

# Give the agent a unique name. Defaults to hostname
name = "client1"                   <--- client2

# Enable the client
client {
    enabled = true

    # For demo assume we are talking to server1. For production,
    # this should be like "nomad.service.consul:4647" and a system
    # like Consul used for service discovery.
    servers = ["127.0.0.1:4647"]
}

# Modify our port to avoid a collision with server1
ports {
    http = 5656                    <--- 5657
}

client1 のエージェント起動

$ mkdir /tmp/client1
$ sudo nomad agent -config client1.hcl
==> Loaded configuration from client1.hcl
==> Starting Nomad agent...
==> Nomad agent configuration:

       Advertise Addrs: HTTP: 10.0.2.15:5656
            Bind Addrs: HTTP: 0.0.0.0:5656
                Client: true
             Log Level: DEBUG
                Region: global (DC: dc1)
                Server: false
               Version: 0.8.6

==> Nomad agent started! Log data will stream in below:
:

client2 のエージェント起動

$ mkdir /tmp/client2
$ sudo nomad agent -config client2.hcl

サーバ確認

$ nomad server members
Name          Address    Port  Status  Leader  Protocol  Build  Datacenter  Region
nomad.global  10.0.2.15  4648  alive   true    2         0.8.6  dc1         global

ノード確認

$ nomad node status
ID        DC   Name     Class   Drain  Eligibility  Status
fecc79d6  dc1  client2  <none>  false  eligible     ready
28eb0853  dc1  client1  <none>  false  eligible     ready

Submit a Job

前使った example.nomad を使う。count=3 で image=redis4.0 だった。 チュートリアルは run だが plan してみる。最初の index は 0 だった。

$ nomad job status
No running jobs

$ nomad job plan example.nomad
+ Job: "example"
+ Task Group: "cache" (3 create)
  + Task: "redis" (forces create)

Scheduler dry-run:
- All tasks successfully allocated.

Job Modify Index: 0
To submit the job with version verification run:

nomad job run -check-index 0 example.nomad

When running the job with the check-index flag, the job will only be run if the
server side version matches the job modify index returned. If the index has
changed, another user has modified the job and the plan's results are
potentially invalid.

nomad job run。別のnodeにallocされたことがわかる。

$ nomad job run -check-index 0 example.nomad
==> Monitoring evaluation "23b0ee56"
    Evaluation triggered by job "example"
    Allocation "fa2fc9df" created: node "28eb0853", group "cache"      <--- 別のnodeにalloc
    Allocation "09d9ae5d" created: node "fecc79d6", group "cache"      <--- 別のnodeにalloc
    Allocation "f77a8705" created: node "fecc79d6", group "cache"
    Evaluation within deployment: "f0e91e62"
    Evaluation status changed: "pending" -> "complete"
==> Evaluation "23b0ee56" finished with status "complete"

job:example を確認

$ nomad job status
ID       Type     Priority  Status   Submit Date
example  service  50        running  2019-04-10T10:59:50Z

$ nomad status example
ID            = example
Name          = example
Submit Date   = 2019-04-10T10:59:50Z
Type          = service
Priority      = 50
Datacenters   = dc1
Status        = running
Periodic      = false
Parameterized = false

Summary
Task Group  Queued  Starting  Running  Failed  Complete  Lost
cache       0       0         3        0       0         0

Latest Deployment
ID          = f0e91e62
Status      = successful
Description = Deployment completed successfully

Deployed
Task Group  Desired  Placed  Healthy  Unhealthy  Progress Deadline
cache       3        3       3        0          2019-04-10T11:10:20Z

Allocations
ID        Node ID   Task Group  Version  Desired  Status   Created    Modified
09d9ae5d  fecc79d6  cache       0        run      running  1m47s ago  1m17s ago
f77a8705  fecc79d6  cache       0        run      running  1m47s ago  1m21s ago
fa2fc9df  28eb0853  cache       0        run      running  1m47s ago  1m25s ago