解壓consul壓縮包,把consul 文件上傳到centos服務器的 /root/consul 目錄下。
執行如下命令修改 /root/.bashrc 文件:
vi /root/.bashrc
按 i 編輯文件,在最後一行加入如下代碼:
export PATH=/root/consul:$PATH
然後先Esc鍵,接着輸入:wq
保存文件並推出vi。
退出 vi 以後,在命令行裏面再次輸入一遍 /root/.bashrc文件裏面剛纔我們放到最後一行的內容: export PATH=/root/consul:$PATH
在命令行裏面輸入 consul ,出現如下內容說明安裝成功。
[root@iZm5e5w8irul9hn3uva51gZ ~]# consul
Usage: consul [--version] [--help] <command> [<args>]
Available commands are:
acl Interact with Consul's ACLs
agent Runs a Consul agent
catalog Interact with the catalog
config Interact with Consul's Centralized Configurations
connect Interact with Consul Connect
debug Records a debugging archive for operators
event Fire a new event
exec Executes a command on Consul nodes
force-leave Forces a member of the cluster to enter the "left" state
info Provides debugging information for operators.
intention Interact with Connect service intentions
join Tell Consul agent to join cluster
keygen Generates a new encryption key
keyring Manages gossip layer encryption keys
kv Interact with the key-value store
leave Gracefully leaves the Consul cluster and shuts down
lock Execute a command holding a lock
login Login to Consul using an auth method
logout Destroy a Consul token created with login
maint Controls node or service maintenance mode
members Lists the members of a Consul cluster
monitor Stream logs from a Consul agent
operator Provides cluster-level tools for Consul operators
reload Triggers the agent to reload configuration files
rtt Estimates network round trip time between nodes
services Interact with services
snapshot Saves, restores and inspects snapshots of Consul server state
tls Builtin helpers for creating CAs and certificates
validate Validate config files/directories
version Prints the Consul version
watch Watch for changes in Consul
創建/root/consul/data_dir目錄。創建 /root/consul/consul.d 目錄,裏面創建web.json 文件。
現在web.json 是配置文件。內容如下:
{
"data_dir": "/root/consul/data_dir",
"services":[
{
"name": "demo_web_1_0_6",
"tags": ["1.0.6"],
"port": 8016,
"check": {
"id": "api4",
"name": "HTTP API on port 8013",
"http": "http://172.31.130.126:8013",
"tls_skip_verify": false,
"method": "GET",
"header": {"x-foo":["bar", "baz"]},
"interval": "10s",
"timeout": "3s"
}
},
{
"name": "demo_web_1_0_7",
"tags": ["1.0.7"],
"port": 8016,
"check": {
"id": "api5",
"name": "HTTP API on port 8016",
"http": "http://172.31.130.126:8016",
"tls_skip_verify": false,
"method": "GET",
"header": {"x-foo":["bar", "baz"]},
"interval": "10s",
"timeout": "3s"
}
},
{
"name": "demo_web_1_0_8",
"tags": ["1.0.8"],
"port": 8017,
"check": {
"id": "api_demo_web_1_0_8",
"name": "HTTP API on port 8017",
"http": "http://172.31.130.126:8017",
"tls_skip_verify": false,
"method": "GET",
"header": {"x-foo":["bar", "baz"]},
"interval": "10s",
"timeout": "3s"
}
},
{
"name": "demo_web_gateway",
"tags": ["gateway"],
"port": 8090,
"check": {
"id": "api6",
"name": "HTTP API on port 8090",
"http": "http://172.31.130.126:8090/centGateway/health",
"tls_skip_verify": false,
"method": "GET",
"header": {"x-foo":["bar", "baz"]},
"interval": "10s",
"timeout": "3s"
}
}
]
}
先解釋一下配置項,data_dir 是consul存放數據的目錄,這裏設置成 /root/consul/data_dir
services數組是要進行健康檢查的服務。這裏用http地址進行檢查,interval 表示檢查的間隔,這裏規定10s檢查一次。timeout 是請求多長時間沒響應算作失敗,這裏是3秒不響應算失敗。id必須不一樣。port指定請求的端口。
啓動
nohup consul agent -dev -enable-local-script-checks -config-dir=/root/consul/consul.d -client 0.0.0.0 > /data0/out/consul.out &
停止
consul leave
重新加載配置
consul reload
當consul啓動後,假設服務器地址是 192.168.1.16,瀏覽器輸入 http://192.168.1.16:8500
這裏可以看到所有的服務。帶紅色叉號表示服務不正常,比如服務停止運行。有些讀者可能會覺得奇怪,爲什麼 Health Checks 等於 2 ? 這裏有兩個健康檢查,一個是我在web.json 配置文件中配置的服務;另一個是consul 自己節點的健康檢查。因爲 consul 可以搭建集羣,所以 consul 會檢查自己的節點是否正常。我們在本文中只用了一個consul節點,因此必然會有一個額外的健康檢查。