學習使用nomad
- 上期consul搭建完成以后,就可以根據這期的內容,部署nomad服務,nomad會自動找到本機的8500consul端口,主動去注冊服務。
啟動服務器
第一步是為服務器創建配置文件。無論是從下載的文件github,或粘貼到一個名為server.hcl:
vim 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}
這是一個相當最小的服務器配置文件,但只能以僅服務器方式啟動代理,並將其選為leader。應該對生產進行的主要變化是運行多台服務器,並更改相應的bootstrap_expect值。
創建文件后,在新選項卡中啟動代理:
$ sudo nomad agent -config server.hcl
==> WARNING: Bootstrap mode enabled! Potentially unsafe operation.
==> Starting Nomad agent...
==> Nomad agent configuration:
Client: false
Log Level: DEBUG
Region: global (DC: dc1)
Server: true
Version: 0.6.0
==> Nomad agent started! Log data will stream in below:
[INFO] serf: EventMemberJoin: nomad.global 127.0.0.1
[INFO] nomad: starting 4 scheduling worker(s) for [service batch _core]
[INFO] raft: Node at 127.0.0.1:4647 [Follower] entering Follower state
[INFO] nomad: adding server nomad.global (Addr: 127.0.0.1:4647) (DC: dc1)
[WARN] raft: Heartbeat timeout reached, starting election
[INFO] raft: Node at 127.0.0.1:4647 [Candidate] entering Candidate state
[DEBUG] raft: Votes needed: 1
[DEBUG] raft: Vote granted. Tally: 1
[INFO] raft: Election won. Tally: 1
[INFO] raft: Node at 127.0.0.1:4647 [Leader] entering Leader state
[INFO] nomad: cluster leadership acquired
[INFO] raft: Disabling EnableSingleNode (bootstrap)
[DEBUG] raft: Node 127.0.0.1:4647 updated peer set (2): [127.0.0.1:4647]
我們可以看到,客戶端模式被禁用,我們只是作為服務器運行。這意味着該服務器將管理狀態並進行調度決策,但不會執行任何任務。現在我們需要一些代理來運行任務!
啟動客戶端
與服務器類似,我們必須先配置客戶端。請從github下載client1和client2的配置 ,或將以下內容粘貼到client1.hcl:
# Increase log verbosity
log_level = "DEBUG"
# Setup data dir
data_dir = "/tmp/client1"
# 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
}
將該文件復制client2.hcl並更改data_dir為“/ tmp / httpclient2 ”並將端口更改為5657.一旦創建了這兩個文件,client1.hcl並client2.hcl打開每個選項卡並啟動代理程序:
$ sudo nomad agent -config client1.hcl
==> Starting Nomad agent...
==> Nomad agent configuration:
Client: true
Log Level: DEBUG
Region: global (DC: dc1)
Server: false
Version: 0.6.0
==> Nomad agent started! Log data will stream in below:
[DEBUG] client: applied fingerprints [host memory storage arch cpu]
[DEBUG] client: available drivers [docker exec]
[DEBUG] client: node registration complete
...
在輸出中,我們可以看到代理僅在客戶端模式下運行。該代理將可用於運行任務,但不會參與管理集群或做出調度決策。
使用node-status命令 我們應該看到ready狀態中的兩個節點:
$ nomad node-status
ID Datacenter Name Class Drain Status
fca62612 dc1 nomad <none> false ready
c887deef dc1 nomad <none> false ready
我們現在有一個簡單的三節點集群運行。演示和完整生產集群之間的唯一區別是,我們運行的是單個服務器,而不是三個或五個。
提交工作
現在我們有一個簡單的集群,我們可以用它來安排一個工作。我們還應該擁有example.nomad之前的作業文件,但是確認count仍然設置為3。
然后,使用run命令提交作業:
$ nomad init
$ nomad run example.nomad
==> Monitoring evaluation "8e0a7cf9"
Evaluation triggered by job "example"
Evaluation within deployment: "0917b771"
Allocation "501154ac" created: node "c887deef", group "cache"
Allocation "7e2b3900" created: node "fca62612", group "cache"
Allocation "9c66fcaf" created: node "c887deef", group "cache"
Evaluation status changed: "pending" -> "complete"
==> Evaluation "8e0a7cf9" finished with status "complete"
我們可以在輸出中看到調度程序為其中一個客戶機節點分配了兩個任務,剩下的任務分配給第二個客戶端。
我們可以再次使用status命令驗證:
$ nomad status example
ID = example
Name = example
Submit Date = 07/26/17 16:34:58 UTC
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 = fc49bd6c
Status = running
Description = Deployment is running
Deployed
Task Group Desired Placed Healthy Unhealthy
cache 3 3 0 0
Allocations
ID Eval ID Node ID Task Group Desired Status Created At
501154ac 8e0a7cf9 c887deef cache run running 08/08/16 21:03:19 CDT
7e2b3900 8e0a7cf9 fca62612 cache run running 08/08/16 21:03:19 CDT
9c66fcaf 8e0a7cf9 c887deef cache run running 08/08/16 21:03:19 CDT
我們可以看到我們的所有任務已經分配並正在運行。一旦我們對我們的工作感到滿意,我們就可以把它刪掉了nomad stop
。
使用nomad UI
在nomad官方文檔上,nomad UI似乎沒能很好的實現,雖然官方說0.7版本以后,ui被集成了,但是在我的本地環境中,瀏覽器訪問nomadIP:4646,會出現404錯誤,鑒於我一直沒有解決這個404問題。我找到並使用github上一位大牛制作的UI,https://github.com/jippi/hashi-ui。
UI更新
- 今天更新了nomad 0.7版本,在github上下載nomad,可以使用官方的UI。
- 這里是github上nomad項目的ui目錄,https://github.com/hashicorp/nomad/tree/master/ui
- 按照readme提示,安裝依賴,即可! 如果你想要使用自定義的地址訪問,使用這條命令:ember serve --proxy http://10.30.0.52:4646 (10.30.0.52換成你的外網IP,4646換成你自定義的端口)