(1) Consul高可用安裝

1. 下載路徑

  https://www.consul.io/downloads.html

  壓縮包: consul_1.2.3_linux_amd64.zip

2. 環境

大網IP 小網IP node名稱
10.40.66.143 172.20.80.193 consul-s-1
10.40.66.145  172.20.80.195 consul-s-2
10.40.66.146 172.20.80.196 consul-s-3
10.40.66.144 172.20.80.194 consul-c-3

 

 

 

 

 

 

目錄:/home/consul

步驟3到步驟8,分別在四臺機子上執行(client和server配置文件不一樣,看步驟6)

3. 拷貝consul_1.2.3_linux_amd64.zip到/home/consul並解壓得到consul可執行文件

  unzip consul_1.2.3_linux_amd64.zip

4. 將consul移動到/usr/local/bin/,即添加到PATH目錄下

mv consul /usr/local/bin/

5. 檢測成功

-bash-4.2# consul version
Consul v1.2.3
Protocol 2 spoken by default, understands 2 to 3 (agent will automatically use protocol >2 when speaking to compatible agents)

6. 配置啓動文件config-dir.d/full.json(啓動默認加載config-dir.d下面所有.json文件)

server的配置文件:
{
"datacenter": "dc1",
"data_dir": "/home/consul/data",
"log_level": "INFO",
"node_name": "consul-s-1",
"server": true,
"bootstrap_expect": 3,
"bind_addr": "172.20.80.193", 
"client_addr": "0.0.0.0",
"ui": true, 
"retry_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"retry_interval": "30s",
"enable_debug": false,
"rejoin_after_leave": true,
"start_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"disable_update_check": true,
"log_file":"/home/consul/log/"
}
註釋:啓動命令 consul agent -config-dir /home/consul/config-dir.d,執行此命令即可啓動。

client的配置文件:
{
"datacenter": "dc1",
"data_dir": "/home/consul/data",
"log_level": "INFO",
"node_name": "consul-c-1",
"bind_addr": "172.20.80.194",
"client_addr": "0.0.0.0",
"retry_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"retry_interval": "30s",
"enable_debug": false,
"rejoin_after_leave": true,
"start_join": ["172.20.80.193","172.20.80.195","172.20.80.196"],
"disable_update_check": true,
"log_file":"/home/consul/log/"
}
註釋:啓動命令 consul agent -config-dir /home/consul/config-dir.d

重要參數解釋:
-data-dir(data_dir):指定agent儲存狀態的數據目錄,這是所有agent都必須的,對server尤其重要,因爲他們必須持久化集羣的狀態;
-config-dir:指定配置文件夾,通常以.d結尾(比如consul.d),裏面若干個配置文件,以.json結尾;
-config-file:指定一個配置文件;
-bind(bind_addr):集羣內部通信使用,默認0.0.0.0,即綁定到本機所有地址(本地多網卡有多地址),並使用第一個可用私有IPv4地址與集羣中其他成 員通信;
-client(client_addr):綁定客戶端命令訪問的地址,默認是127.0.0.1,只允許迴路訪問;如果要對外提供服務改成0.0.0.0(或者本機IP,consul客戶端命 令默認使用127.0.0.1:8500訪問服務,若綁定本機ip則需指定ip,即consul members -http-addr=10.40.66.146:8500);
-bootstrap(bootstrap):使用bootstrap模式,只有一個server的時候可以設置這個參數,此模式允許自我選爲Leader;
-bootstrap-expect(bootstrap_expect):要麼設置,要麼不設置,當設置此參數時,集羣中的server必須達到此數量才能觸發啓動集羣,此參數不能與- bootstrap同時使用;
ui:是否使用默認UI界面,可以使用"ui_dir": "/home/consul/consul_ui",指定自定義UI界面,需要在此目錄放入界面靜態服務文件;
log_file(-log-file):指定consul日誌文件,若指定到目錄(此目錄必須事先創建),則日誌格式類似consul-1537239737396229691.log,也可以自定義日誌 文件名稱;
-syslog(enable_syslog): 是否允許寫入系統日誌;
disable_update_check:禁止consul檢查更新,不然後臺總是不管打印更新失敗。

7. 配置systemctl啓動:/lib/systemd/system/consul.service

[Unit]
Description=Consul Server
After=network.target

[Service]
ExecStart=/usr/local/bin/consul agent -config-dir /home/consul/config-dir.d
ExecStop=/usr/local/bin/consul leave
ExecReload=/usr/local/bin/consul reload -config-dir /home/consul/config-dir.d
KillSignal=SIGINT
Restart=on-failure
RestartSec=1

[Install]
WantedBy=default.target

註釋:ExecReload=/bin/kill -HUP $MAINPID

8. 啓動服務並配置開機啓動

systemctl start consul.service
systemctl enable consul.service
systemctl status consul.service

9. consul常用命令

consul --help
consul version
consul info
consul members
consul force-leave :強制集羣中某個member進入“left”狀態。注意,如果member實際上還是alive,它會rejoin the cluster。
這個方法的真正意圖是強制移除那些“failed”的節點;
consul reload:觸發重新加載配置文件。等於kill -1或kill -HUP
consul leave : 優雅的離開並shutdown;
consul join 集羣主機IP:加入集羣,例如在 172.20.80.193 中 : consul join 172.20.80.197 172.20.80.198

-bash-4.2# consul members
Node Address Status Type Build Protocol DC Segment
consul-s-1 172.20.80.193:8301 alive server 1.2.3 2 dc1 <all>
consul-s-2 172.20.80.195:8301 alive server 1.2.3 2 dc1 <all>
consul-s-3 172.20.80.196:8301 alive server 1.2.3 2 dc1 <all>
consul-c-1 172.20.80.194:8301 alive client 1.2.3 2 dc1 <default>

-bash-4.2# consul operator raft list-peers
Node ID Address State Voter RaftProtocol
consul-s-2 a474d3bc-1380-7c69-794a-3f30c6c71636 172.20.80.195:8300 leader true 3
consul-s-3 f00591e2-53d9-524f-db34-ff811134fa09 172.20.80.196:8300 follower true 3
consul-s-1 1f1f4b54-c28e-4baa-4dbc-8a0df477142c 172.20.80.193:8300 follower true 3

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章